-
Notifications
You must be signed in to change notification settings - Fork 33
/
MessageUtil.java
686 lines (466 loc) · 20.2 KB
/
MessageUtil.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
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.ums.common;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Random;
import jakarta.xml.soap.Name;
import jakarta.xml.soap.SOAPBodyElement;
import jakarta.xml.soap.SOAPElement;
import jakarta.xml.soap.SOAPException;
import jakarta.xml.soap.SOAPFactory;
import jakarta.xml.soap.SOAPHeader;
import jakarta.xml.soap.SOAPHeaderElement;
import jakarta.xml.soap.SOAPMessage;
import jakarta.xml.soap.MessageFactory;
import jakarta.xml.soap.SOAPConstants;
public class MessageUtil {
private static MessageFactory messageFactory = null;
private static SOAPFactory soapFactory = null;
private static String HOST_ADDRESS = null;
private static Object syncObj = new Object();
private static long sequence = 0;
private static long MSG_RANDOM = 0;
private static String MSG_ID_PREFIX = null;
static {
try {
soapFactory = SOAPFactory.newInstance();
// messageFactory = MessageFactory.newInstance();
// messageFactory = MessageFactory.newInstance(SOAPConstants.DEFAULT_SOAP_PROTOCOL);
messageFactory = MessageFactory.newInstance(SOAPConstants.DEFAULT_SOAP_PROTOCOL);
HOST_ADDRESS = InetAddress.getLocalHost().toString();
sequence = 0;
Random random = new Random();
random.setSeed(System.currentTimeMillis());
MSG_RANDOM = random.nextLong();
MSG_ID_PREFIX = HOST_ADDRESS + "-" + MSG_RANDOM + "-";
} catch (Exception se) {
se.printStackTrace();
}
}
public static SOAPHeaderElement getMessageHeaderElement(SOAPMessage message) throws SOAPException {
SOAPHeader soapHeader = message.getSOAPHeader();
SOAPHeaderElement she = (SOAPHeaderElement) getJMSChildElement(soapHeader, Constants.MESSAGE_HEADER);
return she;
}
public static SOAPHeaderElement removeMessageHeaderElement(SOAPMessage message) throws SOAPException {
SOAPHeader soapHeader = message.getSOAPHeader();
SOAPHeaderElement she = (SOAPHeaderElement) getJMSChildElement(soapHeader, Constants.MESSAGE_HEADER);
she.detachNode();
message.saveChanges();
return she;
}
public static Iterator getJMSProperties(SOAPMessage m) throws SOAPException {
SOAPHeader sheader = m.getSOAPHeader();
Name propEleName = createJMSName(InternalConstants.JMS_PROPERTY);
Iterator it = sheader.getChildElements(propEleName);
if (it.hasNext()) {
SOAPHeaderElement she = (SOAPHeaderElement) it.next();
return she.getChildElements();
}
// iterator with no elements.
return it;
}
public static boolean isStringProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.STRING_PROPERTY)) {
return true;
}
return false;
}
public static boolean isBooleanProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.BOOLEAN_PROPERTY)) {
return true;
}
return false;
}
public static boolean isLongProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.LONG_PROPERTY)) {
return true;
}
return false;
}
public static boolean isShortProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.SHORT_PROPERTY)) {
return true;
}
return false;
}
public static boolean isByteProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.BYTE_PROPERTY)) {
return true;
}
return false;
}
public static boolean isIntProperty(SOAPElement se) throws SOAPException {
if (se.getLocalName().equals(InternalConstants.INT_PROPERTY)) {
return true;
}
return false;
}
public static String getPropertyName(SOAPElement se) throws SOAPException {
Name attrName = createJMSName(InternalConstants.PNAME);
String propName = se.getAttributeValue(attrName);
return propName;
}
public static Object getPropertyValue(SOAPElement se) throws SOAPException {
Name pvalueAttrName = createJMSName(InternalConstants.PVALUE);
String pValue = se.getAttributeValue(pvalueAttrName);
Object pvObj = null;
if (isStringProperty(se)) {
pvObj = pValue;
} else if (isLongProperty(se)) {
pvObj = Long.valueOf(pValue);
} else if (isIntProperty(se)) {
pvObj = Integer.valueOf(pValue);
} else if (isShortProperty(se)) {
pvObj = Short.valueOf(pValue);
} else if (isBooleanProperty(se)) {
pvObj = Boolean.valueOf(pValue);
} else if (isByteProperty(se)) {
pvObj = Byte.valueOf(pValue);
}
return pvObj;
}
public static SOAPHeaderElement getSOAPHeaderElement(SOAPMessage message, String headerName, String nameSpace) throws SOAPException {
Iterator it = message.getSOAPHeader().examineAllHeaderElements();
while (it.hasNext()) {
SOAPHeaderElement she = (SOAPHeaderElement) it.next();
String uri = she.getNamespaceURI();
String localName = she.getLocalName();
if (headerName.equals(localName) && nameSpace.equals(uri)) {
// found message header.
return she;
}
}
return null;
}
public static SOAPBodyElement getJMSBodyElement(SOAPMessage message, String localName) throws SOAPException {
Name name = createJMSName(localName);
Iterator it = message.getSOAPBody().getChildElements(name);
if (it.hasNext()) {
return (SOAPBodyElement) it.next();
}
return null;
}
public static String getJMSChildElementValue(SOAPElement soapElement, String localName) throws SOAPException {
String value = null;
SOAPElement se = getJMSChildElement(soapElement, localName);
if (se != null) {
value = se.getValue();
}
return value;
}
public static SOAPElement getJMSChildElement(SOAPElement soapElement, String localName) throws SOAPException {
SOAPElement se = null;
Name name = createJMSName(localName);
Iterator it = soapElement.getChildElements(name);
if (it.hasNext()) {
se = (SOAPElement) it.next();
}
return se;
}
public static boolean isJMSPropertyExists(SOAPHeader sheader) throws SOAPException {
Name pname = createJMSName(InternalConstants.JMS_PROPERTY);
Iterator it = sheader.getChildElements(pname);
if (it.hasNext()) {
return true;
}
return false;
}
public static SOAPHeaderElement getJMSPropertyElement(SOAPHeader sheader) throws SOAPException {
SOAPHeaderElement she = (SOAPHeaderElement) getJMSChildElement(sheader, InternalConstants.JMS_PROPERTY);
return she;
}
public static SOAPHeaderElement addJMSPropertyElement(SOAPHeader sheader) throws SOAPException {
// Name pname = createJMSName (Constants.JMS_PROPERTY);
// System.out.println("*** JMS property exists: " + pname.getQualifiedName());
SOAPHeaderElement she = getJMSPropertyElement(sheader);
if (she == null) {
she = addJMSNsSOAPHeaderElement(sheader, InternalConstants.JMS_PROPERTY);
}
return she;
}
public static SOAPElement setObjectProperty(SOAPMessage soapm, String pname, Object pvalue) throws SOAPException {
SOAPHeader sheader = soapm.getSOAPHeader();
SOAPHeaderElement jmsPropertyRoot = getJMSPropertyElement(sheader);
if (jmsPropertyRoot == null) {
jmsPropertyRoot = addJMSPropertyElement(sheader);
}
SOAPElement propEle = setJMSProperty(jmsPropertyRoot, pname, pvalue);
return propEle;
}
public static SOAPElement setJMSProperty(SOAPHeaderElement jmsPropertyRoot, String pname, Object pvalue) throws SOAPException {
// prop element to be added to JMSProperty element
String eleName = null;
if (pvalue instanceof String) {
eleName = InternalConstants.STRING_PROPERTY;
// System.out.println ("Message util setting String prop ... " +
// pvalue );
} else if (pvalue instanceof Integer) {
eleName = InternalConstants.INT_PROPERTY;
// System.out.println ("Message util setting Int prop ... " + pvalue
// );
} else if (pvalue instanceof Long) {
eleName = InternalConstants.LONG_PROPERTY;
// System.out.println ("Message util setting Long prop ... " +
// pvalue );
} else if (pvalue instanceof Boolean) {
eleName = InternalConstants.BOOLEAN_PROPERTY;
// System.out.println ("Message util setting Boolean prop... " +
// pvalue );
} else if (pvalue instanceof Short) {
eleName = InternalConstants.SHORT_PROPERTY;
// System.out.println ("Message util setting SHORT prop... " +
// pvalue );
} else if (pvalue instanceof Byte) {
eleName = InternalConstants.BYTE_PROPERTY;
// System.out.println ("Message util setting Byte prop ... " +
// pvalue );
} else {
throw new SOAPException("Invalid property value." + pvalue);
}
// SOAPElement propEle = jmsPropertyRoot.addChildElement(eleName);
SOAPElement propEle = MessageUtil.addJMSChildElement(jmsPropertyRoot, eleName);
// pname attr
Name propNameAttr = createJMSName(InternalConstants.PNAME);
// pvalue attr
Name propValueAttr = createJMSName(InternalConstants.PVALUE);
// add pname attr
propEle.addAttribute(propNameAttr, pname);
// add pvalue attr.
propEle.addAttribute(propValueAttr, pvalue.toString());
// System.out.println ("Message util setting prop attributes for "
// + eleName + ", " + pname + "=" +pvalue);
return propEle;
}
public static Name createJMSName(String localName) throws SOAPException {
Name name = soapFactory.createName(localName, Constants.JMS_NS_PREFIX, Constants.JMS_NS_URI);
return name;
}
public static SOAPHeaderElement addMessageHeader(SOAPMessage soapm) throws SOAPException {
SOAPHeaderElement she = getMessageHeaderElement(soapm);
if (she == null) {
SOAPHeader sh = soapm.getSOAPHeader();
she = addJMSNsSOAPHeaderElement(sh, Constants.MESSAGE_HEADER);
addMessageHeaderChildElements(she);
soapm.saveChanges();
}
return she;
}
public static SOAPMessage newMessageInstance() throws SOAPException {
SOAPMessage soapm = null;
/**
* sync create new instance to make sure to work in ALL SAAJ impl.
*/
synchronized (syncObj) {
soapm = messageFactory.createMessage();
}
SOAPHeader sh = soapm.getSOAPHeader();
SOAPHeaderElement she = addJMSNsSOAPHeaderElement(sh, Constants.MESSAGE_HEADER);
addMessageHeaderChildElements(she);
soapm.saveChanges();
return soapm;
}
public static SOAPHeaderElement addJMSNsSOAPHeaderElement(SOAPHeader soapHeader, String localName) throws SOAPException {
Name mh = createJMSName(localName);
SOAPHeaderElement headerElement = soapHeader.addHeaderElement(mh);
addHeaderAttributes(headerElement);
return headerElement;
}
public static void addHeaderAttributes(SOAPHeaderElement she) throws SOAPException {
Name id = createJMSName(Constants.ID);
she.addAttribute(id, "1.0");
Name version = createJMSName(Constants.VERSION);
she.addAttribute(version, "1.1");
// she.setMustUnderstand(true);
}
public static void addMessageHeaderChildElements(SOAPHeaderElement messageHeader) throws SOAPException {
// SOAPElement se = null;
// se = addJMSChildElement(messageHeader, Constants.FROM);
// se.setValue(Constants.FROM_DEFAULT_VALUE);
// se = addJMSChildElement(messageHeader, Constants.TO);
// se.setValue(Constants.TO_DEAFULT_VALUE);
// se = addJMSChildElement(messageHeader, Constants.MESSAGE_ID);
// String mid = getNewMessageID();
// se.setValue(mid);
// se = addJMSChildElement(messageHeader, Constants.TIMESTAMP);
// String ts = getTimestamp(mid);
// se.setValue(ts);
// addJMSChildElement(messageHeader, Constants.REF_TO_MESSAGE_ID);
addJMSChildElement(messageHeader, Constants.SERVICE);
}
public static SOAPElement addJMSChildElement(SOAPElement element, String localName) throws SOAPException {
Name name = createJMSName(localName);
SOAPElement se = element.addChildElement(name);
return se;
}
/**
* Create ack message from the specified soap message.
*
* @param soapm the message to be acknowledged.
* @return the created acknowledge message.
*/
public static SOAPMessage createAcknowledgeMessage(SOAPMessage soapm) throws SOAPException {
// create message
SOAPMessage ackm = newMessageInstance();
// add acknowledge element
SOAPHeaderElement ackele = addJMSNsSOAPHeaderElement(ackm.getSOAPHeader(), InternalConstants.ACKNOWLEDGMENT);
// message to be acked
SOAPHeaderElement messageHeader = getMessageHeaderElement(soapm);
String mid = getJMSChildElementValue(messageHeader, Constants.MESSAGE_ID);
SOAPHeaderElement ackMsgHdr = getMessageHeaderElement(ackm);
SOAPElement se = getJMSChildElement(ackMsgHdr, Constants.REF_TO_MESSAGE_ID);
se.setValue(mid);
// add RefToMessageID element
SOAPElement refEle = addJMSChildElement(ackele, Constants.REF_TO_MESSAGE_ID);
refEle.setValue(mid);
ackm.saveChanges();
return ackm;
}
/**
* Create ack message from the specified soap message.
*
* @param soapm the message to be acknowledged.
* @return the created acknowledge message.
*/
public static SOAPMessage createResponseMessage(SOAPMessage soapm) throws SOAPException {
// create message
SOAPMessage ackm = newMessageInstance();
ackm = createResponseMessage2(soapm, ackm);
return ackm;
}
/**
* Create ack message from the specified soap message.
*
* @param req the message to be acknowledged.
* @return the created acknowledge message.
*/
public static SOAPMessage createResponseMessage2(SOAPMessage req, SOAPMessage resp) throws SOAPException {
// create message
// SOAPMessage ackm = newMessageInstance();
checkJMSMessageHeader(resp);
resp.saveChanges();
return resp;
}
public static SOAPMessage createResponseMessage2_save(SOAPMessage req, SOAPMessage resp) throws SOAPException {
// create message
// SOAPMessage ackm = newMessageInstance();
checkJMSMessageHeader(resp);
// set ref to mid
SOAPHeaderElement messageHeader = getMessageHeaderElement(req);
String mid = getJMSChildElementValue(messageHeader, Constants.MESSAGE_ID);
SOAPHeaderElement ackMsgHdr = getMessageHeaderElement(resp);
SOAPElement se = getJMSChildElement(ackMsgHdr, Constants.REF_TO_MESSAGE_ID);
se.setValue(mid);
// get <from> value from req msg
String from = getJMSChildElementValue(messageHeader, Constants.FROM);
// get <to> element from resp msg
SOAPElement toEle = getJMSChildElement(ackMsgHdr, Constants.TO);
toEle.setValue(from);
resp.saveChanges();
return resp;
}
public static void checkJMSMessageHeader(SOAPMessage sm) throws SOAPException {
SOAPHeaderElement msgHeader = getMessageHeaderElement(sm);
if (msgHeader != null) {
msgHeader.detachNode();
}
addMessageHeader(sm);
}
/**
* XXX chiaming 06/19/2008- OLD -- to be removed
*/
public static void setService(SOAPMessage soapm, String value) throws SOAPException {
SOAPHeaderElement mh = MessageUtil.getMessageHeaderElement(soapm);
SOAPElement service = MessageUtil.addJMSChildElement(mh, Constants.SERVICE);
service.setValue(value);
soapm.saveChanges();
}
public static void setServiceAttribute(SOAPMessage soapm, String localName, String value) throws SOAPException {
SOAPHeaderElement mh = getMessageHeaderElement(soapm);
SOAPElement serviceElement = getJMSChildElement(mh, Constants.SERVICE);
Name n = createJMSName(localName);
serviceElement.addAttribute(n, value);
soapm.saveChanges();
}
public static String getServiceAttribute(SOAPMessage soapm, String localName) throws SOAPException {
SOAPHeaderElement mh = getMessageHeaderElement(soapm);
SOAPElement serviceElement = getJMSChildElement(mh, Constants.SERVICE);
if (serviceElement == null) {
throw new SOAPException("Message does not contain a Service SOAP Header Element.");
}
Name n = createJMSName(localName);
String value = serviceElement.getAttributeValue(n);
return value;
}
public static String getServiceDestinationName(SOAPMessage soapm) throws SOAPException {
String destName = getServiceAttribute(soapm, Constants.DESTINATION_NAME);
return destName;
}
public static long getServiceReceiveTimeout(SOAPMessage soapm) throws SOAPException {
// in milli secconds
long timeout = 30000;
String str = getServiceAttribute(soapm, Constants.RECEIVE_TIMEOUT);
if (str != null) {
try {
timeout = Integer.parseInt(str);
} catch (Exception e) {
throw new SOAPException(e);
}
}
return timeout;
}
public static boolean isServiceTopicDomain(SOAPMessage soapm) throws SOAPException {
boolean isTopic = false;
String domain = getServiceAttribute(soapm, Constants.DOMAIN);
if (Constants.TOPIC_DOMAIN.equals(domain)) {
isTopic = true;
} else if (Constants.QUEUE_DOMAIN.equals(domain)) {
isTopic = false;
} else {
throw new SOAPException("SOAP message does not contain domain attribute.");
}
return isTopic;
}
public static String getServiceName(SOAPMessage soapm) throws SOAPException {
String sname = getServiceAttribute(soapm, Constants.SERVICE_NAME);
return sname;
}
public static String getServiceClientId(SOAPMessage soapm) throws SOAPException {
String clientId = getServiceAttribute(soapm, Constants.CLIENT_ID);
return clientId;
}
public static String getNewMessageID() {
return MSG_ID_PREFIX + System.currentTimeMillis() + getNextSequence();
}
public static long getNextSequence() {
synchronized (syncObj) {
if (sequence == Long.MAX_VALUE) {
sequence = 0;
}
sequence++;
}
return sequence;
}
/*
* private static String getTimestamp(String mid) { int index = mid.lastIndexOf('-') + 1;
*
* String ts = mid.substring(index);
*
* return ts; }
*/
}