KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > webservices > WSSchemaLocation


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.webservices;
24
25 import com.sun.enterprise.deployment.WebServiceEndpoint;
26 import com.sun.enterprise.tools.verifier.Result;
27 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 /*
32  * @class.setup_props: ;
33  */

34
35 /*
36  * @testName: check
37  * @assertion_ids:
38  * @test_Strategy:
39  * @class.testArgs: Additional arguments (if any) to be passed when execing the client
40  * @testDescription: Verify that the schemaLocation in webservices.xml matches the schema file
41  * requirement.
42  *
43  * All webservices deployment descriptors must indicate the
44  * webservices schema by using the J2EE namespace:
45  *
46  * http://java.sun.com/xml/ns/j2ee
47  *
48  * and by indicating the version of the schema by using the version
49  * element as shown below:
50  *
51  * <webservices xmlns="http://java.sun.com/xml/ns/j2ee"
52  * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
53  * xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
54  * http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd"
55  * version="1.1">
56  * ...
57  * </webservices>
58  *
59  *
60  * schemaLocation should be:
61  * xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
62  * http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd">
63  *
64  * A Web services deployment descriptor is located in a WAR at WEB-INF/webservices.xml.
65  */

66
67 public class WSSchemaLocation extends WSTest implements WSCheck {
68     String JavaDoc myValue = null;
69     String JavaDoc[] reqSchemaLocation =
70                     {"http://java.sun.com/xml/ns/j2ee http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd",
71                      "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_web_services_1_2.xsd"};
72     int i;
73     /**
74      * @param descriptor the WebServices descriptor
75      * @return <code>Result</code> the results for this assertion
76      */

77     public Result check (WebServiceEndpoint descriptor) {
78
79         Result result = getInitializedResult();
80         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
81         String JavaDoc[] reqSchemaLocationSub1 =
82                     {"http://java.sun.com/xml/ns/j2ee", "http://java.sun.com/xml/ns/javaee"};
83         String JavaDoc[] reqSchemaLocationSub2 =
84                     {"http://www.ibm.com/webservices/xsd/j2ee_web_services_1_1.xsd",
85                      "http://java.sun.com/xml/ns/javaee/javaee_web_services_1_2.xsd"};
86         boolean rslt = false;
87         String JavaDoc schemaVersion = getVerifierContext().getSchemaVersion();
88         Document JavaDoc wsdoc=getVerifierContext().getWebServiceDocument();
89         //with jax-ws it is not mandatory to define webservices.xml deployment descriptor
90
if (wsdoc == null && schemaVersion.compareTo("1.1") > 0) {
91             addGoodDetails(result, compName);
92             result.passed(smh.getLocalString(getClass().getName() + ".passed1",
93                     "Webservices deployment descriptor is not defined for this archive"));
94             return result;
95         }
96
97         try {
98            if (wsdoc.getDocumentElement().hasAttributes()) {
99                getNode(wsdoc);
100                if ( myValue != null) {
101                    for(i=0; i<reqSchemaLocation.length; i++) {
102                        rslt = verifySchema(myValue, reqSchemaLocation[i],
103                                reqSchemaLocationSub1[i], reqSchemaLocationSub2[i]);
104                        if(rslt) break;
105                    }
106                }
107            }
108            if (rslt) {
109               result.addGoodDetails(smh.getLocalString ("tests.componentNameConstructor",
110                                    "For [ {0} ]", new Object JavaDoc[] {compName.toString()}));
111               result.passed(smh.getLocalString (getClass().getName() + ".passed",
112                           "The schemaLocation in the webservices.xml file for [{0}] matches the schema file requirement",
113                            new Object JavaDoc[] {compName.toString()}));
114             }
115             else {
116              result.addErrorDetails(smh.getLocalString ("tests.componentNameConstructor",
117                                    "For [ {0} ]", new Object JavaDoc[] {compName.toString()}));
118              result.failed(smh.getLocalString (getClass().getName() + ".failed",
119                "The schemaLocation in the webservices.xml file for [{0}] does not match the schema file requirement",
120                 new Object JavaDoc[] {compName.toString()}));
121             }
122         }catch (Exception JavaDoc e) {
123             //result.fail
124
result.failed(smh.getLocalString
125                 (getClass().getName() + ".failed",
126                "The schemaLocation in the webservices.xml file for [{0}] does not match the schema file requirement",
127                 new Object JavaDoc[] {compName.toString()}));
128             result.addErrorDetails(smh.getLocalString
129                ("com.sun.enterprise.tools.verifier.tests.webservices.Error",
130                 "Error: Unexpected error occurred [ {0} ]",
131                 new Object JavaDoc[] {e.getMessage()}));
132         }
133         return result;
134     }
135
136     private boolean verifySchema(String JavaDoc nodeval, String JavaDoc reqSchemaLocation,
137                                  String JavaDoc reqSchemaLocationSub1, String JavaDoc reqSchemaLocationSub2){
138       try {
139            int off1 = reqSchemaLocation.indexOf("http", reqSchemaLocationSub1.length());
140            int off2 = nodeval.indexOf("http",reqSchemaLocationSub1.length());
141            // 1. separate into 2 substrings and verify
142
if ( checkSubString(nodeval, 0,0,reqSchemaLocationSub1.length())
143                  && checkSubString(nodeval, off1,off2,reqSchemaLocationSub2.length()) ) {
144                     ; // ok so far
145
}
146            else {
147                 // no need to go further, strings are not correct
148
return false;
149            }
150
151            // 2. make sure that there is at least a space between the 2 substrings
152
if ( reqSchemaLocation.length() > nodeval.length())
153               return false;
154
155           // 3. Make sure that there is only whitespace as separation between the two substrings
156
// java whitespace is one of the following:
157
/*
158           It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or
159           PARAGRAPH_SEPARATOR) but is not also a non-breaking space (' ', '?',
160           '?').
161
162           It is ' ', HORIZONTAL TABULATION.
163           It is '
164 ', LINE FEED.
165           It is ' ', VERTICAL TABULATION.
166           It is ' ', FORM FEED.
167           It is '
168 ', CARRIAGE RETURN.
169           It is '', FILE SEPARATOR.
170           It is '', GROUP SEPARATOR.
171           It is '', RECORD SEPARATOR.
172           It is '', UNIT SEPARATOR.
173
174           */

175
176          for ( int i = reqSchemaLocationSub1.length(); i < off2; i++) {
177             if ( !(Character.isWhitespace(nodeval.charAt(i))) ) {
178               return false;
179             }
180           }
181
182     }catch (Exception JavaDoc e) {
183             e.toString();
184             e.printStackTrace();
185     }
186     return true;
187  }
188     private boolean checkSubString(String JavaDoc str , int off1, int off2, int len) {
189                 if (reqSchemaLocation[i].regionMatches(off1, str, off2, len)) {
190                         return true;
191                 }
192                 return false;
193            }
194
195   public void getNode(Node JavaDoc node) {
196
197     String JavaDoc name = node.getNodeName();
198     int myType = node.getNodeType();
199     // type 1 = Element
200
if ( ( myType == 1 ) & (name.equals("webservices") )) {
201        Element JavaDoc e = (Element JavaDoc)node;
202        myValue = e.getAttribute("xsi:schemaLocation");
203        return;
204     }
205     if (node.hasChildNodes()) {
206       Node JavaDoc firstChild = node.getFirstChild();
207       getNode(firstChild);
208     }
209     Node JavaDoc nextNode = node.getNextSibling();
210     if (nextNode != null) getNode(nextNode);
211     return ;
212   }
213 }
214
215
Popular Tags