1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.http.eventcore; |
---|
16 | |
---|
17 | import java.net.MalformedURLException; |
---|
18 | import java.net.URL; |
---|
19 | import java.util.Properties; |
---|
20 | import java.util.logging.Level; |
---|
21 | |
---|
22 | import javax.xml.soap.SOAPException; |
---|
23 | import javax.xml.soap.SOAPMessage; |
---|
24 | |
---|
25 | import org.w3c.dom.Element; |
---|
26 | |
---|
27 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
28 | import de.ugoe.cs.autoquest.plugin.http.logdata.Header; |
---|
29 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
30 | import de.ugoe.cs.util.console.Console; |
---|
31 | |
---|
32 | /** |
---|
33 | * <p> |
---|
34 | * represents the specific HTTP event for a SOAP message exchange. It contains the SOAP request |
---|
35 | * and response envelopes and is more concrete when comparing object. E.g., it considers the |
---|
36 | * called SOAP operation on performing comparison. |
---|
37 | * </p> |
---|
38 | * |
---|
39 | * @author Patrick Harms |
---|
40 | */ |
---|
41 | public final class SOAPEventType extends HTTPEventType { |
---|
42 | |
---|
43 | /** */ |
---|
44 | private static final long serialVersionUID = 1L; |
---|
45 | |
---|
46 | /** |
---|
47 | * <p> |
---|
48 | * the SOAP request belonging to the event |
---|
49 | * </p> |
---|
50 | */ |
---|
51 | transient private final SOAPMessage soapRequest; |
---|
52 | |
---|
53 | /** |
---|
54 | * <p> |
---|
55 | * the SOAP response belonging to the event |
---|
56 | * </p> |
---|
57 | */ |
---|
58 | transient private final SOAPMessage soapResponse; |
---|
59 | |
---|
60 | /** |
---|
61 | * <p> |
---|
62 | * the SOAP method called in this request |
---|
63 | * </p> |
---|
64 | */ |
---|
65 | private final String calledMethod; |
---|
66 | |
---|
67 | /** |
---|
68 | * <p> |
---|
69 | * the name of the client; this is either the host/ip and port of the sender or, if a path |
---|
70 | * map is available, a human readable name that may be based also on the receiver |
---|
71 | * </p> |
---|
72 | */ |
---|
73 | private final String clientName; |
---|
74 | |
---|
75 | /** |
---|
76 | * <p> |
---|
77 | * the name of the service; this is either the path or, if a path map is available, a human |
---|
78 | * readable name that is mapped to the path |
---|
79 | * </p> |
---|
80 | */ |
---|
81 | private final String serviceName; |
---|
82 | |
---|
83 | /** |
---|
84 | * <p> |
---|
85 | * the human readable name of this event type |
---|
86 | * </p> |
---|
87 | */ |
---|
88 | private final String name; |
---|
89 | |
---|
90 | /** |
---|
91 | * <p> |
---|
92 | * initializes the event type with the represented HTTP exchange and the already extracted |
---|
93 | * SOAP request and response. |
---|
94 | * </p> |
---|
95 | * |
---|
96 | * @param exchange the represented HTTP exchange |
---|
97 | * @param soapRequest the already extracted SOAP request |
---|
98 | * @param soapResponse the already extracted SOAP response |
---|
99 | * @param urlNameMap used to map paths of servives to logical names |
---|
100 | */ |
---|
101 | public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) { |
---|
102 | super(exchange); |
---|
103 | this.soapRequest = soapRequest; |
---|
104 | this.soapResponse = soapResponse; |
---|
105 | this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest)); |
---|
106 | |
---|
107 | String path = null; |
---|
108 | |
---|
109 | if ((exchange.getRequest() != null) && (exchange.getRequest().getUrl() != null)) { |
---|
110 | try { |
---|
111 | path = new URL(exchange.getRequest().getUrl()).getPath(); |
---|
112 | } |
---|
113 | catch (MalformedURLException e) { |
---|
114 | // ignore and continue |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | StringBuffer nameBuffer = new StringBuffer("SOAPEvent"); |
---|
119 | |
---|
120 | // determine the client name |
---|
121 | if (urlNameMap == null) { |
---|
122 | clientName = exchange.getSender().getHost() + ":" + exchange.getSender().getPort(); |
---|
123 | } |
---|
124 | else { |
---|
125 | // check for a mapping of the sender host |
---|
126 | String key = "clientName.sender." + exchange.getSender().getHost(); |
---|
127 | String value = urlNameMap.getProperty(key); |
---|
128 | |
---|
129 | if (value == null) { |
---|
130 | key += ":" + exchange.getSender().getPort(); |
---|
131 | value = urlNameMap.getProperty(key); |
---|
132 | } |
---|
133 | |
---|
134 | if (value == null) { |
---|
135 | key = "clientName.receiver.server." + exchange.getReceiver().getHost(); |
---|
136 | value = urlNameMap.getProperty(key); |
---|
137 | } |
---|
138 | |
---|
139 | if (value == null) { |
---|
140 | key += ":" + exchange.getReceiver().getPort(); |
---|
141 | value = urlNameMap.getProperty(key); |
---|
142 | } |
---|
143 | |
---|
144 | if (value == null) { |
---|
145 | key = "clientName.receiver.port." + exchange.getReceiver().getPort(); |
---|
146 | value = urlNameMap.getProperty(key); |
---|
147 | } |
---|
148 | |
---|
149 | if (value == null) { |
---|
150 | key = "clientName.default"; |
---|
151 | value = urlNameMap.getProperty(key); |
---|
152 | } |
---|
153 | |
---|
154 | if (value != null) { |
---|
155 | clientName = value; |
---|
156 | } |
---|
157 | else { |
---|
158 | clientName = exchange.getSender().getHost() + ":" + exchange.getSender().getPort(); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | nameBuffer.append("("); |
---|
163 | nameBuffer.append(clientName); |
---|
164 | |
---|
165 | // determine the service name |
---|
166 | if (path != null) { |
---|
167 | nameBuffer.append(", "); |
---|
168 | if (urlNameMap == null) { |
---|
169 | serviceName = path; |
---|
170 | } |
---|
171 | else { |
---|
172 | String value = urlNameMap.getProperty("serviceName.path." + path); |
---|
173 | if (value != null) { |
---|
174 | serviceName = value; |
---|
175 | } |
---|
176 | else { |
---|
177 | serviceName = path; |
---|
178 | } |
---|
179 | } |
---|
180 | nameBuffer.append(serviceName); |
---|
181 | } |
---|
182 | else { |
---|
183 | serviceName = "NA"; |
---|
184 | } |
---|
185 | |
---|
186 | if (calledMethod != null) { |
---|
187 | nameBuffer.append(", "); |
---|
188 | nameBuffer.append(calledMethod); |
---|
189 | } |
---|
190 | |
---|
191 | String senderStr = HTTPUtils.toString(exchange.getSender()); |
---|
192 | String receiverStr = HTTPUtils.toString(exchange.getReceiver()); |
---|
193 | |
---|
194 | if ((senderStr != null) && (receiverStr != null)) { |
---|
195 | nameBuffer.append(", "); |
---|
196 | nameBuffer.append(senderStr); |
---|
197 | nameBuffer.append(" --> "); |
---|
198 | nameBuffer.append(receiverStr); |
---|
199 | } |
---|
200 | else if (senderStr != null) { |
---|
201 | nameBuffer.append(", "); |
---|
202 | nameBuffer.append(senderStr); |
---|
203 | } |
---|
204 | else if (receiverStr != null) { |
---|
205 | nameBuffer.append(", "); |
---|
206 | nameBuffer.append(receiverStr); |
---|
207 | } |
---|
208 | |
---|
209 | nameBuffer.append(")"); |
---|
210 | |
---|
211 | this.name = nameBuffer.toString(); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * <p> |
---|
216 | * the SOAP method called in this request |
---|
217 | * </p> |
---|
218 | * |
---|
219 | * @return the SOAP method called in this request |
---|
220 | */ |
---|
221 | public String getCalledMethod() { |
---|
222 | return calledMethod; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * <p> |
---|
227 | * the name of the client calling in this request |
---|
228 | * </p> |
---|
229 | * |
---|
230 | * @return the name of the client calling in this request |
---|
231 | */ |
---|
232 | public String getClientName() { |
---|
233 | return clientName; |
---|
234 | } |
---|
235 | |
---|
236 | /** |
---|
237 | * <p> |
---|
238 | * the name of the service called in this request |
---|
239 | * </p> |
---|
240 | * |
---|
241 | * @return the name of the service called in this request |
---|
242 | */ |
---|
243 | public String getServiceName() { |
---|
244 | return serviceName; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * @return the soapRequest |
---|
249 | */ |
---|
250 | public SOAPMessage getSoapRequest() { |
---|
251 | return soapRequest; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * @return the soapResponse |
---|
256 | */ |
---|
257 | public SOAPMessage getSoapResponse() { |
---|
258 | return soapResponse; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * @return the body of the soapRequest |
---|
263 | */ |
---|
264 | public Element getSoapRequestBody() { |
---|
265 | try { |
---|
266 | return soapRequest.getSOAPBody(); |
---|
267 | } |
---|
268 | catch (SOAPException e) { |
---|
269 | return null; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * @return the body of the soapResponse |
---|
275 | */ |
---|
276 | public Element getSoapResponseBody() { |
---|
277 | try { |
---|
278 | if( soapResponse!=null ) { |
---|
279 | return soapResponse.getSOAPBody(); |
---|
280 | } else { |
---|
281 | return null; |
---|
282 | } |
---|
283 | } |
---|
284 | catch (SOAPException e) { |
---|
285 | return null; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | /* (non-Javadoc) |
---|
290 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName() |
---|
291 | */ |
---|
292 | @Override |
---|
293 | public String getName() { |
---|
294 | return name; |
---|
295 | } |
---|
296 | |
---|
297 | /* (non-Javadoc) |
---|
298 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#toString() |
---|
299 | */ |
---|
300 | @Override |
---|
301 | public String toString() { |
---|
302 | return name; |
---|
303 | } |
---|
304 | |
---|
305 | /* (non-Javadoc) |
---|
306 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object) |
---|
307 | */ |
---|
308 | @Override |
---|
309 | public boolean equals(Object obj) { |
---|
310 | if (this == obj) { |
---|
311 | return true; |
---|
312 | } |
---|
313 | else if (obj instanceof SOAPEventType) { |
---|
314 | if (!obj.getClass().isAssignableFrom(this.getClass())) { |
---|
315 | return false; |
---|
316 | } |
---|
317 | |
---|
318 | return |
---|
319 | super.equals(obj) && |
---|
320 | HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) && |
---|
321 | HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName) && |
---|
322 | HTTPUtils.equals(clientName, ((SOAPEventType) obj).clientName); |
---|
323 | } |
---|
324 | else { |
---|
325 | return false; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | /* (non-Javadoc) |
---|
330 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode() |
---|
331 | */ |
---|
332 | @Override |
---|
333 | public int hashCode() { |
---|
334 | int hashCode = super.hashCode(); |
---|
335 | if (calledMethod != null) { |
---|
336 | hashCode += calledMethod.hashCode(); |
---|
337 | } |
---|
338 | if( serviceName != null ) { |
---|
339 | hashCode += serviceName.hashCode(); |
---|
340 | } |
---|
341 | if( clientName != null ) { |
---|
342 | hashCode += clientName.hashCode(); |
---|
343 | } |
---|
344 | return hashCode; |
---|
345 | } |
---|
346 | |
---|
347 | /** |
---|
348 | * <p> |
---|
349 | * determines the name of the method called in a SOAP request either through the HTTP header |
---|
350 | * or through the SOAP body |
---|
351 | * </p> |
---|
352 | */ |
---|
353 | private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) { |
---|
354 | // first check for a header containing the SOAP action |
---|
355 | |
---|
356 | if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) && |
---|
357 | (exchange.getRequest().getHeaders().getHeader() != null)) |
---|
358 | { |
---|
359 | for (Header header : exchange.getRequest().getHeaders().getHeader()) { |
---|
360 | if ("SOAPAction".equalsIgnoreCase(header.getKey())) { |
---|
361 | return header.getValue(); |
---|
362 | } |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | // if there is none, use the root element of the body |
---|
367 | try { |
---|
368 | if ((soapRequest.getSOAPBody() != null) && |
---|
369 | (soapRequest.getSOAPBody().getChildNodes() != null) && |
---|
370 | (soapRequest.getSOAPBody().getChildNodes().getLength() > 0)) |
---|
371 | { |
---|
372 | return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName(); |
---|
373 | } |
---|
374 | } |
---|
375 | catch (SOAPException e) { |
---|
376 | Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e); |
---|
377 | Console.logException(e); |
---|
378 | } |
---|
379 | |
---|
380 | return null; |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * removes unnecessary characters from the method name |
---|
385 | */ |
---|
386 | private String normalizeCalledMethod(String calledMethod) { |
---|
387 | if (calledMethod != null) { |
---|
388 | if (calledMethod.startsWith("\"") && calledMethod.endsWith("\"")) { |
---|
389 | calledMethod = calledMethod.substring(1, calledMethod.length() - 1); |
---|
390 | } |
---|
391 | if (calledMethod.startsWith("urn:")) { |
---|
392 | calledMethod = calledMethod.substring(4); |
---|
393 | } |
---|
394 | if (calledMethod.startsWith("http://")) { |
---|
395 | calledMethod = calledMethod.split("/")[calledMethod.split("/").length-1]; |
---|
396 | } |
---|
397 | } |
---|
398 | return calledMethod; |
---|
399 | } |
---|
400 | } |
---|