1 25 package org.ofbiz.service.engine; 26 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Vector ; 35 36 import javax.xml.namespace.QName ; 37 import javax.xml.rpc.ParameterMode ; 38 import javax.xml.rpc.ServiceException ; 39 40 import org.apache.axis.Message; 41 import org.apache.axis.client.Call; 42 import org.apache.axis.client.Service; 43 import org.apache.axis.encoding.XMLType; 44 import org.apache.axis.message.RPCElement; 45 import org.apache.axis.message.RPCParam; 46 import org.apache.axis.message.SOAPEnvelope; 47 import org.ofbiz.service.GenericServiceException; 48 import org.ofbiz.service.ModelParam; 49 import org.ofbiz.service.ModelService; 50 import org.ofbiz.service.ServiceDispatcher; 51 import org.ofbiz.base.util.Debug; 52 import org.ofbiz.base.util.UtilValidate; 53 54 63 public final class SOAPClientEngine extends GenericAsyncEngine { 64 65 public static final String module = SOAPClientEngine.class.getName(); 66 67 public SOAPClientEngine(ServiceDispatcher dispatcher) { 68 super(dispatcher); 69 } 70 71 74 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 75 runSync(localName, modelService, context); 76 } 77 78 81 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 82 Object result = serviceInvoker(modelService, context); 83 84 if (result == null) 85 throw new GenericServiceException("Service did not return expected result"); 86 if (!(result instanceof Map )) { 87 Map newResult = new HashMap (); 88 89 newResult.put("result", result); 90 return newResult; 91 } 92 return (Map ) result; 93 } 94 95 private Object serviceInvoker(ModelService modelService, Map context) throws GenericServiceException { 97 if (modelService.location == null || modelService.invoke == null) 98 throw new GenericServiceException("Cannot locate service to invoke"); 99 100 Service service = null; 101 Call call = null; 102 103 try { 104 service = new Service(); 105 call = (Call) service.createCall(); 106 } catch (javax.xml.rpc.JAXRPCException e) { 107 throw new GenericServiceException("RPC service error", e); 108 } catch (ServiceException e) { throw new GenericServiceException("RPC service error", e); 110 } 111 112 URL endPoint = null; 113 114 try { 115 endPoint = new URL (this.getLocation(modelService)); 116 } catch (MalformedURLException e) { 117 throw new GenericServiceException("Location not a valid URL", e); 118 } 119 120 List inModelParamList = modelService.getInModelParamList(); 121 Object [] params = new Object [inModelParamList.size()]; 122 123 if (Debug.infoOn()) Debug.logInfo("[SOAPClientEngine.invoke] : Parameter length - " + params.length, module); 124 125 call.setTargetEndpointAddress(endPoint); 126 127 if (UtilValidate.isNotEmpty(modelService.nameSpace)){ 128 call.setOperationName(new QName (modelService.nameSpace, modelService.invoke)); 129 } else { 130 call.setOperationName(modelService.invoke); 131 } 132 133 int i = 0; 134 135 call.setOperation(call.getOperationName().getLocalPart()); 136 Vector vParams = new Vector (); 137 Iterator iter = inModelParamList.iterator(); 138 while (iter.hasNext()) { 139 ModelParam p = (ModelParam) iter.next(); 140 141 if (Debug.infoOn()) Debug.logInfo("[SOAPClientEngine.invoke} : Parameter: " + p.name + " (" + p.mode + ") - " + i, module); 142 143 if(!p.name.trim().equals("userLogin") && !p.name.trim().equals("locale")) { 145 QName qName = call.getParameterTypeByName(p.name); call.addParameter(p.name, qName, getMode(p.mode)); 147 vParams.add(context.get(p.name)); 148 } 149 150 params[i] = context.get(p.name); 152 153 i++; 154 } 155 156 call.setReturnType(XMLType.XSD_ANYTYPE); 157 params=vParams.toArray(); 158 159 Object result = null; 160 161 try { 162 Debug.logInfo("[SOAPClientEngine.invoke] : Sending Call To SOAP Server", module); 163 result = call.invoke(params); 164 } catch (java.rmi.RemoteException e) { 165 throw new GenericServiceException("RPC error", e); 166 } 167 if (Debug.verboseOn()) { 168 Debug.log("SOAP Service Result - " + result, module); 169 } 170 171 return getResponseParams(call.getMessageContext().getResponseMessage()); 172 } 173 174 private Map getResponseParams(Message respMessage) { 175 Map mRet = new Hashtable (); 176 try { 177 SOAPEnvelope resEnv = (SOAPEnvelope) respMessage.getSOAPEnvelope(); 178 List bodies = resEnv.getBodyElements(); 179 Iterator i = bodies.iterator(); 180 while (i.hasNext()) { 181 Object o = i.next(); 182 183 if (o instanceof RPCElement) { 184 RPCElement body = (RPCElement) o; 185 List params = null; 186 params = body.getParams(); 187 188 Iterator p = params.iterator(); 189 while (p.hasNext()) { 190 RPCParam param = (RPCParam) p.next(); 191 mRet.put(param.getName(), param.getValue()); 192 if (Debug.verboseOn()) { 193 Debug.log("SOAP Client Param - " + param.getName() + "=" + param.getValue(), module); 194 } 195 } 196 } 197 } 198 } catch (org.apache.axis.AxisFault e) { 199 Debug.logError(e, "AxisFault", module); 200 } catch (org.xml.sax.SAXException e) { 201 Debug.logError(e, "SAXException", module); 202 } 203 return mRet; 204 } 205 206 private ParameterMode getMode(String sMode) { 207 if (sMode.equals("IN")) { 208 return ParameterMode.IN; 209 } else if (sMode.equals("OUT")) { 210 return ParameterMode.OUT; 211 } else if (sMode.equals("INOUT")) { 212 return ParameterMode.INOUT; 213 } else { 214 return null; 215 } 216 } 217 } 218 | Popular Tags |