/
JMSSender.java
768 lines (679 loc) · 34.3 KB
/
JMSSender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.axis2.transport.jms;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.TransportOutDescription;
import org.apache.axis2.transport.MessageFormatter;
import org.apache.axis2.transport.OutTransportInfo;
import org.apache.axis2.transport.base.AbstractTransportSender;
import org.apache.axis2.transport.base.BaseConstants;
import org.apache.axis2.transport.base.BaseUtils;
import org.apache.axis2.transport.base.ManagementSupport;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream;
import org.apache.axis2.util.MessageProcessorSelector;
import org.apache.commons.io.output.WriterOutputStream;
import org.wso2.securevault.SecretResolver;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
/**
* The TransportSender for JMS
*/
public class JMSSender extends AbstractTransportSender implements ManagementSupport {
public static final String TRANSPORT_NAME = Constants.TRANSPORT_JMS;
private static Map<Transaction, ArrayList<JMSMessageSender>> jmsMessageSenderMap = new HashMap<>();
/** The JMS connection factory manager to be used when sending messages out */
private JMSConnectionFactoryManager connFacManager;
/**
* Initialize the transport sender by reading pre-defined connection factories for
* outgoing messages.
*
* @param cfgCtx the configuration context
* @param transportOut the transport sender definition from axis2.xml
* @throws AxisFault on error
*/
@Override
public void init(ConfigurationContext cfgCtx, TransportOutDescription transportOut) throws AxisFault {
super.init(cfgCtx, transportOut);
SecretResolver secretResolver = cfgCtx.getAxisConfiguration().getSecretResolver();
connFacManager = new JMSConnectionFactoryManager(transportOut, secretResolver);
log.info("JMS Transport Sender initialized...");
}
@Override
public void stop() {
// clean up any shared JMS resources in this sender's connection factories
connFacManager.stop();
super.stop();
}
/**
* Get corresponding JMS connection factory defined within the transport sender for the
* transport-out information - usually constructed from a targetEPR
*
* @param trpInfo the transport-out information
* @return the corresponding JMS connection factory, if any
*/
private JMSConnectionFactory getJMSConnectionFactory(JMSOutTransportInfo trpInfo) {
Map<String,String> props = trpInfo.getProperties();
if (trpInfo.getProperties() != null) {
String jmsConnectionFactoryName = props.get(JMSConstants.PARAM_JMS_CONFAC);
log.debug("Using Connection factory:" + jmsConnectionFactoryName);
if (jmsConnectionFactoryName != null) {
log.debug(JMSConstants.PARAM_JMS_CONFAC + " is used");
return connFacManager.getJMSConnectionFactory(jmsConnectionFactoryName);
} else {
JMSConnectionFactory fac = connFacManager.getJMSConnectionFactory(props);
if (fac == null) {
fac = connFacManager.getJMSConnectionFactory(JMSConstants.DEFAULT_CONFAC_NAME);
log.debug("Connection factory has been initialized");
} else {
log.debug("Connection factory has been re-used");
}
return fac;
}
} else {
return null;
}
}
/**
* Performs the actual sending of the JMS message
*/
@Override
public void sendMessage(MessageContext msgCtx, String targetAddress,
OutTransportInfo outTransportInfo) throws AxisFault {
JMSConnectionFactory jmsConnectionFactory = null;
JMSOutTransportInfo jmsOut = null;
JMSMessageSender messageSender = null;
if (targetAddress != null) {
jmsOut = new JMSOutTransportInfo(targetAddress);
// do we have a definition for a connection factory to use for this address?
jmsConnectionFactory = getJMSConnectionFactory(jmsOut);
if (jmsConnectionFactory == null) {
jmsConnectionFactory = connFacManager.getConnectionFactoryFromTargetEndpoint(targetAddress);
}
// MessageSender only needed to add if the scenario involved with XA transaction.
// It can be identified by looking at target url's transport.jms.TransactionCommand parameter.
if ((targetAddress.contains(JMSConstants.JMS_TRANSACTION_COMMAND))) {
try {
messageSender = jmsOut.createJMSSender(msgCtx);
Transaction transaction = (Transaction) msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION);
if (jmsMessageSenderMap.get(transaction) == null) {
ArrayList list = new ArrayList();
list.add(messageSender);
jmsMessageSenderMap.put(transaction, list);
} else {
ArrayList list = jmsMessageSenderMap.get(transaction);
list.add(messageSender);
jmsMessageSenderMap.put(transaction, list);
}
} catch (JMSException e) {
rollbackIfXATransaction(msgCtx);
handleException("Unable to create a JMSMessageSender for : " + outTransportInfo, e);
}
} else {
messageSender = new JMSMessageSender(jmsConnectionFactory, targetAddress);
}
} else if (outTransportInfo instanceof JMSOutTransportInfo) {
jmsOut = (JMSOutTransportInfo) outTransportInfo;
try {
messageSender = jmsOut.createJMSSender(msgCtx);
} catch (JMSException e) {
handleException("Unable to create a JMSMessageSender for : " + outTransportInfo, e);
}
}
// The message property to be used to send the content type is determined by
// the out transport info, i.e. either from the EPR if we are sending a request,
// or, if we are sending a response, from the configuration of the service that
// received the request). The property can be defined globally in axis2.xml JMSSender section as well.
// The property name can be overridden by a message context property.
String contentTypeProperty = getContentTypeProperty(msgCtx, jmsOut, jmsConnectionFactory);
// Fix for ESBJAVA-3687, retrieve JMS transport property transport.jms.MessagePropertyHyphens and set this
// into the msgCtx
String hyphenSupport = JMSConstants.DEFAULT_HYPHEN_SUPPORT;
if (jmsConnectionFactory != null && jmsConnectionFactory.getParameters().get(JMSConstants.PARAM_JMS_HYPHEN_MODE) != null) {
hyphenSupport = jmsConnectionFactory.getParameters().get(JMSConstants.PARAM_JMS_HYPHEN_MODE);
} else if (jmsOut.getProperties() != null && jmsOut.getProperties().get(JMSConstants.PARAM_JMS_HYPHEN_MODE) != null) {
if (jmsOut.getProperties().get(JMSConstants.PARAM_JMS_HYPHEN_MODE).equals(JMSConstants.HYPHEN_MODE_REPLACE)) {
hyphenSupport = JMSConstants.HYPHEN_MODE_REPLACE;
} else if (jmsOut.getProperties().get(JMSConstants.PARAM_JMS_HYPHEN_MODE).equals(JMSConstants.HYPHEN_MODE_DELETE)) {
hyphenSupport = JMSConstants.HYPHEN_MODE_DELETE;
}
}
msgCtx.setProperty(JMSConstants.PARAM_JMS_HYPHEN_MODE, hyphenSupport);
JMSReplyMessage jmsReplyMessage = null;
// Set the status of the SessionWrapper as busy
if (messageSender.getSessionWrapper() != null) {
if (log.isDebugEnabled()) {
log.debug("Set the status of the session as busy for " + messageSender.getSessionWrapper().getSession());
}
messageSender.getSessionWrapper().setBusy(true);
}
// need to synchronize as Sessions are not thread safe
synchronized (messageSender.getSession()) {
try {
jmsReplyMessage = sendOverJMS(msgCtx, messageSender, contentTypeProperty, jmsConnectionFactory, jmsOut);
} finally {
if (msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER) == null) {
messageSender.close();
if (jmsReplyMessage != null && jmsReplyMessage.getReplyDestination() instanceof TemporaryQueue) {
String temporaryQueueName = "";
TemporaryQueue temporaryQueue = (TemporaryQueue) jmsReplyMessage.getReplyDestination();
try {
temporaryQueueName = temporaryQueue.getQueueName();
temporaryQueue.delete();
} catch (JMSException e) {
log.error("Error while deleting the temporary queue " + temporaryQueueName, e);
}
}
}
// Set the status of the SessionWrapper as free
if (messageSender.getSessionWrapper() != null) {
if (log.isDebugEnabled()) {
log.debug("Set the status of the session as free for " + messageSender.getSessionWrapper().getSession());
}
messageSender.getSessionWrapper().setBusy(false);
}
}
}
// Getting the response flow out of the synchronized block so that the session is released
// for other threads.
if (jmsReplyMessage.getMsgctx() != null) {
handleIncomingMessage(jmsReplyMessage.getMsgctx(), jmsReplyMessage.getTransportHeaders(),
jmsReplyMessage.getSoapAction(), jmsReplyMessage.getContentType());
}
}
/**
* Trying to rollback if type is XA transaction.
* @param msgCtx MessageContext.
* @throws AxisFault
*/
private void rollbackIfXATransaction(MessageContext msgCtx) throws AxisFault {
Transaction transaction = null;
try {
if (msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER) != null) {
transaction = ((TransactionManager) msgCtx.getProperty(JMSConstants
.JMS_XA_TRANSACTION_MANAGER)).getTransaction();
rollbackXATransaction(transaction);
}
} catch (SystemException e1) {
handleException("Error occurred during obtaining transaction", e1);
}
}
/**
* Retrieves the contentType property looking at the message context, jms outbound transport information
* and connection factory parameters
*
* @param msgCtx current message context
* @param jmsOut JMS outbound transport information
* @param jmsConnectionFactory JMS connection factory
* @return the content type property name
*/
protected String getContentTypeProperty(MessageContext msgCtx, JMSOutTransportInfo jmsOut,
JMSConnectionFactory jmsConnectionFactory) {
String contentTypeProperty =
(String) msgCtx.getProperty(JMSConstants.CONTENT_TYPE_PROPERTY_PARAM);
if (contentTypeProperty == null) {
contentTypeProperty = jmsOut.getContentTypeProperty();
}
if (contentTypeProperty == null && jmsConnectionFactory != null) {
contentTypeProperty = jmsConnectionFactory.getParameters().get(JMSConstants.CONTENT_TYPE_PROPERTY_PARAM);
}
return contentTypeProperty;
}
/**
* Perform actual sending of the JMS message
*/
private JMSReplyMessage sendOverJMS(MessageContext msgCtx, JMSMessageSender messageSender,
String contentTypeProperty, JMSConnectionFactory jmsConnectionFactory,
JMSOutTransportInfo jmsOut) throws AxisFault {
JMSReplyMessage jmsReplyMessage = null;
// convert the axis message context into a JMS Message that we can send over JMS
Message message = null;
String correlationId = null;
try {
message = createJMSMessage(msgCtx, messageSender.getSession(), contentTypeProperty);
} catch (JMSException e) {
rollbackIfXATransaction(msgCtx);
jmsConnectionFactory.clearCache(messageSender.getConnection());
handleException("Error creating a JMS message from the message context", e);
}
// should we wait for a synchronous response on this same thread?
boolean waitForResponse = waitForSynchronousResponse(msgCtx);
Destination replyDestination = jmsOut.getReplyDestination();
// if this is a synchronous out-in, prepare to listen on the response destination
String replyDestName;
if (isWaitForResponseOrReplyDestination(waitForResponse, replyDestination, msgCtx)) { //check replyDestination for APIMANAGER-5892
replyDestName = (String) msgCtx.getProperty(JMSConstants.JMS_REPLY_TO);
if (replyDestName == null && jmsConnectionFactory != null) {
if (jmsOut != null && jmsOut.getReplyDestinationName() != null) {
replyDestName = jmsOut.getReplyDestinationName();
} else {
replyDestName = jmsConnectionFactory.getReplyToDestination();
}
}
String replyDestType = (String) msgCtx.getProperty(JMSConstants.JMS_REPLY_TO_TYPE);
if (replyDestType == null && jmsConnectionFactory != null) {
replyDestType = jmsConnectionFactory.getReplyDestinationType();
}
if (replyDestName != null) {
if (jmsConnectionFactory != null) {
replyDestination = jmsConnectionFactory.getDestination(
replyDestName, replyDestType);
} else {
replyDestination = jmsOut.getReplyDestination(replyDestName);
}
}
replyDestination = JMSUtils.setReplyDestination(
replyDestination, messageSender.getSession(), message);
}
try {
if (msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION) != null) {
messageSender.send(message, msgCtx);
Transaction transaction = (Transaction) msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION);
if (msgCtx.getTo().toString().contains("transport.jms.TransactionCommand=end")) {
commitXATransaction(transaction);
} else if (msgCtx.getTo().toString().contains("transport.jms.TransactionCommand=rollback")) {
rollbackXATransaction(transaction);
}
} else {
messageSender.transactionallySend(message, msgCtx);
}
metrics.incrementMessagesSent(msgCtx);
} catch (AxisJMSException e) {
metrics.incrementFaultsSending();
rollbackIfXATransaction(msgCtx);
jmsConnectionFactory.clearCache(messageSender.getConnection());
handleException("Error sending JMS message", e);
} catch (JMSException e) {
handleException("Error sending JMS message to destination", e);
}
try {
metrics.incrementBytesSent(msgCtx, JMSUtils.getMessageSize(message));
} catch (JMSException e) {
log.warn("Error reading JMS message size to update transport metrics", e);
}
// if we are expecting a synchronous response back for the message sent out
if (isWaitForResponseOrReplyDestination(waitForResponse, replyDestination, msgCtx)) {
// TODO ********************************************************************************
// TODO **** replace with asynchronous polling via a poller task to process this *******
// information would be given. Then it should poll (until timeout) the
// requested destination for the response message and inject it from a
// asynchronous worker thread
try {
String jmsCorrelationID = message.getJMSCorrelationID();
if (jmsCorrelationID != null && jmsCorrelationID.length() > 0) {
correlationId = jmsCorrelationID;
} else {
correlationId = message.getJMSMessageID();
}
} catch(JMSException ignore) {}
// We assume here that the response uses the same message property to
// specify the content type of the message.
jmsReplyMessage = waitForResponseAndProcess(messageSender.getSession(), replyDestination,
msgCtx, correlationId, contentTypeProperty);
// TODO ********************************************************************************
}
if (jmsReplyMessage == null) {
jmsReplyMessage = new JMSReplyMessage();
}
jmsReplyMessage.setReplyDestination(replyDestination);
return jmsReplyMessage;
}
private boolean hasDestinationEPR(MessageContext msgContext) {
String transportURL = (String) msgContext.getProperty(
Constants.Configuration.TRANSPORT_URL);
if (transportURL != null) {
return true;
} else {
return (msgContext.getTo() != null) && !msgContext.getTo().hasAnonymousAddress();
}
}
protected boolean isWaitForResponseOrReplyDestination(boolean waitForResponse, Destination replyDestination) {
return waitForResponse || replyDestination != null;
}
protected boolean isWaitForResponseOrReplyDestination(boolean waitForResponse, Destination replyDestination,
MessageContext messageContext) {
return isWaitForResponseOrReplyDestination(waitForResponse, replyDestination) &&
hasDestinationEPR(messageContext);
}
/**
* Create a Consumer for the reply destination and wait for the response JMS message
* synchronously. If a message arrives within the specified time interval, process it
* through Axis2
*
* @param session the session to use to listen for the response
* @param replyDestination the JMS reply Destination
* @param msgCtx the outgoing message for which we are expecting the response
* @param contentTypeProperty the message property used to determine the content type
* of the response message
* @return Bean containing response message information.
* @throws AxisFault on error
*/
private JMSReplyMessage waitForResponseAndProcess(Session session, Destination replyDestination,
MessageContext msgCtx, String correlationId,
String contentTypeProperty) throws AxisFault {
JMSReplyMessage jmsReplyMessage = null;
MessageConsumer consumer = null;
try {
consumer = JMSUtils.createConsumer(session, replyDestination,
"JMSCorrelationID = '" + correlationId + "'");
// how long are we willing to wait for the sync response
long timeout = JMSConstants.DEFAULT_JMS_TIMEOUT;
String waitReply = (String) msgCtx.getProperty(JMSConstants.JMS_WAIT_REPLY);
if (waitReply != null) {
timeout = Long.valueOf(waitReply).longValue();
}
if (log.isDebugEnabled()) {
log.debug("Waiting for a maximum of " + timeout +
"ms for a response message to destination : " + replyDestination +
" with JMS correlation ID : " + correlationId);
}
Message reply = consumer.receive(timeout);
if (reply != null) {
// update transport level metrics
metrics.incrementMessagesReceived();
try {
metrics.incrementBytesReceived(JMSUtils.getMessageSize(reply));
} catch (JMSException e) {
log.warn("Error reading JMS message size to update transport metrics", e);
}
try {
jmsReplyMessage = processSyncResponse(msgCtx, reply, contentTypeProperty);
metrics.incrementMessagesReceived();
} catch (AxisFault e) {
metrics.incrementFaultsReceiving();
throw e;
}
} else {
metrics.incrementFaultsReceiving();
handleException("Did not receive a JMS response within " + timeout +
" ms to destination : " + replyDestination +
" with JMS correlation ID : " + correlationId);
}
} catch (JMSException e) {
metrics.incrementFaultsReceiving();
handleException("Error creating a consumer, or receiving a synchronous reply " +
"for outgoing MessageContext ID : " + msgCtx.getMessageID() +
" and reply Destination : " + replyDestination, e);
} finally {
try {
if (consumer != null) {
consumer.close();
}
} catch (JMSException e) {
handleException(
"Unable to close consumer for reply destination: " + replyDestination, e);
}
}
return jmsReplyMessage;
}
/**
* Create a JMS Message from the given MessageContext and using the given
* session
*
* @param msgContext the MessageContext
* @param session the JMS session
* @param contentTypeProperty the message property to be used to store the
* content type
* @return a JMS message from the context and session
* @throws JMSException on exception
* @throws AxisFault on exception
*/
private Message createJMSMessage(MessageContext msgContext, Session session,
String contentTypeProperty) throws JMSException, AxisFault {
Message message = null;
String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);
// check the first element of the SOAP body, do we have content wrapped using the
// default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
// text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
// for JMS but just get the payload in its native format
String jmsPayloadType = guessMessageType(msgContext);
if (jmsPayloadType == null) {
OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
MessageFormatter messageFormatter = null;
try {
messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
} catch (AxisFault axisFault) {
throw new JMSException("Unable to get the message formatter to use");
}
String contentType = messageFormatter.getContentType(
msgContext, format, msgContext.getSoapAction());
boolean useBytesMessage =
msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType) ||
contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
OutputStream out;
StringWriter sw;
if (useBytesMessage) {
BytesMessage bytesMsg = session.createBytesMessage();
sw = null;
out = new BytesMessageOutputStream(bytesMsg);
message = bytesMsg;
} else {
sw = new StringWriter();
try {
out = new WriterOutputStream(sw, format.getCharSetEncoding());
} catch (UnsupportedCharsetException ex) {
handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
return null;
}
}
try {
messageFormatter.writeTo(msgContext, format, out, true);
out.close();
} catch (IOException e) {
rollbackIfXATransaction(msgContext);
handleException("IO Error while creating BytesMessage", e);
}
if (!useBytesMessage) {
TextMessage txtMsg = session.createTextMessage();
txtMsg.setText(sw.toString());
message = txtMsg;
}
if (contentTypeProperty != null) {
message.setStringProperty(contentTypeProperty, contentType);
}
} else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
message = session.createBytesMessage();
BytesMessage bytesMsg = (BytesMessage) message;
OMElement wrapper = msgContext.getEnvelope().getBody().
getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
OMNode omNode = wrapper.getFirstOMChild();
if (omNode != null && omNode instanceof OMText) {
Object dh = ((OMText) omNode).getDataHandler();
if (dh != null && dh instanceof DataHandler) {
try {
((DataHandler) dh).writeTo(new BytesMessageOutputStream(bytesMsg));
} catch (IOException e) {
rollbackIfXATransaction(msgContext);
handleException("Error serializing binary content of element : " +
BaseConstants.DEFAULT_BINARY_WRAPPER, e);
}
}
}
} else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
message = session.createTextMessage();
TextMessage txtMsg = (TextMessage) message;
txtMsg.setText(msgContext.getEnvelope().getBody().
getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
} else if (JMSConstants.JMS_MAP_MESSAGE.equalsIgnoreCase(jmsPayloadType)){
message = session.createMapMessage();
JMSUtils.convertXMLtoJMSMap(msgContext.getEnvelope().getBody().getFirstChildWithName(
JMSConstants.JMS_MAP_QNAME),(MapMessage)message);
}
// set the JMS correlation ID if specified
String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
if (correlationId == null && msgContext.getRelatesTo() != null) {
correlationId = msgContext.getRelatesTo().getValue();
}
if (correlationId != null) {
message.setJMSCorrelationID(correlationId);
}
if (msgContext.isServerSide()) {
// set SOAP Action as a property on the JMS message
setProperty(message, msgContext, BaseConstants.SOAPACTION);
} else {
String action = msgContext.getOptions().getAction();
if (action != null) {
message.setStringProperty(BaseConstants.SOAPACTION, action);
}
}
JMSUtils.setTransportHeaders(msgContext, message);
// set INTERNAL_TRANSACTION_COUNTED property in the JMS message if it is present in the messageContext
setTransactionProperty(msgContext, message);
return message;
}
/**
* Guess the message type to use for JMS looking at the message contexts' envelope
* @param msgContext the message context
* @return JMSConstants.JMS_BYTE_MESSAGE or JMSConstants.JMS_TEXT_MESSAGE or null
*/
private String guessMessageType(MessageContext msgContext) {
OMElement firstChild = msgContext.getEnvelope().getBody().getFirstElement();
if (firstChild != null) {
if (BaseConstants.DEFAULT_BINARY_WRAPPER.equals(firstChild.getQName())) {
return JMSConstants.JMS_BYTE_MESSAGE;
} else if (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(firstChild.getQName())) {
return JMSConstants.JMS_TEXT_MESSAGE;
} else if (JMSConstants.JMS_MAP_QNAME.equals(firstChild.getQName())){
return JMSConstants.JMS_MAP_MESSAGE;
}
}
return null;
}
/**
* Creates an Axis MessageContext for the received JMS message and
* sets up the transports and various properties
*
* @param outMsgCtx the outgoing message for which we are expecting the response
* @param message the JMS response message received
* @param contentTypeProperty the message property used to determine the content type
* of the response message
* @return Bean containing response message information.
* @throws AxisFault on error
*/
private JMSReplyMessage processSyncResponse(MessageContext outMsgCtx, Message message,
String contentTypeProperty) throws AxisFault {
MessageContext responseMsgCtx = createResponseMessageContext(outMsgCtx);
// load any transport headers from received message
JMSUtils.loadTransportHeaders(message, responseMsgCtx);
String contentType = (contentTypeProperty == null) ? (String) outMsgCtx.getProperty(JMSConstants.CONTENT_TYPE)
: JMSUtils.getProperty(message, contentTypeProperty);
try {
JMSUtils.setSOAPEnvelope(message, responseMsgCtx, contentType);
} catch (JMSException ex) {
throw AxisFault.makeFault(ex);
}
JMSReplyMessage jmsReplyMessage = new JMSReplyMessage();
jmsReplyMessage.setMsgctx(responseMsgCtx);
jmsReplyMessage.setTransportHeaders(JMSUtils.getTransportHeaders(message, responseMsgCtx));
jmsReplyMessage.setSoapAction(JMSUtils.getProperty(message, BaseConstants.SOAPACTION));
jmsReplyMessage.setContentType(contentType);
return jmsReplyMessage;
}
/**
* Set the "INTERNAL_TRANSACTION_COUNTED" property in the message if it is present in the message context.
*
* @param msgContext the message context.
* @param message the JMS message.
*/
private void setTransactionProperty(MessageContext msgContext, Message message) {
Object transactionProperty = msgContext.getProperty(BaseConstants.INTERNAL_TRANSACTION_COUNTED);
if (transactionProperty instanceof Boolean) {
try {
message.setBooleanProperty(BaseConstants.INTERNAL_TRANSACTION_COUNTED, (Boolean) transactionProperty);
} catch (JMSException e) {
log.warn("Couldn't set message property : " + BaseConstants.INTERNAL_TRANSACTION_COUNTED + " = "
+ transactionProperty, e);
}
}
}
private void setProperty(Message message, MessageContext msgCtx, String key) {
String value = getProperty(msgCtx, key);
if (value != null) {
try {
message.setStringProperty(key, value);
} catch (JMSException e) {
log.warn("Couldn't set message property : " + key + " = " + value, e);
}
}
}
private String getProperty(MessageContext mc, String key) {
return (String) mc.getProperty(key);
}
public void clearActiveConnections(){
log.error("Not Implemented.");
}
private void commitXATransaction(Transaction transaction) throws AxisFault {
ArrayList<JMSMessageSender> msgSenderList = (ArrayList) jmsMessageSenderMap.get(transaction);
if (msgSenderList.size() > 0) {
for (JMSMessageSender msgSender : msgSenderList) {
try {
msgSender.getJmsXaResource().end(msgSender.getJmsXAXid(), XAResource.TMSUCCESS);
msgSender.getJmsXaResource().prepare(msgSender.getJmsXAXid());
msgSender.getJmsXaResource().commit(msgSender.getJmsXAXid(), false);
msgSender.close();
} catch (XAException e) {
handleException("Error occurred during rolling back transaction", e);
msgSender.closeOnException();
}
}
jmsMessageSenderMap.remove(transaction);
}
}
private void rollbackXATransaction(Transaction transaction) throws AxisFault {
ArrayList<JMSMessageSender> msgSenderList = (ArrayList) jmsMessageSenderMap.get(transaction);
if (msgSenderList.size() > 0) {
for (JMSMessageSender msgSender : msgSenderList) {
try {
msgSender.getJmsXaResource().end(msgSender.getJmsXAXid(), XAResource.TMSUCCESS);
msgSender.getJmsXaResource().prepare(msgSender.getJmsXAXid());
msgSender.getJmsXaResource().commit(msgSender.getJmsXAXid(), false);
msgSender.close();
} catch (XAException e) {
handleException("Error occurred during rolling back transaction", e);
msgSender.closeOnException();
}
}
jmsMessageSenderMap.remove(transaction);
}
}
}