001: package org.objectweb.celtix.tools.generators.java2;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.logging.Logger;
006:
007: import javax.jws.soap.SOAPBinding;
008: import javax.wsdl.Binding;
009: import javax.wsdl.BindingFault;
010: import javax.wsdl.BindingInput;
011: import javax.wsdl.BindingOperation;
012: import javax.wsdl.BindingOutput;
013: import javax.wsdl.Definition;
014: import javax.wsdl.WSDLException;
015: import javax.wsdl.extensions.ExtensionRegistry;
016: import javax.wsdl.extensions.soap.SOAPBody;
017: import javax.wsdl.extensions.soap.SOAPHeader;
018: import javax.wsdl.extensions.soap.SOAPOperation;
019: import javax.xml.namespace.QName;
020:
021: import org.objectweb.celtix.common.i18n.Message;
022: import org.objectweb.celtix.common.logging.LogUtils;
023: import org.objectweb.celtix.tools.common.ToolException;
024: import org.objectweb.celtix.tools.common.WSDLConstants;
025: import org.objectweb.celtix.tools.common.model.JavaMethod;
026: import org.objectweb.celtix.tools.common.model.JavaParameter;
027: import org.objectweb.celtix.tools.common.model.WSDLModel;
028: import org.objectweb.celtix.tools.common.model.WSDLParameter;
029: import org.objectweb.celtix.tools.processors.java2.JavaToWSDLProcessor;
030:
031: public class BindingGenerator {
032: private static final Logger LOG = LogUtils
033: .getL7dLogger(JavaToWSDLProcessor.class);
034: private WSDLModel wmodel;
035: private Definition definition;
036: private ExtensionRegistry extensionRegistry;
037:
038: public BindingGenerator(WSDLModel model) {
039: this .wmodel = model;
040: definition = model.getDefinition();
041: extensionRegistry = definition.getExtensionRegistry();
042: }
043:
044: public void generate() {
045: Binding binding = definition.createBinding();
046:
047: binding.setQName(new QName(WSDLConstants.NS_WSDL, wmodel
048: .getPortTypeName()
049: + "Binding"));
050: binding.setPortType(definition.getPortType(new QName(wmodel
051: .getTargetNameSpace(), wmodel.getPortTypeName())));
052:
053: // genearte the soap binding
054:
055: javax.wsdl.extensions.soap.SOAPBinding soapBinding;
056: try {
057: soapBinding = (javax.wsdl.extensions.soap.SOAPBinding) extensionRegistry
058: .createExtension(Binding.class, new QName(
059: WSDLConstants.SOAP11_NAMESPACE, "binding"));
060: soapBinding
061: .setTransportURI("http://schemas.xmlsoap.org/soap/http");
062: soapBinding.setStyle(wmodel.getStyle().toString()
063: .toLowerCase());
064: binding.addExtensibilityElement(soapBinding);
065: } catch (WSDLException e) {
066: // TODO Auto-generated catch block
067: e.printStackTrace();
068: }
069:
070: generateBindingOperation(binding);
071: binding.setUndefined(false);
072: definition.addBinding(binding);
073:
074: }
075:
076: private void generateBindingOperation(Binding binding) {
077: for (JavaMethod jmethod : wmodel.getJavaMethods()) {
078: BindingOperation bindOperation = definition
079: .createBindingOperation();
080: bindOperation.setName(jmethod.getName());
081: generateBindingOperationInputOutPut(bindOperation, jmethod);
082: binding.addBindingOperation(bindOperation);
083: }
084:
085: }
086:
087: private void generateBindingOperationInputOutPut(
088: BindingOperation operation, JavaMethod jmethod) {
089: // generate soap binding action
090: SOAPOperation soapOperation = generateSoapAction();
091: soapOperation.setStyle(jmethod.getSoapStyle().name()
092: .toLowerCase());
093: soapOperation.setSoapActionURI(jmethod.getSoapAction());
094: operation.addExtensibilityElement(soapOperation);
095:
096: generateInputSoapBody(jmethod, operation, jmethod.getRequest());
097:
098: generateOutputSoapBody(jmethod, operation, jmethod
099: .getResponse());
100:
101: for (org.objectweb.celtix.tools.common.model.WSDLException ex : jmethod
102: .getWSDLExceptions()) {
103:
104: BindingFault bindingFault = definition.createBindingFault();
105: bindingFault
106: .setName(ex.getExcpetionClass().getSimpleName());
107: operation.addBindingFault(bindingFault);
108: javax.wsdl.extensions.soap.SOAPFault soapFault = null;
109: try {
110: soapFault = (javax.wsdl.extensions.soap.SOAPFault) extensionRegistry
111: .createExtension(BindingFault.class,
112: new QName(
113: WSDLConstants.SOAP11_NAMESPACE,
114: "fault"));
115: soapFault.setUse("literal");
116: soapFault.setName(ex.getExcpetionClass()
117: .getSimpleName());
118: } catch (WSDLException e) {
119: throw new ToolException(e.getMessage(), e);
120: }
121: bindingFault.addExtensibilityElement(soapFault);
122:
123: }
124:
125: }
126:
127: private SOAPOperation generateSoapAction() {
128: SOAPOperation soapOperation = null;
129: try {
130: soapOperation = (SOAPOperation) extensionRegistry
131: .createExtension(BindingOperation.class,
132: new QName(WSDLConstants.SOAP11_NAMESPACE,
133: "operation"));
134: } catch (WSDLException e) {
135: throw new ToolException(e.getMessage(), e);
136: }
137:
138: return soapOperation;
139: }
140:
141: private void generateOutputSoapBody(JavaMethod jmethod,
142: BindingOperation operation, WSDLParameter param) {
143: if (param == null) {
144: return;
145: }
146:
147: SOAPBody body = null;
148:
149: BindingOutput bindingOutput = definition.createBindingOutput();
150: bindingOutput.setName(param.getName());
151:
152: operation.setBindingOutput(bindingOutput);
153:
154: try {
155: body = (SOAPBody) extensionRegistry.createExtension(
156: BindingOutput.class, new QName(
157: WSDLConstants.SOAP11_NAMESPACE, "body"));
158: } catch (WSDLException e1) {
159: throw new ToolException(e1.getMessage(), e1);
160: }
161:
162: if (jmethod.getSoapUse() == SOAPBinding.Use.LITERAL) {
163: body.setUse("literal");
164: } else {
165: Message msg = new Message("ENCODED_USE_NOT_SUPPORTED", LOG);
166: throw new ToolException(msg);
167: }
168:
169: List<JavaParameter> bodyParams = new ArrayList<JavaParameter>();
170: List<JavaParameter> headerParams = new ArrayList<JavaParameter>();
171:
172: splitSoapHeaderBodyParams(param, bodyParams, headerParams);
173: // if exists soap header,then generate soap body parts
174:
175: if (headerParams.size() > 0) {
176: List<String> parts = new ArrayList<String>();
177: for (JavaParameter parameter : bodyParams) {
178: parts.add(parameter.getPartName());
179: }
180: body.setParts(parts);
181: SOAPHeader soapHeader = null;
182: for (JavaParameter jp : headerParams) {
183:
184: try {
185: soapHeader = (SOAPHeader) extensionRegistry
186: .createExtension(
187: BindingOutput.class,
188: new QName(
189: WSDLConstants.SOAP11_NAMESPACE,
190: "header"));
191: soapHeader.setMessage(new QName(param
192: .getTargetNamespace(), param.getName()));
193: soapHeader.setPart(jp.getPartName());
194: soapHeader.setUse("literal");
195:
196: } catch (WSDLException e) {
197: throw new ToolException(e.getMessage(), e);
198: }
199: }
200:
201: if (jmethod.getSoapStyle() == SOAPBinding.Style.RPC) {
202: body.setNamespaceURI(param.getTargetNamespace());
203: }
204: bindingOutput.addExtensibilityElement(soapHeader);
205:
206: }
207: bindingOutput.addExtensibilityElement(body);
208:
209: }
210:
211: private void generateInputSoapBody(JavaMethod jmethod,
212: BindingOperation operation, WSDLParameter param) {
213: if (param == null) {
214: return;
215: }
216: SOAPBody body = null;
217:
218: BindingInput bindingInput = definition.createBindingInput();
219: bindingInput.setName(param.getName());
220:
221: operation.setBindingInput(bindingInput);
222:
223: try {
224: body = (SOAPBody) extensionRegistry.createExtension(
225: BindingInput.class, new QName(
226: WSDLConstants.SOAP11_NAMESPACE, "body"));
227: } catch (WSDLException e1) {
228: throw new ToolException(e1.getMessage(), e1);
229: }
230:
231: if (jmethod.getSoapUse() == SOAPBinding.Use.LITERAL) {
232: body.setUse("literal");
233: } else {
234: Message msg = new Message("ENCODED_USE_NOT_SUPPORTED", LOG);
235: throw new ToolException(msg);
236: }
237:
238: List<JavaParameter> bodyParams = new ArrayList<JavaParameter>();
239: List<JavaParameter> headerParams = new ArrayList<JavaParameter>();
240:
241: splitSoapHeaderBodyParams(param, bodyParams, headerParams);
242:
243: // if exists soap header,then generate soap body parts
244:
245: if (headerParams.size() > 0) {
246: List<String> parts = new ArrayList<String>();
247: for (JavaParameter parameter : bodyParams) {
248: parts.add(parameter.getPartName());
249: }
250: body.setParts(parts);
251: SOAPHeader soapHeader = null;
252: for (JavaParameter jp : headerParams) {
253:
254: try {
255: soapHeader = (SOAPHeader) extensionRegistry
256: .createExtension(
257: BindingInput.class,
258: new QName(
259: WSDLConstants.SOAP11_NAMESPACE,
260: "header"));
261:
262: soapHeader.setMessage(new QName(param
263: .getTargetNamespace(), param.getName()));
264: soapHeader.setPart(jp.getPartName());
265: soapHeader.setUse("literal");
266:
267: } catch (WSDLException e) {
268: throw new ToolException(e.getMessage(), e);
269: }
270: }
271:
272: if (jmethod.getSoapStyle() == SOAPBinding.Style.RPC) {
273: body.setNamespaceURI(param.getTargetNamespace());
274: }
275: bindingInput.addExtensibilityElement(soapHeader);
276:
277: }
278: bindingInput.addExtensibilityElement(body);
279:
280: }
281:
282: private void splitSoapHeaderBodyParams(WSDLParameter param,
283: List<JavaParameter> bodyList, List<JavaParameter> headerList) {
284: for (JavaParameter jpara : param.getChildren()) {
285: if (jpara.isHeader()) {
286: headerList.add(jpara);
287: } else {
288: bodyList.add(jpara);
289: }
290: }
291:
292: }
293:
294: }
|