1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.soap.axis.extensions; |
12 | |
|
13 | |
import org.mule.RequestContext; |
14 | |
import org.mule.api.service.Service; |
15 | |
import org.mule.transport.soap.axis.AxisConnector; |
16 | |
import org.mule.transport.soap.axis.AxisMessageReceiver; |
17 | |
import org.mule.transport.soap.axis.AxisServiceProxy; |
18 | |
|
19 | |
import java.lang.reflect.Proxy; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Iterator; |
22 | |
|
23 | |
import javax.xml.namespace.QName; |
24 | |
import javax.xml.rpc.holders.Holder; |
25 | |
|
26 | |
import org.apache.axis.AxisFault; |
27 | |
import org.apache.axis.Constants; |
28 | |
import org.apache.axis.MessageContext; |
29 | |
import org.apache.axis.constants.Style; |
30 | |
import org.apache.axis.description.OperationDesc; |
31 | |
import org.apache.axis.description.ParameterDesc; |
32 | |
import org.apache.axis.description.ServiceDesc; |
33 | |
import org.apache.axis.handlers.soap.SOAPService; |
34 | |
import org.apache.axis.message.RPCElement; |
35 | |
import org.apache.axis.message.RPCHeaderParam; |
36 | |
import org.apache.axis.message.RPCParam; |
37 | |
import org.apache.axis.message.SOAPEnvelope; |
38 | |
import org.apache.axis.providers.java.RPCProvider; |
39 | |
import org.apache.axis.soap.SOAPConstants; |
40 | |
import org.apache.axis.utils.JavaUtils; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class MuleRPCProvider extends RPCProvider |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
private static final long serialVersionUID = -4987111047650933518L; |
52 | |
|
53 | |
private AxisConnector connector; |
54 | |
|
55 | |
public MuleRPCProvider(AxisConnector connector) |
56 | 0 | { |
57 | 0 | this.connector = connector; |
58 | 0 | } |
59 | |
|
60 | |
protected Object makeNewServiceObject(MessageContext messageContext, String s) throws Exception |
61 | |
{ |
62 | 0 | String transUrl = (String)messageContext.getProperty("transport.url"); |
63 | 0 | int i = transUrl.indexOf("?"); |
64 | 0 | if (i > -1) |
65 | |
{ |
66 | 0 | transUrl = transUrl.substring(0, i); |
67 | |
} |
68 | 0 | AxisMessageReceiver receiver = (AxisMessageReceiver)connector.lookupReceiver(transUrl); |
69 | 0 | if (receiver == null) |
70 | |
{ |
71 | 0 | receiver = (AxisMessageReceiver)connector.lookupReceiver(messageContext.getTargetService()); |
72 | |
} |
73 | 0 | if (receiver == null) |
74 | |
{ |
75 | 0 | throw new AxisFault("Could not find Mule registered service: " + s); |
76 | |
} |
77 | |
|
78 | 0 | if (!(receiver.getFlowConstruct() instanceof Service)) |
79 | |
{ |
80 | 0 | throw new IllegalArgumentException( |
81 | |
"Only the Service flow constuct is supported by the axis transport"); |
82 | |
} |
83 | 0 | Service service = (Service) receiver.getFlowConstruct(); |
84 | 0 | Class[] classes = AxisServiceProxy.getInterfacesForComponent(service); |
85 | 0 | return AxisServiceProxy.createProxy(receiver, true, classes); |
86 | |
} |
87 | |
|
88 | |
protected Class getServiceClass(String s, SOAPService soapService, MessageContext messageContext) |
89 | |
throws AxisFault |
90 | |
{ |
91 | 0 | Service component = connector.getMuleContext().getRegistry().lookupService(soapService.getName()); |
92 | |
try |
93 | |
{ |
94 | 0 | Class[] classes = AxisServiceProxy.getInterfacesForComponent(component); |
95 | 0 | return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classes); |
96 | |
} |
97 | 0 | catch (Exception e) |
98 | |
{ |
99 | 0 | throw new AxisFault("Failed to implementation class for component: " + e.getMessage(), e); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
public void invoke(MessageContext msgContext) throws AxisFault |
104 | |
{ |
105 | 0 | super.invoke(msgContext); |
106 | 0 | if (RequestContext.getExceptionPayload() != null) |
107 | |
{ |
108 | 0 | Throwable t = RequestContext.getExceptionPayload().getException(); |
109 | 0 | if (t instanceof Exception) |
110 | |
{ |
111 | 0 | AxisFault fault = AxisFault.makeFault((Exception)t); |
112 | 0 | if (t instanceof RuntimeException) |
113 | |
{ |
114 | 0 | fault.addFaultDetail(Constants.QNAME_FAULTDETAIL_RUNTIMEEXCEPTION, "true"); |
115 | |
} |
116 | 0 | throw fault; |
117 | |
} |
118 | |
else |
119 | |
{ |
120 | 0 | throw (Error)t; |
121 | |
} |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
protected RPCElement createResponseBody(RPCElement body, |
126 | |
MessageContext msgContext, |
127 | |
OperationDesc operation, |
128 | |
ServiceDesc serviceDesc, |
129 | |
Object objRes, |
130 | |
SOAPEnvelope resEnv, |
131 | |
ArrayList outs) throws Exception |
132 | |
{ |
133 | 0 | String methodName = operation.getName(); |
134 | |
|
135 | |
|
136 | 0 | RPCElement resBody = new RPCElement(methodName + "Response"); |
137 | 0 | resBody.setPrefix(body.getPrefix()); |
138 | 0 | resBody.setNamespaceURI(body.getNamespaceURI()); |
139 | 0 | resBody.setEncodingStyle(msgContext.getEncodingStyle()); |
140 | |
try |
141 | |
{ |
142 | |
|
143 | 0 | if (operation.getMethod().getReturnType() != Void.TYPE) |
144 | |
{ |
145 | 0 | QName returnQName = operation.getReturnQName(); |
146 | 0 | if (returnQName == null) |
147 | |
{ |
148 | 0 | String nsp = body.getNamespaceURI(); |
149 | 0 | if (nsp == null || nsp.length() == 0) |
150 | |
{ |
151 | 0 | nsp = serviceDesc.getDefaultNamespace(); |
152 | |
} |
153 | 0 | returnQName = new QName(msgContext.isEncoded() ? "" : nsp, methodName + "Return"); |
154 | |
} |
155 | |
|
156 | 0 | RPCParam param = new RPCParam(returnQName, objRes); |
157 | 0 | param.setParamDesc(operation.getReturnParamDesc()); |
158 | |
|
159 | 0 | if (!operation.isReturnHeader()) |
160 | |
{ |
161 | |
|
162 | 0 | if (msgContext.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS |
163 | |
&& (serviceDesc.getStyle().equals(Style.RPC))) |
164 | |
{ |
165 | 0 | RPCParam resultParam = new RPCParam(Constants.QNAME_RPC_RESULT, returnQName); |
166 | 0 | resultParam.setXSITypeGeneration(Boolean.FALSE); |
167 | 0 | resBody.addParam(resultParam); |
168 | |
} |
169 | 0 | resBody.addParam(param); |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | 0 | resEnv.addHeader(new RPCHeaderParam(param)); |
174 | |
} |
175 | |
|
176 | |
} |
177 | |
|
178 | |
|
179 | 0 | if (!outs.isEmpty()) |
180 | |
{ |
181 | 0 | for (Iterator i = outs.iterator(); i.hasNext();) |
182 | |
{ |
183 | |
|
184 | 0 | RPCParam param = (RPCParam)i.next(); |
185 | 0 | Holder holder = (Holder)param.getObjectValue(); |
186 | 0 | Object value = JavaUtils.getHolderValue(holder); |
187 | 0 | ParameterDesc paramDesc = param.getParamDesc(); |
188 | |
|
189 | 0 | param.setObjectValue(value); |
190 | 0 | if (paramDesc != null && paramDesc.isOutHeader()) |
191 | |
{ |
192 | 0 | resEnv.addHeader(new RPCHeaderParam(param)); |
193 | |
} |
194 | |
else |
195 | |
{ |
196 | 0 | resBody.addParam(param); |
197 | |
} |
198 | 0 | } |
199 | |
} |
200 | |
} |
201 | 0 | catch (Exception e) |
202 | |
{ |
203 | 0 | throw e; |
204 | 0 | } |
205 | 0 | return resBody; |
206 | |
} |
207 | |
} |