001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * Modified by Nabh Information Systems, Inc. for Stringbeans Web Services
019: * Framework.
020: *
021: * Modifications (c) 2005 Nabh Information Systems, Inc.
022: */
023:
024: package com.nabhinc.ws.soap;
025:
026: import org.apache.axis.AxisFault;
027: import org.apache.axis.MessageContext;
028: import org.apache.axis.handlers.soap.SOAPService;
029: import org.apache.axis.description.OperationDesc;
030: import org.apache.axis.description.ServiceDesc;
031: import org.apache.axis.i18n.Messages;
032: import org.apache.axis.message.SOAPBodyElement;
033: import org.apache.axis.message.SOAPEnvelope;
034: import org.apache.axis.message.MessageElement;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037:
038: import com.nabhinc.ws.server.InterceptorChain;
039: import com.nabhinc.ws.server.RequestInfo;
040:
041: import javax.xml.namespace.QName;
042:
043: import java.lang.reflect.Method;
044: import java.util.Vector;
045:
046: /**
047: * Deal with message-style Java services. For now, these are services
048: * with exactly ONE OperationDesc, pointing to a method which looks like
049: * one of the following:
050: *
051: * public Element [] method(Vector v);
052: * (NOTE : This is silly, we should change it to either be Vector/Vector
053: * or Element[]/Element[])
054: *
055: * public Document method(Document doc);
056: *
057: * public void method(MessageContext mc);
058: *
059: * @author Doug Davis (dug@us.ibm.com)
060: * @author Glen Daniels (gdaniels@apache.org)
061: */
062: public class MsgProvider {
063: /**
064: * Process the message. Figure out the method "style" (one of the three
065: * allowed signatures, which has already been determined and cached in
066: * the OperationDesc) and do the actual invocation. Note that we don't
067: * catch exceptions here, preferring to bubble them right up through to
068: * someone who'll catch it above us.
069: *
070: * @param msgContext the active MessageContext
071: * @param reqEnv the request SOAPEnvelope
072: * @param resEnv the response SOAPEnvelope (we should fill this in)
073: * @param obj the service target object
074: * @throws Exception
075: */
076: public void processMessage(MessageContext msgContext,
077: SOAPEnvelope reqEnv, SOAPEnvelope resEnv,
078: RequestInfo reqInfo, InterceptorChain chain)
079: throws Throwable {
080: OperationDesc operation = msgContext.getOperation();
081: SOAPService service = msgContext.getService();
082: ServiceDesc serviceDesc = service.getServiceDescription();
083: QName opQName = null;
084:
085: if (operation == null) {
086: Vector bodyElements = reqEnv.getBodyElements();
087: if (bodyElements.size() > 0) {
088: MessageElement element = (MessageElement) bodyElements
089: .get(0);
090: if (element != null) {
091: opQName = new QName(element.getNamespaceURI(),
092: element.getLocalName());
093: operation = serviceDesc
094: .getOperationByElementQName(opQName);
095: }
096: }
097: }
098:
099: if (operation == null) {
100: throw new AxisFault(Messages.getMessage(
101: "noOperationForQName", opQName == null ? "null"
102: : opQName.toString()));
103: }
104:
105: Method method = operation.getMethod();
106: reqInfo.method = method;
107: reqInfo.methodName = method.getName();
108:
109: int methodType = operation.getMessageOperationStyle();
110:
111: if (methodType != OperationDesc.MSG_METHOD_SOAPENVELOPE) {
112: // dig out just the body, and pass it on
113: Vector bodies = reqEnv.getBodyElements();
114: Object argObjects[] = new Object[1];
115:
116: switch (methodType) {
117: // SOAPBodyElement [] / SOAPBodyElement []
118: case OperationDesc.MSG_METHOD_BODYARRAY:
119: SOAPBodyElement[] bodyElements = new SOAPBodyElement[bodies
120: .size()];
121: bodies.toArray(bodyElements);
122: argObjects[0] = bodyElements;
123: reqInfo.arguments = argObjects;
124: boolean redirect = invokeMethod(reqInfo, chain);
125: if (redirect)
126: return;
127: SOAPBodyElement[] bodyResult = (SOAPBodyElement[]) reqInfo.result;
128: if (bodyResult != null) {
129: for (int i = 0; i < bodyResult.length; i++) {
130: SOAPBodyElement bodyElement = bodyResult[i];
131: resEnv.addBodyElement(bodyElement);
132: }
133: }
134: return;
135:
136: // Element [] / Element []
137: case OperationDesc.MSG_METHOD_ELEMENTARRAY:
138: Element[] elements = new Element[bodies.size()];
139: for (int i = 0; i < elements.length; i++) {
140: SOAPBodyElement body = (SOAPBodyElement) bodies
141: .get(i);
142: elements[i] = body.getAsDOM();
143: }
144: argObjects[0] = elements;
145: reqInfo.arguments = argObjects;
146: redirect = invokeMethod(reqInfo, chain);
147: if (redirect)
148: return;
149: Element[] elemResult = (Element[]) reqInfo.result;
150: if (elemResult != null) {
151: for (int i = 0; i < elemResult.length; i++) {
152: if (elemResult[i] != null)
153: resEnv.addBodyElement(new SOAPBodyElement(
154: elemResult[i]));
155: }
156: }
157: return;
158:
159: // Element [] / Element []
160: case OperationDesc.MSG_METHOD_DOCUMENT:
161: Document doc = ((SOAPBodyElement) bodies.get(0))
162: .getAsDocument();
163: argObjects[0] = doc;
164: reqInfo.arguments = argObjects;
165: redirect = invokeMethod(reqInfo, chain);
166: if (redirect)
167: return;
168: Document resultDoc = (Document) reqInfo.result;
169: if (resultDoc != null) {
170: resEnv.addBodyElement(new SOAPBodyElement(resultDoc
171: .getDocumentElement()));
172: }
173: return;
174: }
175: } else {
176: Object argObjects[] = new Object[2];
177:
178: // SOAPEnvelope / SOAPEnvelope
179: argObjects[0] = reqEnv;
180: argObjects[1] = resEnv;
181: invokeMethod(reqInfo, chain);
182: return;
183: }
184:
185: // SHOULD NEVER GET HERE...
186: throw new AxisFault(Messages.getMessage("badMsgMethodStyle"));
187: }
188:
189: private boolean invokeMethod(RequestInfo reqInfo,
190: InterceptorChain chain) throws Throwable {
191: // Invoke interceptor chain. At the end of interceptor chain,
192: // WebServiceInterceptorChain object will invoke the actual
193: // service method.
194: /*
195: InterceptorInfo[] ints =
196: InterceptorManager.getInterceptorChain(reqInfo.serviceInfo.name, reqInfo.methodName);
197: InterceptorChain chain = new InterceptorChain(ints);
198: */
199: chain.doIntercept(reqInfo);
200: if (reqInfo.redirect != null)
201: return true;
202: if (reqInfo.error != null)
203: throw reqInfo.error;
204: return false;
205: }
206: }
|