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.tools.wsdlto.frontend.jaxws.wsdl11;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.logging.Logger;
028:
029: import javax.jws.soap.SOAPBinding;
030: import javax.wsdl.Binding;
031: import javax.wsdl.BindingOperation;
032: import javax.wsdl.Definition;
033: import javax.wsdl.Fault;
034: import javax.wsdl.Operation;
035: import javax.wsdl.PortType;
036: import javax.wsdl.Service;
037: import javax.wsdl.extensions.ExtensionRegistry;
038: import javax.wsdl.xml.WSDLReader;
039:
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042:
043: import org.xml.sax.InputSource;
044:
045: import org.apache.cxf.common.i18n.Message;
046: import org.apache.cxf.common.logging.LogUtils;
047: import org.apache.cxf.helpers.DOMUtils;
048: import org.apache.cxf.helpers.FileUtils;
049: import org.apache.cxf.tools.common.ToolConstants;
050: import org.apache.cxf.tools.common.ToolException;
051: import org.apache.cxf.tools.util.SOAPBindingUtil;
052: import org.apache.cxf.tools.validator.internal.WSDL11Validator;
053: import org.apache.cxf.tools.wsdlto.core.AbstractWSDLBuilder;
054: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.CustomizationParser;
055: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBinding;
056: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBindingDeserializer;
057: import org.apache.cxf.tools.wsdlto.frontend.jaxws.customization.JAXWSBindingSerializer;
058: import org.apache.cxf.wsdl11.WSDLDefinitionBuilder;
059:
060: public class JAXWSDefinitionBuilder extends
061: AbstractWSDLBuilder<Definition> {
062:
063: protected static final Logger LOG = LogUtils
064: .getL7dLogger(JAXWSDefinitionBuilder.class);
065: protected CustomizationParser cusParser;
066:
067: private WSDLDefinitionBuilder builder;
068: private WSDLReader wsdlReader;
069: private Definition wsdlDefinition;
070:
071: private List<InputSource> jaxbBindings;
072: private Element handlerChain;
073:
074: public JAXWSDefinitionBuilder() {
075: builder = new WSDLDefinitionBuilder();
076: ExtensionRegistry registry = builder.getExtenstionRegistry();
077: registerJaxwsExtension(registry);
078: wsdlReader = builder.getWSDLReader();
079: wsdlReader.setExtensionRegistry(registry);
080: }
081:
082: public Definition build() {
083: String wsdlURL = (String) context
084: .get(ToolConstants.CFG_WSDLURL);
085: return build(wsdlURL);
086: }
087:
088: public Definition build(String wsdlURL) {
089: this .builder.setBus(this .bus);
090: wsdlDefinition = builder.build(wsdlURL);
091: context.put(ToolConstants.IMPORTED_DEFINITION, builder
092: .getImportedDefinitions());
093: checkSupported(wsdlDefinition);
094: return wsdlDefinition;
095: }
096:
097: private void registerJaxwsExtension(ExtensionRegistry registry) {
098: registerJAXWSBinding(registry, Definition.class);
099: registerJAXWSBinding(registry, Service.class);
100: registerJAXWSBinding(registry, Fault.class);
101: registerJAXWSBinding(registry, PortType.class);
102: registerJAXWSBinding(registry, Operation.class);
103: registerJAXWSBinding(registry, Binding.class);
104: registerJAXWSBinding(registry, BindingOperation.class);
105: }
106:
107: private void registerJAXWSBinding(ExtensionRegistry registry,
108: Class clz) {
109: registry.registerSerializer(clz, ToolConstants.JAXWS_BINDINGS,
110: new JAXWSBindingSerializer());
111: registry.registerDeserializer(clz,
112: ToolConstants.JAXWS_BINDINGS,
113: new JAXWSBindingDeserializer());
114: registry.mapExtensionTypes(clz, ToolConstants.JAXWS_BINDINGS,
115: JAXWSBinding.class);
116: }
117:
118: public void customize() {
119: if (!context.containsKey(ToolConstants.CFG_BINDING)) {
120: return;
121: }
122: cusParser = new CustomizationParser();
123: cusParser.parse(context);
124:
125: jaxbBindings = cusParser.getJaxbBindings();
126: handlerChain = cusParser.getHandlerChains();
127:
128: context.setJaxbBindingFiles(jaxbBindings);
129: context.put(ToolConstants.HANDLER_CHAIN, handlerChain);
130: try {
131: this .wsdlDefinition = buildCustomizedDefinition();
132: } catch (Exception e) {
133: Message msg = new Message("FAIL_TO_CREATE_WSDL_DEFINITION",
134: LOG, cusParser.getCustomizedWSDLElement()
135: .getBaseURI());
136: throw new RuntimeException(msg.toString(), e);
137: }
138: }
139:
140: private void checkSupported(Definition def) throws ToolException {
141: if (isRPCEncoded(def)) {
142: org.apache.cxf.common.i18n.Message msg = new org.apache.cxf.common.i18n.Message(
143: "UNSUPPORTED_RPC_ENCODED", LOG);
144: throw new ToolException(msg);
145: }
146: }
147:
148: private boolean isRPCEncoded(Definition def) {
149: Iterator ite1 = def.getBindings().values().iterator();
150: while (ite1.hasNext()) {
151: Binding binding = (Binding) ite1.next();
152: String bindingStyle = SOAPBindingUtil
153: .getBindingStyle(binding);
154:
155: Iterator ite2 = binding.getBindingOperations().iterator();
156: while (ite2.hasNext()) {
157: BindingOperation bop = (BindingOperation) ite2.next();
158: String bopStyle = SOAPBindingUtil
159: .getSOAPOperationStyle(bop);
160:
161: String outputUse = "";
162: if (SOAPBindingUtil.getBindingOutputSOAPBody(bop) != null) {
163: outputUse = SOAPBindingUtil
164: .getBindingOutputSOAPBody(bop).getUse();
165: }
166: String inputUse = "";
167: if (SOAPBindingUtil.getBindingInputSOAPBody(bop) != null) {
168: inputUse = SOAPBindingUtil.getBindingInputSOAPBody(
169: bop).getUse();
170: }
171: if ((SOAPBinding.Style.RPC.name().equalsIgnoreCase(
172: bindingStyle) || SOAPBinding.Style.RPC.name()
173: .equalsIgnoreCase(bopStyle))
174: && (SOAPBinding.Use.ENCODED.name()
175: .equalsIgnoreCase(inputUse) || SOAPBinding.Use.ENCODED
176: .name().equalsIgnoreCase(outputUse))) {
177: return true;
178: }
179: }
180:
181: }
182: return false;
183: }
184:
185: private CustomizationParser getCustomizationParer() {
186: return cusParser;
187: }
188:
189: private Definition buildCustomizedDefinition() throws Exception {
190: File tmpFile = File.createTempFile("customzied", ".wsdl");
191: OutputStream outs = new FileOutputStream(tmpFile);
192: DOMUtils.writeXml(getCustomizationParer()
193: .getCustomizedWSDLElement(), outs);
194: InputStream ins = new FileInputStream(new File(tmpFile.toURI()));
195: Document wsdlDoc = DOMUtils.readXml(ins);
196: Definition def = wsdlReader.readWSDL(this .wsdlDefinition
197: .getDocumentBaseURI(), wsdlDoc);
198: FileUtils.delete(tmpFile);
199: return def;
200: }
201:
202: public Definition getWSDLModel() {
203: if (this .wsdlDefinition == null) {
204: this .build();
205: this .customize();
206: }
207:
208: return this .wsdlDefinition;
209: }
210:
211: public WSDLReader getWSDLReader() {
212: return wsdlReader;
213: }
214:
215: public boolean validate(Definition def) throws ToolException {
216: return new WSDL11Validator(def, context).isValid();
217: }
218: }
|