KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > webservices > LightWeightMappingValidator


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.webservices;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Map JavaDoc;
21 import javax.wsdl.*;
22 import javax.wsdl.extensions.soap.SOAPBinding;
23 import javax.wsdl.extensions.soap.SOAPBody;
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.geronimo.validator.ValidationContext;
27 import org.apache.geronimo.validator.ValidationFailure;
28
29 public class LightWeightMappingValidator extends WSDLVisitor {
30
31     private ArrayList JavaDoc operationNames;
32     private ValidationContext context;
33
34     private static final QName JavaDoc XSD_STRING = new QName JavaDoc("http://www.w3.org/2001/XMLSchema", "string");
35
36     public LightWeightMappingValidator(Definition definition) {
37         super(definition);
38     }
39
40     public ValidationContext validate() {
41         if (context == null) {
42             context = new ValidationContext(definition.getQName().toString());
43             walkTree();
44         }
45         return context;
46     }
47
48     public boolean isValid() {
49         ValidationContext context = validate();
50         return !context.hasFailures() && !context.hasErrors();
51     }
52
53     protected void begin() {
54         operationNames = new ArrayList JavaDoc();
55     }
56
57     protected void visit(Definition definition) {
58         if (definition.getServices().values().size() != 1) {
59             context.addFailure(new ValidationFailure("A lightweight RPC/Encoded service must contain only one Service"));
60         }
61     }
62
63     protected void visit(Output output) {
64         Map JavaDoc outputParts = output.getMessage().getParts();
65         if (outputParts.size() != 0 && outputParts.size() != 1) {
66             context.addFailure(new ValidationFailure("The output message must contain zero or one parts: " + output.getName()));
67         }
68
69     }
70
71     protected void visit(Operation operation) {
72         if (!operationNames.add(operation.getName())) {
73             context.addFailure(new ValidationFailure("No two operations can have the same name: " + operation.getName()));
74         }
75     }
76
77     protected void visit(Fault fault) {
78         Part message = fault.getMessage().getPart("message");
79         if (message == null) {
80             context.addFailure(new ValidationFailure("The fault message must contain one part named 'message' : " + fault.getName()));
81         } else if (!XSD_STRING.equals(message.getTypeName())) {
82             context.addFailure(new ValidationFailure("The fault message must contain one part of type 'xsd:string' : " + fault.getName()));
83         }
84     }
85
86
87     protected void visit(BindingInput bindingInput) {
88         SOAPBody body = getSOAPBody(bindingInput.getExtensibilityElements());
89         String JavaDoc encoding = body.getUse();
90         if (encoding == null || !encoding.equals("encoded")) {
91             context.addFailure(new ValidationFailure("The use attribute of the binding input operation must be 'encoded': " + bindingInput.getName()));
92         }
93     }
94
95     protected void visit(BindingOutput bindingOutput) {
96         SOAPBody body = getSOAPBody(bindingOutput.getExtensibilityElements());
97         String JavaDoc encoding = body.getUse();
98         if (encoding == null || !encoding.equals("encoded")) {
99             context.addFailure(new ValidationFailure("The use attribute of the binding output operation must be 'encoded': " + bindingOutput.getName()));
100         }
101     }
102
103     protected void visit(BindingFault bindingFault) {
104         SOAPBody body = getSOAPBody(bindingFault.getExtensibilityElements());
105         String JavaDoc encoding = body.getUse();
106         if (encoding == null || !encoding.equals("encoded")) {
107             context.addFailure(new ValidationFailure("The use attribute of the binding fault operation must be 'encoded': " + bindingFault.getName()));
108         }
109     }
110
111     protected void visit(Binding binding) {
112         SOAPBinding soapBinding = getSOAPBinding(binding);
113         if (soapBinding == null || soapBinding.getStyle() == null || !soapBinding.getStyle().equals("rpc")) {
114             context.addFailure(new ValidationFailure("The messaging style of the binding must be rpc: " + binding.getQName()));
115         }
116     }
117
118     protected void visit(Service service) {
119         if (service.getPorts().values().size() != 1) {
120             context.addFailure(new ValidationFailure("A lightweight RPC/Encoded service must contain only one Port"));
121         }
122     }
123 }
124
Popular Tags