1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.cxf.transport; |
18 |
|
|
19 |
|
import java.io.ByteArrayInputStream; |
20 |
|
import java.io.ByteArrayOutputStream; |
21 |
|
import java.io.IOException; |
22 |
|
import java.io.InputStream; |
23 |
|
import java.io.OutputStream; |
24 |
|
import java.util.logging.Level; |
25 |
|
import java.util.logging.Logger; |
26 |
|
|
27 |
|
import org.apache.camel.CamelContext; |
28 |
|
import org.apache.camel.Processor; |
29 |
|
import org.apache.cxf.Bus; |
30 |
|
import org.apache.cxf.common.logging.LogUtils; |
31 |
|
import org.apache.cxf.configuration.Configurable; |
32 |
|
import org.apache.cxf.configuration.Configurer; |
33 |
|
import org.apache.cxf.io.CachedOutputStream; |
34 |
|
import org.apache.cxf.message.Message; |
35 |
|
import org.apache.cxf.message.MessageImpl; |
36 |
|
import org.apache.cxf.service.model.EndpointInfo; |
37 |
|
import org.apache.cxf.transport.AbstractConduit; |
38 |
|
import org.apache.cxf.transport.Conduit; |
39 |
|
import org.apache.cxf.transport.Destination; |
40 |
|
import org.apache.cxf.transport.MessageObserver; |
41 |
|
import org.apache.cxf.ws.addressing.AttributedURIType; |
42 |
|
import org.apache.cxf.ws.addressing.EndpointReferenceType; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
0 |
public class CamelConduit extends AbstractConduit implements Configurable { |
48 |
|
protected static final String BASE_BEAN_NAME_SUFFIX = ".camel-conduit-base"; |
49 |
0 |
private static final Logger LOG = LogUtils.getL7dLogger(CamelConduit.class); |
50 |
|
private final CamelTransportBase base; |
51 |
|
private String targetCamelEndpointUri; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
public CamelConduit(CamelContext camelContext, Bus bus, EndpointInfo endpointInfo, EndpointReferenceType targetReference) { |
60 |
0 |
super(targetReference); |
61 |
0 |
AttributedURIType address = targetReference.getAddress(); |
62 |
0 |
if (address != null) { |
63 |
0 |
this.targetCamelEndpointUri = address.getValue(); |
64 |
|
} |
65 |
|
|
66 |
0 |
base = new CamelTransportBase(camelContext, bus, endpointInfo, false, BASE_BEAN_NAME_SUFFIX); |
67 |
|
|
68 |
0 |
initConfig(); |
69 |
0 |
} |
70 |
|
|
71 |
|
|
72 |
|
public void prepare(Message message) throws IOException { |
73 |
0 |
getLogger().log(Level.FINE, "CamelConduit send message"); |
74 |
|
|
75 |
0 |
message.setContent(OutputStream.class, new CamelOutputStream(message)); |
76 |
0 |
} |
77 |
|
|
78 |
|
public void close() { |
79 |
0 |
getLogger().log(Level.FINE, "CamelConduit closed "); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
0 |
base.close(); |
84 |
0 |
} |
85 |
|
|
86 |
|
protected Logger getLogger() { |
87 |
0 |
return LOG; |
88 |
|
} |
89 |
|
|
90 |
|
public String getBeanName() { |
91 |
0 |
EndpointInfo info = base.endpointInfo; |
92 |
0 |
if (info == null) { |
93 |
0 |
return "default.camel-conduit"; |
94 |
|
} |
95 |
0 |
return info.getName() + ".camel-conduit"; |
96 |
|
} |
97 |
|
|
98 |
|
private void initConfig() { |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
0 |
Configurer configurer = base.bus.getExtension(Configurer.class); |
112 |
0 |
if (null != configurer) { |
113 |
0 |
configurer.configureBean(this); |
114 |
|
} |
115 |
0 |
} |
116 |
|
|
117 |
0 |
private class CamelOutputStream extends CachedOutputStream { |
118 |
|
private Message outMessage; |
119 |
|
private boolean isOneWay; |
120 |
|
|
121 |
0 |
public CamelOutputStream(Message m) { |
122 |
0 |
outMessage = m; |
123 |
0 |
} |
124 |
|
|
125 |
|
protected void doFlush() throws IOException { |
126 |
|
|
127 |
0 |
} |
128 |
|
|
129 |
|
protected void doClose() throws IOException { |
130 |
0 |
isOneWay = outMessage.getExchange().isOneWay(); |
131 |
0 |
commitOutputMessage(); |
132 |
0 |
if (!isOneWay) { |
133 |
0 |
handleResponse(); |
134 |
|
} |
135 |
0 |
} |
136 |
|
|
137 |
|
protected void onWrite() throws IOException { |
138 |
|
|
139 |
0 |
} |
140 |
|
|
141 |
|
private void commitOutputMessage() { |
142 |
0 |
base.template.send(targetCamelEndpointUri, new Processor() { |
143 |
0 |
public void process(org.apache.camel.Exchange reply) { |
144 |
0 |
Object request = null; |
145 |
0 |
if (isTextPayload()) { |
146 |
0 |
request = currentStream.toString(); |
147 |
0 |
} else { |
148 |
0 |
request = ((ByteArrayOutputStream)currentStream).toByteArray(); |
149 |
|
} |
150 |
|
|
151 |
0 |
getLogger().log(Level.FINE, "Conduit Request is :[" + request + "]"); |
152 |
0 |
String replyTo = base.getReplyDestination(); |
153 |
|
|
154 |
0 |
base.marshal(request, replyTo, reply); |
155 |
0 |
base.setMessageProperties(outMessage, reply); |
156 |
|
|
157 |
0 |
String correlationID = null; |
158 |
0 |
if (!isOneWay) { |
159 |
|
|
160 |
0 |
String id = null; |
161 |
|
|
162 |
0 |
if (id != null) { |
163 |
0 |
if (correlationID != null) { |
164 |
0 |
String error = "User cannot set CamelCorrelationID when " + "making a request/reply invocation using " + "a static replyTo Queue."; |
165 |
|
} |
166 |
0 |
correlationID = id; |
167 |
|
} |
168 |
|
} |
169 |
|
|
170 |
0 |
if (correlationID != null) { |
171 |
0 |
reply.getIn().setHeader(CamelConstants.CAMEL_CORRELATION_ID, correlationID); |
172 |
|
} else { |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
} |
178 |
|
|
179 |
0 |
getLogger().log(Level.FINE, "template sending request: ", reply.getIn()); |
180 |
0 |
} |
181 |
|
}); |
182 |
0 |
} |
183 |
|
|
184 |
|
private void handleResponse() throws IOException { |
185 |
|
|
186 |
0 |
Object response = null; |
187 |
|
|
188 |
|
|
189 |
0 |
Message inMessage = new MessageImpl(); |
190 |
0 |
outMessage.getExchange().setInMessage(inMessage); |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
0 |
getLogger().log(Level.FINE, "The Response Message is : [" + response + "]"); |
216 |
|
|
217 |
|
|
218 |
0 |
byte[] bytes = null; |
219 |
0 |
if (response instanceof String) { |
220 |
0 |
String requestString = (String)response; |
221 |
0 |
bytes = requestString.getBytes(); |
222 |
0 |
} else { |
223 |
0 |
bytes = (byte[])response; |
224 |
|
} |
225 |
0 |
inMessage.setContent(InputStream.class, new ByteArrayInputStream(bytes)); |
226 |
0 |
getLogger().log(Level.FINE, "incoming observer is " + incomingObserver); |
227 |
0 |
incomingObserver.onMessage(inMessage); |
228 |
0 |
} |
229 |
|
} |
230 |
|
|
231 |
|
private boolean isTextPayload() { |
232 |
|
|
233 |
0 |
return true; |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
protected class DecoupledDestination implements Destination { |
240 |
|
protected MessageObserver decoupledMessageObserver; |
241 |
|
private EndpointReferenceType address; |
242 |
|
|
243 |
0 |
DecoupledDestination(EndpointReferenceType ref, MessageObserver incomingObserver) { |
244 |
0 |
address = ref; |
245 |
0 |
decoupledMessageObserver = incomingObserver; |
246 |
0 |
} |
247 |
|
|
248 |
|
public EndpointReferenceType getAddress() { |
249 |
0 |
return address; |
250 |
|
} |
251 |
|
|
252 |
|
public Conduit getBackChannel(Message inMessage, Message partialResponse, EndpointReferenceType addr) throws IOException { |
253 |
|
|
254 |
0 |
return null; |
255 |
|
} |
256 |
|
|
257 |
|
public void shutdown() { |
258 |
|
|
259 |
0 |
} |
260 |
|
|
261 |
|
public synchronized void setMessageObserver(MessageObserver observer) { |
262 |
0 |
decoupledMessageObserver = observer; |
263 |
0 |
} |
264 |
|
|
265 |
|
public synchronized MessageObserver getMessageObserver() { |
266 |
0 |
return decoupledMessageObserver; |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
} |