KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import com.sun.enterprise.deployment.Descriptor;
29 import com.sun.enterprise.deployment.JspConfigDescriptor;
30 import com.sun.enterprise.deployment.JspGroupDescriptor;
31 import com.sun.enterprise.deployment.WebBundleDescriptor;
32 import com.sun.enterprise.deployment.WebComponentDescriptor;
33 import com.sun.enterprise.deployment.web.SecurityConstraint;
34 import com.sun.enterprise.deployment.web.ServletFilterMapping;
35 import com.sun.enterprise.deployment.web.WebResourceCollection;
36 import com.sun.enterprise.tools.verifier.Result;
37 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
38
39 /**
40  * The content of the url-pattern element follows the rules specified in
41  * section 10 of the servlet spec.
42  * This abstract class serves as the base of some concrete classes like
43  * URLPatternErrorCheck, URLPatternWarningCheck & URLPatternContainsCRLF.
44  * This class implements the check method, but inside the check method it calls a pure virtual function
45  * called checkUrlPatternAndSetResult. This pure virtual function is implemented in the two derived classes.
46  */

47 public abstract class URLPattern extends WebTest implements WebCheck {
48     //These variables are needed because Result object does not maintain state.
49
protected boolean oneFailed=false, oneWarning=false;
50
51     /**
52      * The content of the url-pattern element follows the rules specified in
53      * section 10 of the servlet spec.
54      *
55      * @param descriptor the Web deployment descriptor
56      *
57      * @return <code>Result</code> the results for this assertion
58      */

59     public Result check(WebBundleDescriptor descriptor) {
60
61         Result result = getInitializedResult();
62         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
63
64         result.setStatus(Result.NOT_APPLICABLE);
65         result.addNaDetails(smh.getLocalString
66                 ("tests.componentNameConstructor",
67                         "For [ {0} ]",
68                         new Object JavaDoc[] {compName.toString()}));
69         result.addNaDetails(smh.getLocalString
70                 (getClass().getName() + ".notApplicable",
71                         "There is no url-pattern element within the web archive [ {0} ]",
72                         new Object JavaDoc[] {descriptor.getName()}));
73         checkWebResourceCollections(descriptor, result, compName);
74         checkServletMappings(descriptor, result, compName);
75         checkServletFilterMappings(descriptor, result, compName);
76         checkJspGroupProperties(descriptor, result, compName);
77
78         if(oneFailed) result.setStatus(Result.FAILED);
79         else if(oneWarning) result.setStatus(Result.WARNING);
80         return result;
81     }
82
83     //Each derived test should implement this method
84
protected abstract void checkUrlPatternAndSetResult(String JavaDoc urlPattern, Descriptor descriptor, Result result, ComponentNameConstructor compName);
85
86     private void checkWebResourceCollections(WebBundleDescriptor descriptor, Result result, ComponentNameConstructor compName){
87         Enumeration JavaDoc e=descriptor.getSecurityConstraints();
88         while (e.hasMoreElements()) {
89             SecurityConstraint securityConstraint = (SecurityConstraint) e.nextElement();
90             Enumeration JavaDoc ee = securityConstraint.getWebResourceCollections();
91             while (ee.hasMoreElements()) {
92                 WebResourceCollection webResourceCollection = (WebResourceCollection) ee.nextElement();
93                 Enumeration JavaDoc eee = webResourceCollection.getUrlPatterns();
94                 while (eee.hasMoreElements()) {
95                     checkUrlPatternAndSetResult((String JavaDoc) eee.nextElement(), descriptor, result, compName);
96                 }
97             }
98         }
99     }
100
101     private void checkServletMappings(WebBundleDescriptor descriptor, Result result, ComponentNameConstructor compName){
102         for(Iterator JavaDoc iter=descriptor.getWebComponentDescriptorsSet().iterator();iter.hasNext();)
103             for(Iterator JavaDoc iter2=((WebComponentDescriptor)iter.next()).getUrlPatternsSet().iterator(); iter2.hasNext();
104                 checkUrlPatternAndSetResult((String JavaDoc)iter2.next(), descriptor, result, compName));
105     }
106
107     private void checkServletFilterMappings(WebBundleDescriptor descriptor, Result result, ComponentNameConstructor compName){
108         for(Iterator JavaDoc iter=descriptor.getServletFilterMappings().iterator();iter.hasNext();){
109             ServletFilterMapping filterMapping=(ServletFilterMapping)iter.next();
110             if(filterMapping.getURLPatterns().size() > 0) {
111                 for(String JavaDoc url : filterMapping.getURLPatterns())
112                     checkUrlPatternAndSetResult(url, descriptor, result, compName);
113             }
114         }
115     }
116
117     //This method checks for url-patterns appearing in jsp-config element in an web-app.
118
private void checkJspGroupProperties(WebBundleDescriptor descriptor, Result result, ComponentNameConstructor compName){
119         JspConfigDescriptor jspC=descriptor.getJspConfigDescriptor();
120         if(jspC==null) return;
121         for(Iterator JavaDoc iter=jspC.getJspGroupSet().iterator();iter.hasNext();){
122             for(Iterator JavaDoc iter2=((JspGroupDescriptor)iter.next()).getUrlPatternsSet().iterator();
123                 iter2.hasNext();
124                 checkUrlPatternAndSetResult((String JavaDoc)iter2.next(), descriptor, result, compName));
125         }
126     }
127 }
128
Popular Tags