[1417] | 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; |
---|
[1622] | 19 | import java.util.Properties; |
---|
[1417] | 20 | import java.util.logging.Level; |
---|
| 21 | |
---|
| 22 | import javax.xml.soap.SOAPException; |
---|
| 23 | import javax.xml.soap.SOAPMessage; |
---|
| 24 | |
---|
[1924] | 25 | import org.w3c.dom.Element; |
---|
| 26 | |
---|
[1417] | 27 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
[1561] | 28 | import de.ugoe.cs.autoquest.plugin.http.logdata.Header; |
---|
| 29 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
[1417] | 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 | */ |
---|
[1599] | 41 | public final class SOAPEventType extends HTTPEventType { |
---|
[1417] | 42 | |
---|
| 43 | /** */ |
---|
| 44 | private static final long serialVersionUID = 1L; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * <p> |
---|
| 48 | * the SOAP request belonging to the event |
---|
| 49 | * </p> |
---|
| 50 | */ |
---|
[1635] | 51 | transient private final SOAPMessage soapRequest; |
---|
[1417] | 52 | |
---|
| 53 | /** |
---|
| 54 | * <p> |
---|
| 55 | * the SOAP response belonging to the event |
---|
| 56 | * </p> |
---|
| 57 | */ |
---|
[1635] | 58 | transient private final SOAPMessage soapResponse; |
---|
[1417] | 59 | |
---|
| 60 | /** |
---|
| 61 | * <p> |
---|
| 62 | * the SOAP method called in this request |
---|
| 63 | * </p> |
---|
| 64 | */ |
---|
[1635] | 65 | private final String calledMethod; |
---|
[1417] | 66 | |
---|
| 67 | /** |
---|
| 68 | * <p> |
---|
[1906] | 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 |
---|
[1635] | 71 | * </p> |
---|
[1622] | 72 | */ |
---|
[1906] | 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 | */ |
---|
[1635] | 81 | private final String serviceName; |
---|
[1622] | 82 | |
---|
| 83 | /** |
---|
| 84 | * <p> |
---|
[1417] | 85 | * the human readable name of this event type |
---|
| 86 | * </p> |
---|
| 87 | */ |
---|
[1635] | 88 | private final String name; |
---|
[1417] | 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 |
---|
[1622] | 99 | * @param urlNameMap used to map paths of servives to logical names |
---|
[1417] | 100 | */ |
---|
[1622] | 101 | public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) { |
---|
[1417] | 102 | super(exchange); |
---|
| 103 | this.soapRequest = soapRequest; |
---|
| 104 | this.soapResponse = soapResponse; |
---|
[1600] | 105 | this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest)); |
---|
[1417] | 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 | |
---|
[1906] | 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) { |
---|
[1910] | 135 | key = "clientName.receiver.server." + exchange.getReceiver().getHost(); |
---|
[1906] | 136 | value = urlNameMap.getProperty(key); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | if (value == null) { |
---|
| 140 | key += ":" + exchange.getReceiver().getPort(); |
---|
| 141 | value = urlNameMap.getProperty(key); |
---|
| 142 | } |
---|
| 143 | |
---|
[1910] | 144 | if (value == null) { |
---|
| 145 | key = "clientName.receiver.port." + exchange.getReceiver().getPort(); |
---|
| 146 | value = urlNameMap.getProperty(key); |
---|
| 147 | } |
---|
| 148 | |
---|
[1912] | 149 | if (value == null) { |
---|
| 150 | key = "clientName.default"; |
---|
| 151 | value = urlNameMap.getProperty(key); |
---|
| 152 | } |
---|
| 153 | |
---|
[1906] | 154 | if (value != null) { |
---|
| 155 | clientName = value; |
---|
| 156 | } |
---|
| 157 | else { |
---|
| 158 | clientName = exchange.getSender().getHost() + ":" + exchange.getSender().getPort(); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
[1907] | 162 | nameBuffer.append("("); |
---|
[1906] | 163 | nameBuffer.append(clientName); |
---|
[1417] | 164 | |
---|
[1906] | 165 | // determine the service name |
---|
[1417] | 166 | if (path != null) { |
---|
[1906] | 167 | nameBuffer.append(", "); |
---|
| 168 | if (urlNameMap == null) { |
---|
[1622] | 169 | serviceName = path; |
---|
[1906] | 170 | } |
---|
| 171 | else { |
---|
| 172 | String value = urlNameMap.getProperty("serviceName.path." + path); |
---|
| 173 | if (value != null) { |
---|
[1622] | 174 | serviceName = value; |
---|
[1906] | 175 | } |
---|
| 176 | else { |
---|
[1622] | 177 | serviceName = path; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | nameBuffer.append(serviceName); |
---|
[1906] | 181 | } |
---|
| 182 | else { |
---|
[1622] | 183 | serviceName = "NA"; |
---|
[1417] | 184 | } |
---|
| 185 | |
---|
| 186 | if (calledMethod != null) { |
---|
[1906] | 187 | nameBuffer.append(", "); |
---|
[1417] | 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)) { |
---|
[1906] | 195 | nameBuffer.append(", "); |
---|
[1417] | 196 | nameBuffer.append(senderStr); |
---|
| 197 | nameBuffer.append(" --> "); |
---|
| 198 | nameBuffer.append(receiverStr); |
---|
| 199 | } |
---|
| 200 | else if (senderStr != null) { |
---|
[1906] | 201 | nameBuffer.append(", "); |
---|
[1417] | 202 | nameBuffer.append(senderStr); |
---|
| 203 | } |
---|
| 204 | else if (receiverStr != null) { |
---|
[1906] | 205 | nameBuffer.append(", "); |
---|
[1417] | 206 | nameBuffer.append(receiverStr); |
---|
| 207 | } |
---|
| 208 | |
---|
[1906] | 209 | nameBuffer.append(")"); |
---|
[1417] | 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 | } |
---|
[1622] | 224 | |
---|
| 225 | /** |
---|
| 226 | * <p> |
---|
[1906] | 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> |
---|
[1622] | 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 | } |
---|
[1417] | 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 | } |
---|
[1924] | 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 | } |
---|
[1993] | 272 | |
---|
| 273 | /** |
---|
| 274 | * @return the body of the soapResponse |
---|
| 275 | */ |
---|
| 276 | public Element getSoapResponseBody() { |
---|
| 277 | try { |
---|
[2037] | 278 | if( soapResponse!=null ) { |
---|
| 279 | return soapResponse.getSOAPBody(); |
---|
| 280 | } else { |
---|
| 281 | return null; |
---|
| 282 | } |
---|
[1993] | 283 | } |
---|
| 284 | catch (SOAPException e) { |
---|
| 285 | return null; |
---|
| 286 | } |
---|
| 287 | } |
---|
[1417] | 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) { |
---|
[1599] | 314 | if (!obj.getClass().isAssignableFrom(this.getClass())) { |
---|
| 315 | return false; |
---|
| 316 | } |
---|
| 317 | |
---|
[1417] | 318 | return |
---|
| 319 | super.equals(obj) && |
---|
[1635] | 320 | HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) && |
---|
[1906] | 321 | HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName) && |
---|
| 322 | HTTPUtils.equals(clientName, ((SOAPEventType) obj).clientName); |
---|
[1417] | 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() { |
---|
[1635] | 334 | int hashCode = super.hashCode(); |
---|
[1599] | 335 | if (calledMethod != null) { |
---|
[1635] | 336 | hashCode += calledMethod.hashCode(); |
---|
[1599] | 337 | } |
---|
[1635] | 338 | if( serviceName != null ) { |
---|
| 339 | hashCode += serviceName.hashCode(); |
---|
[1599] | 340 | } |
---|
[1906] | 341 | if( clientName != null ) { |
---|
| 342 | hashCode += clientName.hashCode(); |
---|
| 343 | } |
---|
[1635] | 344 | return hashCode; |
---|
[1417] | 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 | } |
---|
[1600] | 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 | } |
---|
[1884] | 394 | if (calledMethod.startsWith("http://")) { |
---|
| 395 | calledMethod = calledMethod.split("/")[calledMethod.split("/").length-1]; |
---|
| 396 | } |
---|
[1600] | 397 | } |
---|
| 398 | return calledMethod; |
---|
| 399 | } |
---|
[1417] | 400 | } |
---|