1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.cxf.transport; |
19 |
|
|
20 |
|
import org.apache.camel.CamelContext; |
21 |
|
import org.apache.camel.Endpoint; |
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.Processor; |
24 |
|
import org.apache.camel.Producer; |
25 |
|
import org.apache.cxf.Bus; |
26 |
|
import org.apache.cxf.common.logging.LogUtils; |
27 |
|
import org.apache.cxf.configuration.Configurable; |
28 |
|
import org.apache.cxf.io.AbstractCachedOutputStream; |
29 |
|
import org.apache.cxf.message.Message; |
30 |
|
import org.apache.cxf.message.MessageImpl; |
31 |
|
import org.apache.cxf.service.model.EndpointInfo; |
32 |
|
import org.apache.cxf.transport.AbstractConduit; |
33 |
|
import org.apache.cxf.transport.AbstractDestination; |
34 |
|
import org.apache.cxf.transport.Conduit; |
35 |
|
import org.apache.cxf.transport.ConduitInitiator; |
36 |
|
import org.apache.cxf.transport.MessageObserver; |
37 |
|
import org.apache.cxf.ws.addressing.EndpointReferenceType; |
38 |
|
import org.apache.cxf.wsdl.EndpointReferenceUtils; |
39 |
|
|
40 |
|
import java.io.ByteArrayInputStream; |
41 |
|
import java.io.IOException; |
42 |
|
import java.io.InputStream; |
43 |
|
import java.io.OutputStream; |
44 |
|
import java.util.logging.Level; |
45 |
|
import java.util.logging.Logger; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
0 |
public class CamelDestination extends AbstractDestination implements Configurable { |
51 |
|
protected static final String BASE_BEAN_NAME_SUFFIX = ".camel-destination-base"; |
52 |
0 |
private static final Logger LOG = LogUtils.getL7dLogger(CamelDestination.class); |
53 |
|
CamelContext camelContext; |
54 |
|
String camelUri; |
55 |
|
final ConduitInitiator conduitInitiator; |
56 |
|
private CamelTransportBase base; |
57 |
|
private Endpoint endpoint; |
58 |
|
|
59 |
|
public CamelDestination(CamelContext camelContext, Bus bus, |
60 |
|
ConduitInitiator ci, |
61 |
|
EndpointInfo info) throws IOException { |
62 |
0 |
super(getTargetReference(info, bus), info); |
63 |
0 |
this.camelContext = camelContext; |
64 |
|
|
65 |
0 |
base = new CamelTransportBase(camelContext, bus, endpointInfo, true, BASE_BEAN_NAME_SUFFIX); |
66 |
|
|
67 |
0 |
conduitInitiator = ci; |
68 |
|
|
69 |
0 |
initConfig(); |
70 |
0 |
} |
71 |
|
|
72 |
|
protected Logger getLogger() { |
73 |
0 |
return LOG; |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
protected Conduit getInbuiltBackChannel(Message inMessage) { |
81 |
0 |
return new BackChannelConduit(EndpointReferenceUtils.getAnonymousEndpointReference(), |
82 |
|
inMessage); |
83 |
|
} |
84 |
|
|
85 |
|
public void activate() { |
86 |
0 |
getLogger().log(Level.INFO, "CamelDestination activate().... "); |
87 |
|
|
88 |
|
try { |
89 |
0 |
getLogger().log(Level.FINE, "establishing Camel connection"); |
90 |
0 |
endpoint = camelContext.getEndpoint(camelUri); |
91 |
|
} |
92 |
0 |
catch (Exception ex) { |
93 |
0 |
getLogger().log(Level.SEVERE, "Camel connect failed with EException : ", ex); |
94 |
0 |
} |
95 |
0 |
} |
96 |
|
|
97 |
|
public void deactivate() { |
98 |
0 |
base.close(); |
99 |
0 |
} |
100 |
|
|
101 |
|
public void shutdown() { |
102 |
0 |
getLogger().log(Level.FINE, "CamelDestination shutdown()"); |
103 |
0 |
this.deactivate(); |
104 |
0 |
} |
105 |
|
|
106 |
|
protected void incoming(Exchange exchange) { |
107 |
0 |
getLogger().log(Level.FINE, "server received request: ", exchange); |
108 |
|
|
109 |
0 |
byte[] bytes = base.unmarshal(exchange); |
110 |
|
|
111 |
|
|
112 |
0 |
MessageImpl inMessage = new MessageImpl(); |
113 |
0 |
inMessage.setContent(InputStream.class, new ByteArrayInputStream(bytes)); |
114 |
0 |
base.populateIncomingContext(exchange, inMessage, CamelConstants.CAMEL_SERVER_REQUEST_HEADERS); |
115 |
|
|
116 |
0 |
inMessage.put(CamelConstants.CAMEL_REQUEST_MESSAGE, exchange); |
117 |
|
|
118 |
0 |
inMessage.setDestination(this); |
119 |
|
|
120 |
|
|
121 |
0 |
incomingObserver.onMessage(inMessage); |
122 |
0 |
} |
123 |
|
|
124 |
|
public String getBeanName() { |
125 |
0 |
return endpointInfo.getName().toString() + ".camel-destination"; |
126 |
|
} |
127 |
|
|
128 |
|
private void initConfig() { |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
0 |
} |
137 |
|
|
138 |
0 |
protected class ConsumerProcessor implements Processor { |
139 |
|
public void process(Exchange exchange) { |
140 |
|
try { |
141 |
0 |
incoming(exchange); |
142 |
|
} |
143 |
0 |
catch (Throwable ex) { |
144 |
0 |
getLogger().log(Level.WARNING, "Failed to process incoming message : ", ex); |
145 |
0 |
} |
146 |
0 |
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
|
protected class BackChannelConduit extends AbstractConduit { |
151 |
|
protected Message inMessage; |
152 |
|
|
153 |
0 |
BackChannelConduit(EndpointReferenceType ref, Message message) { |
154 |
0 |
super(ref); |
155 |
0 |
inMessage = message; |
156 |
0 |
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
public void setMessageObserver(MessageObserver observer) { |
164 |
|
|
165 |
0 |
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
public void prepare(Message message) throws IOException { |
174 |
|
|
175 |
0 |
message.put(CamelConstants.CAMEL_REQUEST_MESSAGE, |
176 |
|
inMessage.get(CamelConstants.CAMEL_REQUEST_MESSAGE)); |
177 |
0 |
message.setContent(OutputStream.class, |
178 |
|
new CamelOutputStream(inMessage)); |
179 |
0 |
} |
180 |
|
|
181 |
|
protected Logger getLogger() { |
182 |
0 |
return LOG; |
183 |
|
} |
184 |
|
|
185 |
|
} |
186 |
|
|
187 |
0 |
private class CamelOutputStream extends AbstractCachedOutputStream { |
188 |
|
private Message inMessage; |
189 |
|
private Producer<Exchange> replyTo; |
190 |
|
private Producer<Exchange> sender; |
191 |
|
|
192 |
|
|
193 |
0 |
public CamelOutputStream(Message m) { |
194 |
0 |
super(); |
195 |
0 |
inMessage = m; |
196 |
0 |
} |
197 |
|
|
198 |
|
|
199 |
|
private void commitOutputMessage() throws IOException { |
200 |
|
|
201 |
|
|
202 |
0 |
final String replyToUri = getReplyToDestination(inMessage); |
203 |
|
|
204 |
0 |
base.template.send(replyToUri, new Processor() { |
205 |
0 |
public void process(Exchange reply) { |
206 |
0 |
base.marshal(currentStream.toString(), replyToUri, reply); |
207 |
|
|
208 |
0 |
setReplyCorrelationID(inMessage, reply); |
209 |
|
|
210 |
0 |
base.setMessageProperties(inMessage, reply); |
211 |
|
|
212 |
0 |
getLogger().log(Level.FINE, "just server sending reply: ", reply); |
213 |
0 |
} |
214 |
|
}); |
215 |
0 |
} |
216 |
|
|
217 |
|
@Override |
218 |
|
protected void doFlush() throws IOException { |
219 |
|
|
220 |
0 |
} |
221 |
|
|
222 |
|
@Override |
223 |
|
protected void doClose() throws IOException { |
224 |
0 |
commitOutputMessage(); |
225 |
0 |
} |
226 |
|
|
227 |
|
@Override |
228 |
|
protected void onWrite() throws IOException { |
229 |
|
|
230 |
0 |
} |
231 |
|
} |
232 |
|
|
233 |
|
protected String getReplyToDestination(Message inMessage) { |
234 |
0 |
if (inMessage.get(CamelConstants.CAMEL_REBASED_REPLY_TO) != null) { |
235 |
0 |
return (String) inMessage.get(CamelConstants.CAMEL_REBASED_REPLY_TO); |
236 |
|
} |
237 |
|
else { |
238 |
0 |
return base.getReplyDestination(); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
protected void setReplyCorrelationID(Message inMessage, Exchange reply) { |
243 |
0 |
Object value = inMessage.get(CamelConstants.CAMEL_CORRELATION_ID); |
244 |
0 |
if (value != null) { |
245 |
0 |
reply.getIn().setHeader(CamelConstants.CAMEL_CORRELATION_ID, value); |
246 |
|
} |
247 |
0 |
} |
248 |
|
} |