Java tutorial
/** * Mule Development Kit * Copyright 2010-2011 (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * This file was automatically generated by the Mule Development Kit */ package org.mule.module.wsdlproc; import java.beans.PropertyDescriptor; import java.io.File; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.inject.Inject; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.Converter; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.ClientImpl; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.endpoint.dynamic.DynamicClientFactory; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.service.model.BindingInfo; import org.apache.cxf.service.model.BindingMessageInfo; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.MessagePartInfo; import org.apache.cxf.service.model.ServiceInfo; import org.mule.api.MuleContext; import org.mule.api.annotations.Module; import org.mule.api.annotations.Configurable; import org.mule.api.annotations.Processor; import org.mule.api.transformer.DataType; import org.mule.api.transformer.Transformer; import org.mule.api.transformer.TransformerException; import org.mule.transformer.types.DataTypeFactory; import org.mule.util.BeanUtils; /** * WSDL Processing module. This module is configured with a WSDL location and a method * and then executes that method on the webservice using the incoming message as arguments, * and then returns the result. * * @author Douglas Davies, MuleSoft, Inc. */ @Module(name = "wsdlproc", schemaVersion = "1.0") public class WSDLProcModule { /** * The location of the WSDL. Must either be a URL or a file path name. */ @Configurable private String wsdlLocation; /** * The name of the operation to be called on the web service. */ @Configurable private String operationName; @Inject private MuleContext muleContext; public MuleContext getMuleContext() { return muleContext; } public void setMuleContext(MuleContext muleContext) { this.muleContext = muleContext; } private Client client; private Class<?> type; private BindingOperationInfo boi; @PostConstruct public void setUp() throws MalformedURLException, InstantiationException { DynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); client = factory.createClient(createUrl());//Shouldn't need a service name type = findInputClass(); } private Class<?> findInputClass() throws InstantiationException { ClientImpl clientImpl = (ClientImpl) client; Endpoint endpoint = clientImpl.getEndpoint(); ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0); Collection<BindingInfo> bindings = serviceInfo.getBindings(); BindingMessageInfo bindingMessageInfo = null; for (BindingInfo b : bindings) { for (BindingOperationInfo o : b.getOperations()) { if (o.getName().getLocalPart().equals(operationName)) { bindingMessageInfo = o.getInput(); boi = o; } } } if (bindingMessageInfo == null) { throw new InstantiationException("Could not find specified operation"); } List<MessagePartInfo> parts = bindingMessageInfo.getMessageParts(); // only one part. MessagePartInfo partInfo = parts.get(0); Class<?> partClass = partInfo.getTypeClass(); System.out.println(boi.isUnwrapped()); return partClass; } private String camelCaseToEnumFormat(String orig) {//WSDL2JAVA converts camelCase to upper-case, underscore separated: camelCase => CAMEL_CASE String[] camelCaseWords = orig.split("(?=[A-Z])"); String ret = camelCaseWords[0]; for (int i = 1; i < camelCaseWords.length; i++) { ret += "_" + camelCaseWords[i]; } return ret.toUpperCase(); } public Object createArgumentObject(Map<String, Object> inputParams) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException { Object inputObject = type.newInstance(); BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {//This is to make BeanUtils handle Enums @Override public Object convert(String value, Class clazz) { if (clazz.isEnum()) { return Enum.valueOf(clazz, camelCaseToEnumFormat(value)); } else { return super.convert(value, clazz); } } }); beanUtilsBean.populate(inputObject, inputParams); //BeanUtils.populateWithoutFail(inputObject, inputParams, false); return inputObject; } public URL createUrl() throws MalformedURLException { URL wsdlURL; File wsdlFile = new File(wsdlLocation); if (wsdlFile.exists()) { wsdlURL = wsdlFile.toURL(); } else { wsdlURL = new URL(wsdlLocation); } return wsdlURL; } /** * Sends a request to the Web Service and returns the result * * {@sample.xml ../../../doc/WSDLProc-connector.xml.sample wsdlproc:send-request} * * @param params The arguments to send with the web service call, must be a parameter map * @return The Object result of the web service call * @throws Exception Some exception */ @Processor public Object sendRequest(Map<String, Object> params) throws Exception { Object input = createArgumentObject(params); //Object[] result = client.invoke(operationName, input); Object[] result = client.invoke(boi, input); return result; } public String getWsdlLocation() { return wsdlLocation; } public void setWsdlLocation(String wsdlLocation) { this.wsdlLocation = wsdlLocation; } public String getOperationName() { return operationName; } public void setOperationName(String operationName) { this.operationName = operationName; } }