KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
27 import java.io.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import com.sun.enterprise.util.FileClassLoader;
32
33
34 /**
35  * The http-method element contains the name of web resource collection's HTTP
36  * method
37  */

38 public class WebResourceHTTPMethod extends WebTest implements WebCheck {
39
40     
41     /**
42      * The http-method element contains the name of web resource collection's HTTP
43      * method
44      *
45      * @param descriptor the Web deployment descriptor
46      *
47      * @return <code>Result</code> the results for this assertion
48      */

49     public Result check(WebBundleDescriptor descriptor) {
50
51     Result result = getInitializedResult();
52     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
53
54     if (descriptor.getSecurityConstraints().hasMoreElements()) {
55         boolean oneFailed = false;
56         boolean foundIt = false;
57             int na = 0;
58             int noSc = 0;
59             int naWRC = 0;
60             int noWRC = 0;
61         // get the http method's in this .war
62
for (Enumeration e = descriptor.getSecurityConstraints() ; e.hasMoreElements() ;) {
63         foundIt = false;
64                 noSc++;
65         SecurityConstraintImpl securityConstraintImpl = (SecurityConstraintImpl) e.nextElement();
66                 if (securityConstraintImpl.getWebResourceCollections().hasMoreElements()) {
67             for (Enumeration ee = securityConstraintImpl.getWebResourceCollections(); ee.hasMoreElements();) {
68             noWRC++;
69             WebResourceCollectionImpl webResourceCollection = (WebResourceCollectionImpl) ee.nextElement();
70             if (webResourceCollection.getHttpMethods().hasMoreElements()) {
71                 for (Enumeration eee = webResourceCollection.getHttpMethods(); eee.hasMoreElements();) {
72                 String JavaDoc webRCHTTPMethod = (String JavaDoc) eee.nextElement();
73                 // valid methods are the following
74
if ((webRCHTTPMethod.equals("OPTIONS")) ||
75                     (webRCHTTPMethod.equals("GET")) ||
76                     (webRCHTTPMethod.equals("HEAD")) ||
77                     (webRCHTTPMethod.equals("POST")) ||
78                     (webRCHTTPMethod.equals("PUT")) ||
79                     (webRCHTTPMethod.equals("DELETE")) ||
80                     (webRCHTTPMethod.equals("TRACE")) ||
81                     (webRCHTTPMethod.equals("CONNECT"))) {
82                     foundIt = true;
83                 } else {
84                     foundIt = false;
85                 }
86        
87                 if (foundIt) {
88                     result.addGoodDetails(smh.getLocalString
89                        ("tests.componentNameConstructor",
90                     "For [ {0} ]",
91                     new Object JavaDoc[] {compName.toString()}));
92                     result.addGoodDetails(smh.getLocalString
93                               (getClass().getName() + ".passed",
94                                "http-method [ {0} ] is valid HTTP method name within web resource collection [ {1} ] in web application [ {2} ]",
95                                new Object JavaDoc[] {webRCHTTPMethod, webResourceCollection.getName(), descriptor.getName()}));
96                 } else {
97                     if (!oneFailed) {
98                     oneFailed = true;
99                     }
100                     result.addErrorDetails(smh.getLocalString
101                        ("tests.componentNameConstructor",
102                     "For [ {0} ]",
103                     new Object JavaDoc[] {compName.toString()}));
104                     result.addErrorDetails(smh.getLocalString
105                                (getClass().getName() + ".failed",
106                                 "Error: http-method [ {0} ] is not valid HTTP method name within web resource collection [ {1} ] in web application [ {2} ]",
107                                 new Object JavaDoc[] {webRCHTTPMethod, webResourceCollection.getName(), descriptor.getName()}));
108                 }
109                 }
110             } else {
111                 result.addNaDetails(smh.getLocalString
112                        ("tests.componentNameConstructor",
113                     "For [ {0} ]",
114                     new Object JavaDoc[] {compName.toString()}));
115                 result.notApplicable(smh.getLocalString
116                          (getClass().getName() + ".notApplicable1",
117                           "There are no web http-methods in the web resource collection [ {0} ] within [ {1} ]",
118                           new Object JavaDoc[] {webResourceCollection.getName(), descriptor.getName()}));
119                 naWRC++;
120             }
121             }
122                 } else {
123             result.addNaDetails(smh.getLocalString
124                        ("tests.componentNameConstructor",
125                     "For [ {0} ]",
126                     new Object JavaDoc[] {compName.toString()}));
127                     result.notApplicable(smh.getLocalString
128                                          (getClass().getName() + ".notApplicable2",
129                                           "There are no web web resource collections in the web security constraint within [ {0} ]",
130                                           new Object JavaDoc[] {descriptor.getName()})); na++;
131                 }
132         }
133         if (oneFailed) {
134         result.setStatus(Result.FAILED);
135             } else if ((na == noSc) || (naWRC == noWRC)) {
136                 result.setStatus(Result.NOT_APPLICABLE);
137         } else {
138         result.setStatus(Result.PASSED);
139         }
140     } else {
141         result.addNaDetails(smh.getLocalString
142                        ("tests.componentNameConstructor",
143                     "For [ {0} ]",
144                     new Object JavaDoc[] {compName.toString()}));
145         result.notApplicable(smh.getLocalString
146                  (getClass().getName() + ".notApplicable",
147                   "There are no http-method elements within the web archive [ {0} ]",
148                   new Object JavaDoc[] {descriptor.getName()}));
149     }
150
151     return result;
152     }
153 }
154
Popular Tags