1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.ws.utils;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.apache.cxf.binding.soap.Soap11;
21 import org.apache.cxf.binding.soap.Soap12;
22 import org.apache.cxf.message.Exchange;
23 import org.apache.cxf.message.Message;
24 import org.apache.cxf.ws.addressing.Names;
25 import org.apache.cxf.ws.addressing.VersionTransformer.Names200403;
26 import org.apache.cxf.ws.addressing.VersionTransformer.Names200408;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34
35
36
37
38 public abstract class SoapUtils {
39 private static final transient Logger LOG = LoggerFactory.getLogger(SoapUtils.class);
40
41 private SoapUtils() {
42 throw new UnsupportedOperationException("Utility class cannot be instantiated");
43 }
44
45
46
47
48
49
50
51
52 public static final Set<String> WS_ADDRESSING_NS_URIS;
53
54
55
56
57 public static final Set<String> SOAP_NS_URIS;
58
59 static {
60 WS_ADDRESSING_NS_URIS = new HashSet<>();
61 WS_ADDRESSING_NS_URIS.add(Names.WSA_NAMESPACE_NAME);
62 WS_ADDRESSING_NS_URIS.add(Names200403.WSA_NAMESPACE_NAME);
63 WS_ADDRESSING_NS_URIS.add(Names200408.WSA_NAMESPACE_NAME);
64
65 SOAP_NS_URIS = new HashSet<>();
66 SOAP_NS_URIS.add(Soap11.SOAP_NAMESPACE);
67 SOAP_NS_URIS.add(Soap12.SOAP_NAMESPACE);
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static String extractSoapBody(String soapEnvelope) {
86 try {
87
88
89
90
91
92
93
94
95
96
97
98 int pos1, pos2, pos3, pos4, pos5;
99 pos1 = soapEnvelope.lastIndexOf("<");
100 pos1 = soapEnvelope.lastIndexOf("<", pos1 - 1);
101 pos5 = soapEnvelope.indexOf(">", pos1);
102 if (soapEnvelope.charAt(pos5 - 1) == '/') {
103 return "";
104 }
105 pos2 = soapEnvelope.indexOf(":", pos1);
106 String soapPrefix = ((pos2 == -1) || (pos5 < pos2)) ?
107 "" : soapEnvelope.substring(pos1 + 2, pos2 + 1);
108 String bodyElementStart = "<" + soapPrefix + "Body";
109 pos3 = soapEnvelope.indexOf(bodyElementStart);
110 pos4 = soapEnvelope.indexOf('>', pos3 + bodyElementStart.length());
111 return soapEnvelope.substring(pos4 + 1, pos1);
112
113 } catch(Exception e) {
114 LOG.error("Invalid contents, probably not a SOAP Envelope in the parameter", e);
115 return soapEnvelope;
116 }
117 }
118
119
120
121
122
123
124 public static Exception extractOutgoingException(Exchange exchange) {
125 Message outFaultMessage = exchange.getOutFaultMessage();
126 return (outFaultMessage != null) ? outFaultMessage.getContent(Exception.class) : null;
127 }
128
129
130
131
132
133
134 public static String extractOutgoingPayload(Exchange exchange) {
135 try {
136 return (String) exchange.getOutMessage().getContent(List.class).get(0);
137 } catch (Exception e) {
138 return null;
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151 public static Element getElementNS(
152 Element root,
153 Set<String> nsUris,
154 String wantedLocalName)
155 {
156 if (root == null) {
157 return null;
158 }
159
160 Node node = root.getFirstChild();
161 while (node != null) {
162 if ((node instanceof Element) &&
163 nsUris.contains(node.getNamespaceURI()) &&
164 node.getLocalName().equals(wantedLocalName)) {
165 return (Element) node;
166 }
167
168 node = node.getNextSibling();
169 }
170
171 return null;
172 }
173 }