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.ofbiz.service.engine;
019:
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Vector;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.rpc.ParameterMode;
031: import javax.xml.rpc.ServiceException;
032:
033: import org.apache.axis.Message;
034: import org.apache.axis.client.Call;
035: import org.apache.axis.client.Service;
036: import org.apache.axis.encoding.XMLType;
037: import org.apache.axis.message.RPCElement;
038: import org.apache.axis.message.RPCParam;
039: import org.apache.axis.message.SOAPEnvelope;
040: import org.ofbiz.service.GenericServiceException;
041: import org.ofbiz.service.ModelParam;
042: import org.ofbiz.service.ModelService;
043: import org.ofbiz.service.ServiceDispatcher;
044: import org.ofbiz.base.util.Debug;
045: import org.ofbiz.base.util.UtilValidate;
046:
047: /**
048: * Generic Service SOAP Interface
049: */
050: public final class SOAPClientEngine extends GenericAsyncEngine {
051:
052: public static final String module = SOAPClientEngine.class
053: .getName();
054:
055: public SOAPClientEngine(ServiceDispatcher dispatcher) {
056: super (dispatcher);
057: }
058:
059: /**
060: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
061: */
062: public void runSyncIgnore(String localName,
063: ModelService modelService, Map context)
064: throws GenericServiceException {
065: runSync(localName, modelService, context);
066: }
067:
068: /**
069: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
070: */
071: public Map runSync(String localName, ModelService modelService,
072: Map context) throws GenericServiceException {
073: Object result = serviceInvoker(modelService, context);
074:
075: if (result == null)
076: throw new GenericServiceException(
077: "Service did not return expected result");
078: if (!(result instanceof Map)) {
079: Map newResult = new HashMap();
080:
081: newResult.put("result", result);
082: return newResult;
083: }
084: return (Map) result;
085: }
086:
087: // Invoke the remote SOAP service
088: private Object serviceInvoker(ModelService modelService, Map context)
089: throws GenericServiceException {
090: if (modelService.location == null
091: || modelService.invoke == null)
092: throw new GenericServiceException(
093: "Cannot locate service to invoke");
094:
095: Service service = null;
096: Call call = null;
097:
098: try {
099: service = new Service();
100: call = (Call) service.createCall();
101: } catch (javax.xml.rpc.JAXRPCException e) {
102: throw new GenericServiceException("RPC service error", e);
103: } catch (ServiceException e) {//Add by Andy.Chen 2003.01.15
104: throw new GenericServiceException("RPC service error", e);
105: }
106:
107: URL endPoint = null;
108:
109: try {
110: endPoint = new URL(this .getLocation(modelService));
111: } catch (MalformedURLException e) {
112: throw new GenericServiceException(
113: "Location not a valid URL", e);
114: }
115:
116: List inModelParamList = modelService.getInModelParamList();
117: Object[] params = new Object[inModelParamList.size()];
118:
119: if (Debug.infoOn())
120: Debug.logInfo(
121: "[SOAPClientEngine.invoke] : Parameter length - "
122: + params.length, module);
123:
124: call.setTargetEndpointAddress(endPoint);
125:
126: if (UtilValidate.isNotEmpty(modelService.nameSpace)) {
127: call.setOperationName(new QName(modelService.nameSpace,
128: 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())
142: Debug.logInfo("[SOAPClientEngine.invoke} : Parameter: "
143: + p.name + " (" + p.mode + ") - " + i, module);
144:
145: //Exclude params that ModelServiceReader insert into
146: if (!p.name.trim().equals("userLogin")
147: && !p.name.trim().equals("locale")) {
148: QName qName = call.getParameterTypeByName(p.name); //.getTypeMapping().getTypeQName((Class) ObjectType.classNameClassMap.get(p.type));
149: call.addParameter(p.name, qName, getMode(p.mode));
150: vParams.add(context.get(p.name));
151: }
152:
153: // if the value is null, that's fine, it will go in null...
154: params[i] = context.get(p.name);
155:
156: i++;
157: }
158:
159: call.setReturnType(XMLType.XSD_ANYTYPE);
160: params = vParams.toArray();
161:
162: Object result = null;
163:
164: try {
165: Debug
166: .logInfo(
167: "[SOAPClientEngine.invoke] : Sending Call To SOAP Server",
168: module);
169: result = call.invoke(params);
170: } catch (java.rmi.RemoteException e) {
171: throw new GenericServiceException("RPC error", e);
172: }
173: if (Debug.verboseOn()) {
174: Debug.log("SOAP Service Result - " + result, module);
175: }
176:
177: return getResponseParams(call.getMessageContext()
178: .getResponseMessage());
179: }
180:
181: private Map getResponseParams(Message respMessage) {
182: Map mRet = new Hashtable();
183: try {
184: SOAPEnvelope resEnv = respMessage.getSOAPEnvelope();
185: List bodies = resEnv.getBodyElements();
186: Iterator i = bodies.iterator();
187: while (i.hasNext()) {
188: Object o = i.next();
189:
190: if (o instanceof RPCElement) {
191: RPCElement body = (RPCElement) o;
192: List params = null;
193: params = body.getParams();
194:
195: Iterator p = params.iterator();
196: while (p.hasNext()) {
197: RPCParam param = (RPCParam) p.next();
198: mRet.put(param.getName(), param.getValue());
199: if (Debug.verboseOn()) {
200: Debug.log("SOAP Client Param - "
201: + param.getName() + "="
202: + param.getValue(), module);
203: }
204: }
205: }
206: }
207: } catch (org.apache.axis.AxisFault e) {
208: Debug.logError(e, "AxisFault", module);
209: } catch (org.xml.sax.SAXException e) {
210: Debug.logError(e, "SAXException", module);
211: }
212: return mRet;
213: }
214:
215: private ParameterMode getMode(String sMode) {
216: if (sMode.equals("IN")) {
217: return ParameterMode.IN;
218: } else if (sMode.equals("OUT")) {
219: return ParameterMode.OUT;
220: } else if (sMode.equals("INOUT")) {
221: return ParameterMode.INOUT;
222: } else {
223: return null;
224: }
225: }
226: }
|