KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > WelcomeFile


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.web;
24
25 import com.sun.enterprise.tools.verifier.tests.web.WebTest;
26 import java.util.jar.*;
27 import java.util.*;
28 import java.io.*;
29 import java.util.regex.Pattern JavaDoc;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.*;
33
34 /**
35  * Welcome file element contains the file name to use as a default welcome file
36  * within web application test.
37  */

38 public class WelcomeFile extends WebTest implements WebCheck {
39     
40     /**
41      * Welcome file element contains the file name to use as a default welcome file
42      * within web application test.
43      *
44      * @param descriptor the Web deployment descriptor
45      *
46      * @return <code>Result</code> the results for this assertion
47      */

48     public Result check(WebBundleDescriptor descriptor) {
49         
50         Result result = getInitializedResult();
51         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
52         
53         if(!isApplicable(descriptor, result)) {
54             return result;
55         }
56         
57         // Check whether the syntax of welcome-file is correct or not.
58
boolean syntaxOK = checkSyntax(descriptor, result);
59         
60         // check whether each welcome-file exists or not
61
//boolean exists = checkExists(descriptor, result);
62
boolean exists = true;
63         
64         // report WARNING if the syntax is wrong or none of welcome-files exist.
65
if (!syntaxOK) {
66             result.setStatus(Result.FAILED);
67         } else if (!exists) {
68             result.setStatus(Result.WARNING);
69         } else {
70             result.setStatus(Result.PASSED);
71         }
72         
73         return result;
74     }
75     
76     private boolean isApplicable(WebBundleDescriptor descriptor, Result result) {
77         boolean applicable = true;
78         if (!descriptor.getWelcomeFiles().hasMoreElements()) {
79             ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
80             addNaDetails(result, compName);
81             result.notApplicable(smh.getLocalString
82                     (getClass().getName() + ".notApplicable",
83                     "There are no welcome files within the web archive [ {0} ]",
84                     new Object JavaDoc[] {descriptor.getName()}));
85             applicable = false;
86         }
87         return applicable;
88     }
89     
90     private boolean checkSyntax(WebBundleDescriptor descriptor, Result result) {
91         boolean syntaxOK = true;
92         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
93         for (Enumeration e = descriptor.getWelcomeFiles() ; e.hasMoreElements() ;) {
94             String JavaDoc welcomefile = (String JavaDoc) e.nextElement();
95             if (welcomefile.startsWith("/") || welcomefile.endsWith("/")) {
96                 addErrorDetails(result, compName);
97                 result.addErrorDetails(smh.getLocalString(
98                         getClass().getName() + ".failed1",
99                         "Error : Servlet 2.3 Spec 9.9 Welcome file URL [ {0} ] must be partial URLs with no trailing or leading /",
100                         new Object JavaDoc[] {welcomefile, descriptor.getName()}));
101                 syntaxOK = false;
102             }
103         }
104         return syntaxOK;
105     }
106     
107     private boolean checkExists(WebBundleDescriptor descriptor, Result result) {
108         findDynamicResourceURIs(descriptor);
109         boolean exists = false;
110         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
111         for (Enumeration e = descriptor.getWelcomeFiles() ; e.hasMoreElements() ;) {
112             String JavaDoc welcomeFile = (String JavaDoc) e.nextElement();
113             if(fileExists(descriptor, welcomeFile) || urlMatches(welcomeFile)) {
114                 exists = true;
115                 addGoodDetails(result, compName);
116                 result.addGoodDetails(smh.getLocalString
117                         (getClass().getName() + ".passed",
118                         "Welcome file [ {0} ] contains the file name to use as a default welcome file within web application [ {1} ]",
119                         new Object JavaDoc[] {welcomeFile, descriptor.getName()}));
120             } else {
121                 addWarningDetails(result, compName);
122                 result.addWarningDetails(smh.getLocalString
123                         (getClass().getName() + ".failed",
124                         "Error: Welcome file [ {0} ] is not found within [ {1} ] or does not contain the file name to use as a default welcome file within web application [ {2} ]",
125                         new Object JavaDoc[] {welcomeFile, descriptor.getModuleDescriptor().getArchiveUri(), descriptor.getName()}));
126             }
127         }
128         return exists;
129     }
130     
131     private boolean fileExists(WebBundleDescriptor descriptor, String JavaDoc fileName) {
132         File webCompRoot = new File(getAbstractArchiveUri(descriptor));
133         File welcomeFile = new File(webCompRoot, fileName);
134         return welcomeFile.exists();
135     }
136     
137     private Set dynamicResourceUrlPatterns = new HashSet();
138     
139     private void findDynamicResourceURIs(WebBundleDescriptor descriptor) {
140         Set webComponentDescriptors = descriptor.getWebComponentDescriptorsSet();
141         for(Iterator iter = webComponentDescriptors.iterator(); iter.hasNext(); ) {
142             WebComponentDescriptor webComponentDescriptor = (WebComponentDescriptor) iter.next();
143             dynamicResourceUrlPatterns.addAll(webComponentDescriptor.getUrlPatternsSet());
144         }
145         // Remove the leading and trailing '/' character from each dynamicResourceUrlPatters
146
Set newUrlPatterns = new HashSet();
147         for(Iterator iter = dynamicResourceUrlPatterns.iterator(); iter.hasNext() ;) {
148             String JavaDoc urlPattern = (String JavaDoc) iter.next();
149             if (urlPattern.startsWith("/")) {
150                 urlPattern = urlPattern.substring(1);
151             }
152             if (urlPattern.endsWith("/")) {
153                 urlPattern = urlPattern.substring(0, urlPattern.length() - 1);
154             }
155             newUrlPatterns.add(urlPattern);
156         }
157         dynamicResourceUrlPatterns = newUrlPatterns;
158     }
159     
160     private boolean urlMatches(String JavaDoc url) {
161         for(Iterator iter = dynamicResourceUrlPatterns.iterator(); iter.hasNext() ;) {
162             boolean matches = Pattern.matches((String JavaDoc)iter.next(), url);
163             if (matches) {
164                 return true;
165             }
166         }
167         return false;
168     }
169 }
170
Popular Tags