001: /*
002: * $Id: SOAPClientEngine.java,v 1.2 2004/03/13 04:55:22 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.engine;
026:
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.util.HashMap;
030: import java.util.Hashtable;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.Vector;
035:
036: import javax.xml.namespace.QName;
037: import javax.xml.rpc.ParameterMode;
038: import javax.xml.rpc.ServiceException;
039:
040: import org.apache.axis.Message;
041: import org.apache.axis.client.Call;
042: import org.apache.axis.client.Service;
043: import org.apache.axis.encoding.XMLType;
044: import org.apache.axis.message.RPCElement;
045: import org.apache.axis.message.RPCParam;
046: import org.apache.axis.message.SOAPEnvelope;
047: import org.ofbiz.service.GenericServiceException;
048: import org.ofbiz.service.ModelParam;
049: import org.ofbiz.service.ModelService;
050: import org.ofbiz.service.ServiceDispatcher;
051: import org.ofbiz.base.util.Debug;
052:
053: /**
054: * Generic Service SOAP Interface
055: *
056: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
057: * @author <a href="mailto:">Andy Chen</a>
058: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
059: * @version $Revision: 1.2 $
060: * @since 2.0
061: */
062: public final class SOAPClientEngine extends GenericAsyncEngine {
063:
064: public static final String module = SOAPClientEngine.class
065: .getName();
066:
067: public SOAPClientEngine(ServiceDispatcher dispatcher) {
068: super (dispatcher);
069: }
070:
071: /**
072: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.
073: * String, org.ofbiz.service.ModelService, java.util.Map)
074: */
075: public void runSyncIgnore(String localName,
076: ModelService modelService, Map context)
077: throws GenericServiceException {
078: Map result = runSync(localName, modelService, context);
079: }
080:
081: /**
082: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String,
083: * org.ofbiz.service.ModelService, java.util.Map)
084: */
085: public Map runSync(String localName, ModelService modelService,
086: Map context) throws GenericServiceException {
087: Object result = serviceInvoker(localName, modelService, context);
088:
089: if (result == null)
090: throw new GenericServiceException(
091: "Service did not return expected result");
092: if (!(result instanceof Map)) {
093: Map newResult = new HashMap();
094:
095: newResult.put("result", result);
096: return newResult;
097: }
098: return (Map) result;
099: }
100:
101: // Invoke the remote SOAP service
102: private Object serviceInvoker(String localName,
103: ModelService modelService, Map context)
104: throws GenericServiceException {
105: if (modelService.location == null
106: || modelService.invoke == null)
107: throw new GenericServiceException(
108: "Cannot locate service to invoke");
109:
110: Service service = null;
111: Call call = null;
112:
113: try {
114: service = new Service();
115: call = (Call) service.createCall();
116: } catch (javax.xml.rpc.JAXRPCException e) {
117: throw new GenericServiceException("RPC service error", e);
118: } catch (ServiceException e) {//Add by Andy.Chen 2003.01.15
119: throw new GenericServiceException("RPC service error", e);
120: }
121:
122: URL endPoint = null;
123:
124: try {
125: endPoint = new URL(modelService.location);
126: } catch (MalformedURLException e) {
127: throw new GenericServiceException(
128: "Location not a valid URL", e);
129: }
130:
131: List inModelParamList = modelService.getInModelParamList();
132: Object[] params = new Object[inModelParamList.size()];
133:
134: if (Debug.infoOn())
135: Debug.logInfo(
136: "[SOAPClientEngine.invoke] : Parameter length - "
137: + params.length, module);
138:
139: call.setTargetEndpointAddress(endPoint);
140:
141: if (!"".equals(modelService.nameSpace)) {
142: call.setOperationName(new QName(modelService.nameSpace,
143: modelService.invoke));
144: } else {
145: call.setOperationName(modelService.invoke);
146: }
147:
148: int i = 0;
149:
150: call.setOperation(call.getOperationName().getLocalPart());
151: Vector vParams = new Vector();
152: Iterator iter = inModelParamList.iterator();
153: while (iter.hasNext()) {
154: ModelParam p = (ModelParam) iter.next();
155:
156: if (Debug.infoOn())
157: Debug.logInfo("[SOAPClientEngine.invoke} : Parameter: "
158: + p.name + " (" + p.mode + ") - " + i, module);
159:
160: //Exclude params that ModelServiceReader insert into
161: if (!p.name.trim().equals("userLogin")
162: && !p.name.trim().equals("locale")) {
163: QName qName = call.getParameterTypeByName(p.name); //.getTypeMapping().getTypeQName((Class) ObjectType.classNameClassMap.get(p.type));
164: call.addParameter(p.name, qName, getMode(p.mode));
165: vParams.add(context.get(p.name));
166: }
167:
168: // if the value is null, that's fine, it will go in null...
169: params[i] = context.get(p.name);
170:
171: i++;
172: }
173:
174: call.setReturnType(XMLType.XSD_ANYTYPE);
175: params = vParams.toArray();
176:
177: Object result = null;
178:
179: try {
180: Debug
181: .logInfo(
182: "[SOAPClientEngine.invoke] : Sending Call To SOAP Server",
183: module);
184: result = call.invoke(params);
185: } catch (java.rmi.RemoteException e) {
186: throw new GenericServiceException("RPC error", e);
187: }
188:
189: return getResponseParams(call.getMessageContext()
190: .getResponseMessage());
191: }
192:
193: private Map getResponseParams(Message respMessage) {
194: Map mRet = new Hashtable();
195: try {
196: SOAPEnvelope resEnv = (SOAPEnvelope) respMessage
197: .getSOAPEnvelope();
198: List bodies = resEnv.getBodyElements();
199: Iterator i = bodies.iterator();
200: while (i.hasNext()) {
201: Object o = i.next();
202:
203: if (o instanceof RPCElement) {
204: RPCElement body = (RPCElement) o;
205: String serviceName = body.getMethodName();
206: List params = null;
207:
208: params = body.getParams();
209:
210: Map serviceContext = new HashMap();
211: Iterator p = params.iterator();
212:
213: while (p.hasNext()) {
214: RPCParam param = (RPCParam) p.next();
215: mRet.put(param.getName(), param.getValue());
216: System.out.println(param.getName() + "="
217: + param.getValue());
218: }
219: }
220: }
221: } catch (org.apache.axis.AxisFault e) {
222: System.out.println("AxisFault:" + e.toString());
223: } catch (org.xml.sax.SAXException e) {
224: System.out.println("SAXException:" + e.toString());
225: }
226: return mRet;
227: }
228:
229: private ParameterMode getMode(String sMode) {
230: if (sMode.equals("IN")) {
231: return ParameterMode.IN;
232: } else if (sMode.equals("OUT")) {
233: return ParameterMode.OUT;
234: } else if (sMode.equals("INOUT")) {
235: return ParameterMode.INOUT;
236: } else {
237: return null;
238: }
239: }
240: }
|