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