001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.helpers;
019:
020: import java.io.File;
021: import java.lang.annotation.Annotation;
022: import java.lang.reflect.Method;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.jws.WebParam;
030: import javax.wsdl.Binding;
031: import javax.wsdl.BindingOperation;
032: import javax.wsdl.Definition;
033: import javax.wsdl.Input;
034: import javax.wsdl.Message;
035: import javax.wsdl.Operation;
036: import javax.wsdl.Output;
037: import javax.wsdl.Part;
038: import javax.wsdl.PortType;
039: import javax.wsdl.factory.WSDLFactory;
040: import javax.wsdl.xml.WSDLReader;
041: import javax.xml.ws.RequestWrapper;
042:
043: public class WSDLHelper {
044:
045: public BindingOperation getBindingOperation(Definition def,
046: String operationName) {
047: if (operationName == null) {
048: return null;
049: }
050: Iterator ite = def.getBindings().values().iterator();
051: while (ite.hasNext()) {
052: Binding binding = (Binding) ite.next();
053: Iterator ite1 = binding.getBindingOperations().iterator();
054: while (ite1.hasNext()) {
055: BindingOperation bop = (BindingOperation) ite1.next();
056: if (bop.getName().equals(operationName)) {
057: return bop;
058: }
059: }
060: }
061: return null;
062: }
063:
064: public BindingOperation getBindingOperation(Binding binding,
065: String operationName) {
066: if (operationName == null) {
067: return null;
068: }
069: List bindingOperations = binding.getBindingOperations();
070: for (Iterator iter = bindingOperations.iterator(); iter
071: .hasNext();) {
072: BindingOperation bindingOperation = (BindingOperation) iter
073: .next();
074: if (operationName.equals(bindingOperation.getName())) {
075: return bindingOperation;
076: }
077: }
078: return null;
079: }
080:
081: public Map getParts(Operation operation, boolean out) {
082: Message message = null;
083: if (out) {
084: Output output = operation.getOutput();
085: message = output.getMessage();
086: } else {
087: Input input = operation.getInput();
088: message = input.getMessage();
089: }
090: return message.getParts() == null ? new HashMap() : message
091: .getParts();
092: }
093:
094: public javax.jws.soap.SOAPBinding getBindingAnnotationFromClass(
095: List<Class<?>> classList) {
096: javax.jws.soap.SOAPBinding sb = null;
097: for (Class<?> c : classList) {
098: sb = c.getAnnotation(javax.jws.soap.SOAPBinding.class);
099: if (null != sb) {
100: break;
101: }
102: }
103: return sb;
104: }
105:
106: public javax.jws.soap.SOAPBinding getBindingAnnotationFromMethod(
107: Method m) {
108: javax.jws.soap.SOAPBinding sb = null;
109: if (null != m) {
110: sb = m.getAnnotation(javax.jws.soap.SOAPBinding.class);
111: }
112: return sb;
113: }
114:
115: public WebParam getWebParamAnnotation(Annotation[] pa) {
116: WebParam wp = null;
117:
118: if (null != pa) {
119: for (Annotation annotation : pa) {
120: if (WebParam.class.equals(annotation.annotationType())) {
121: wp = (WebParam) annotation;
122: break;
123: }
124: }
125: }
126: return wp;
127: }
128:
129: public RequestWrapper getRequestWrapperAnnotation(Method m) {
130: RequestWrapper rw = null;
131:
132: if (null != m) {
133: rw = m.getAnnotation(RequestWrapper.class);
134: }
135: return rw;
136: }
137:
138: public List<PortType> getPortTypes(Definition def) {
139: List<PortType> portTypes = new ArrayList<PortType>();
140: Iterator ite = def.getPortTypes().values().iterator();
141: while (ite.hasNext()) {
142: PortType portType = (PortType) ite.next();
143: portTypes.add(portType);
144: }
145: return portTypes;
146: }
147:
148: public List<Part> getInMessageParts(Operation operation) {
149: Input input = operation.getInput();
150: List<Part> partsList = new ArrayList<Part>();
151: if (input != null) {
152: Iterator ite = input.getMessage().getParts().values()
153: .iterator();
154: while (ite.hasNext()) {
155: partsList.add((Part) ite.next());
156: }
157: }
158: return partsList;
159: }
160:
161: public List<Part> getOutMessageParts(Operation operation) {
162: Output output = operation.getOutput();
163: List<Part> partsList = new ArrayList<Part>();
164: if (output != null) {
165: Iterator ite = output.getMessage().getParts().values()
166: .iterator();
167: while (ite.hasNext()) {
168: partsList.add((Part) ite.next());
169: }
170: }
171: return partsList;
172: }
173:
174: public Binding getBinding(BindingOperation bop, Definition def) {
175: Iterator ite = def.getBindings().values().iterator();
176: while (ite.hasNext()) {
177: Binding binding = (Binding) ite.next();
178: for (Iterator ite2 = binding.getBindingOperations()
179: .iterator(); ite2.hasNext();) {
180: BindingOperation bindingOperation = (BindingOperation) ite2
181: .next();
182: if (bindingOperation.getName().equals(bop)) {
183: return binding;
184: }
185: }
186: }
187: return null;
188: }
189:
190: public Definition getDefinition(File wsdlFile) throws Exception {
191: WSDLFactory wsdlFactory = WSDLFactory.newInstance();
192: WSDLReader reader = wsdlFactory.newWSDLReader();
193: reader.setFeature("javax.wsdl.verbose", false);
194: return reader.readWSDL(wsdlFile.toURI().toURL().toString());
195: }
196: }
|