[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 | |
---|
| 25 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils; |
---|
[1561] | 26 | import de.ugoe.cs.autoquest.plugin.http.logdata.Header; |
---|
| 27 | import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange; |
---|
[1417] | 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 | */ |
---|
[1599] | 39 | public final class SOAPEventType extends HTTPEventType { |
---|
[1417] | 40 | |
---|
| 41 | /** */ |
---|
| 42 | private static final long serialVersionUID = 1L; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * <p> |
---|
| 46 | * the SOAP request belonging to the event |
---|
| 47 | * </p> |
---|
| 48 | */ |
---|
[1635] | 49 | transient private final SOAPMessage soapRequest; |
---|
[1417] | 50 | |
---|
| 51 | /** |
---|
| 52 | * <p> |
---|
| 53 | * the SOAP response belonging to the event |
---|
| 54 | * </p> |
---|
| 55 | */ |
---|
[1635] | 56 | transient private final SOAPMessage soapResponse; |
---|
[1417] | 57 | |
---|
| 58 | /** |
---|
| 59 | * <p> |
---|
| 60 | * the SOAP method called in this request |
---|
| 61 | * </p> |
---|
| 62 | */ |
---|
[1635] | 63 | private final String calledMethod; |
---|
[1417] | 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
[1906] | 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 |
---|
[1635] | 69 | * </p> |
---|
[1622] | 70 | */ |
---|
[1906] | 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 | */ |
---|
[1635] | 79 | private final String serviceName; |
---|
[1622] | 80 | |
---|
| 81 | /** |
---|
| 82 | * <p> |
---|
[1417] | 83 | * the human readable name of this event type |
---|
| 84 | * </p> |
---|
| 85 | */ |
---|
[1635] | 86 | private final String name; |
---|
[1417] | 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 |
---|
[1622] | 97 | * @param urlNameMap used to map paths of servives to logical names |
---|
[1417] | 98 | */ |
---|
[1622] | 99 | public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) { |
---|
[1417] | 100 | super(exchange); |
---|
| 101 | this.soapRequest = soapRequest; |
---|
| 102 | this.soapResponse = soapResponse; |
---|
[1600] | 103 | this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest)); |
---|
[1417] | 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 | |
---|
[1906] | 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) { |
---|
[1910] | 133 | key = "clientName.receiver.server." + exchange.getReceiver().getHost(); |
---|
[1906] | 134 | value = urlNameMap.getProperty(key); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if (value == null) { |
---|
| 138 | key += ":" + exchange.getReceiver().getPort(); |
---|
| 139 | value = urlNameMap.getProperty(key); |
---|
| 140 | } |
---|
| 141 | |
---|
[1910] | 142 | if (value == null) { |
---|
| 143 | key = "clientName.receiver.port." + exchange.getReceiver().getPort(); |
---|
| 144 | value = urlNameMap.getProperty(key); |
---|
| 145 | } |
---|
| 146 | |
---|
[1912] | 147 | if (value == null) { |
---|
| 148 | key = "clientName.default"; |
---|
| 149 | value = urlNameMap.getProperty(key); |
---|
| 150 | } |
---|
| 151 | |
---|
[1906] | 152 | if (value != null) { |
---|
| 153 | clientName = value; |
---|
| 154 | } |
---|
| 155 | else { |
---|
| 156 | clientName = exchange.getSender().getHost() + ":" + exchange.getSender().getPort(); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[1907] | 160 | nameBuffer.append("("); |
---|
[1906] | 161 | nameBuffer.append(clientName); |
---|
[1417] | 162 | |
---|
[1906] | 163 | // determine the service name |
---|
[1417] | 164 | if (path != null) { |
---|
[1906] | 165 | nameBuffer.append(", "); |
---|
| 166 | if (urlNameMap == null) { |
---|
[1622] | 167 | serviceName = path; |
---|
[1906] | 168 | } |
---|
| 169 | else { |
---|
| 170 | String value = urlNameMap.getProperty("serviceName.path." + path); |
---|
| 171 | if (value != null) { |
---|
[1622] | 172 | serviceName = value; |
---|
[1906] | 173 | } |
---|
| 174 | else { |
---|
[1622] | 175 | serviceName = path; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | nameBuffer.append(serviceName); |
---|
[1906] | 179 | } |
---|
| 180 | else { |
---|
[1622] | 181 | serviceName = "NA"; |
---|
[1417] | 182 | } |
---|
| 183 | |
---|
| 184 | if (calledMethod != null) { |
---|
[1906] | 185 | nameBuffer.append(", "); |
---|
[1417] | 186 | nameBuffer.append(calledMethod); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | String senderStr = HTTPUtils.toString(exchange.getSender()); |
---|
| 190 | String receiverStr = HTTPUtils.toString(exchange.getReceiver()); |
---|
| 191 | |
---|
| 192 | if ((senderStr != null) && (receiverStr != null)) { |
---|
[1906] | 193 | nameBuffer.append(", "); |
---|
[1417] | 194 | nameBuffer.append(senderStr); |
---|
| 195 | nameBuffer.append(" --> "); |
---|
| 196 | nameBuffer.append(receiverStr); |
---|
| 197 | } |
---|
| 198 | else if (senderStr != null) { |
---|
[1906] | 199 | nameBuffer.append(", "); |
---|
[1417] | 200 | nameBuffer.append(senderStr); |
---|
| 201 | } |
---|
| 202 | else if (receiverStr != null) { |
---|
[1906] | 203 | nameBuffer.append(", "); |
---|
[1417] | 204 | nameBuffer.append(receiverStr); |
---|
| 205 | } |
---|
| 206 | |
---|
[1906] | 207 | nameBuffer.append(")"); |
---|
[1417] | 208 | |
---|
| 209 | this.name = nameBuffer.toString(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * <p> |
---|
| 214 | * the SOAP method called in this request |
---|
| 215 | * </p> |
---|
| 216 | * |
---|
| 217 | * @return the SOAP method called in this request |
---|
| 218 | */ |
---|
| 219 | public String getCalledMethod() { |
---|
| 220 | return calledMethod; |
---|
| 221 | } |
---|
[1622] | 222 | |
---|
| 223 | /** |
---|
| 224 | * <p> |
---|
[1906] | 225 | * the name of the client calling in this request |
---|
| 226 | * </p> |
---|
| 227 | * |
---|
| 228 | * @return the name of the client calling in this request |
---|
| 229 | */ |
---|
| 230 | public String getClientName() { |
---|
| 231 | return clientName; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * <p> |
---|
[1622] | 236 | * the name of the service called in this request |
---|
| 237 | * </p> |
---|
| 238 | * |
---|
| 239 | * @return the name of the service called in this request |
---|
| 240 | */ |
---|
| 241 | public String getServiceName() { |
---|
| 242 | return serviceName; |
---|
| 243 | } |
---|
[1417] | 244 | |
---|
| 245 | /** |
---|
| 246 | * @return the soapRequest |
---|
| 247 | */ |
---|
| 248 | public SOAPMessage getSoapRequest() { |
---|
| 249 | return soapRequest; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * @return the soapResponse |
---|
| 254 | */ |
---|
| 255 | public SOAPMessage getSoapResponse() { |
---|
| 256 | return soapResponse; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /* (non-Javadoc) |
---|
| 260 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName() |
---|
| 261 | */ |
---|
| 262 | @Override |
---|
| 263 | public String getName() { |
---|
| 264 | return name; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /* (non-Javadoc) |
---|
| 268 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#toString() |
---|
| 269 | */ |
---|
| 270 | @Override |
---|
| 271 | public String toString() { |
---|
| 272 | return name; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /* (non-Javadoc) |
---|
| 276 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object) |
---|
| 277 | */ |
---|
| 278 | @Override |
---|
| 279 | public boolean equals(Object obj) { |
---|
| 280 | if (this == obj) { |
---|
| 281 | return true; |
---|
| 282 | } |
---|
| 283 | else if (obj instanceof SOAPEventType) { |
---|
[1599] | 284 | if (!obj.getClass().isAssignableFrom(this.getClass())) { |
---|
| 285 | return false; |
---|
| 286 | } |
---|
| 287 | |
---|
[1417] | 288 | return |
---|
| 289 | super.equals(obj) && |
---|
[1635] | 290 | HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) && |
---|
[1906] | 291 | HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName) && |
---|
| 292 | HTTPUtils.equals(clientName, ((SOAPEventType) obj).clientName); |
---|
[1417] | 293 | } |
---|
| 294 | else { |
---|
| 295 | return false; |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /* (non-Javadoc) |
---|
| 300 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode() |
---|
| 301 | */ |
---|
| 302 | @Override |
---|
| 303 | public int hashCode() { |
---|
[1635] | 304 | int hashCode = super.hashCode(); |
---|
[1599] | 305 | if (calledMethod != null) { |
---|
[1635] | 306 | hashCode += calledMethod.hashCode(); |
---|
[1599] | 307 | } |
---|
[1635] | 308 | if( serviceName != null ) { |
---|
| 309 | hashCode += serviceName.hashCode(); |
---|
[1599] | 310 | } |
---|
[1906] | 311 | if( clientName != null ) { |
---|
| 312 | hashCode += clientName.hashCode(); |
---|
| 313 | } |
---|
[1635] | 314 | return hashCode; |
---|
[1417] | 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * <p> |
---|
| 319 | * determines the name of the method called in a SOAP request either through the HTTP header |
---|
| 320 | * or through the SOAP body |
---|
| 321 | * </p> |
---|
| 322 | */ |
---|
| 323 | private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) { |
---|
| 324 | // first check for a header containing the SOAP action |
---|
| 325 | |
---|
| 326 | if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) && |
---|
| 327 | (exchange.getRequest().getHeaders().getHeader() != null)) |
---|
| 328 | { |
---|
| 329 | for (Header header : exchange.getRequest().getHeaders().getHeader()) { |
---|
| 330 | if ("SOAPAction".equalsIgnoreCase(header.getKey())) { |
---|
| 331 | return header.getValue(); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | // if there is none, use the root element of the body |
---|
| 337 | try { |
---|
| 338 | if ((soapRequest.getSOAPBody() != null) && |
---|
| 339 | (soapRequest.getSOAPBody().getChildNodes() != null) && |
---|
| 340 | (soapRequest.getSOAPBody().getChildNodes().getLength() > 0)) |
---|
| 341 | { |
---|
| 342 | return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName(); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | catch (SOAPException e) { |
---|
| 346 | Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e); |
---|
| 347 | Console.logException(e); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | return null; |
---|
| 351 | } |
---|
[1600] | 352 | |
---|
| 353 | /** |
---|
| 354 | * removes unnecessary characters from the method name |
---|
| 355 | */ |
---|
| 356 | private String normalizeCalledMethod(String calledMethod) { |
---|
| 357 | if (calledMethod != null) { |
---|
| 358 | if (calledMethod.startsWith("\"") && calledMethod.endsWith("\"")) { |
---|
| 359 | calledMethod = calledMethod.substring(1, calledMethod.length() - 1); |
---|
| 360 | } |
---|
| 361 | if (calledMethod.startsWith("urn:")) { |
---|
| 362 | calledMethod = calledMethod.substring(4); |
---|
| 363 | } |
---|
[1884] | 364 | if (calledMethod.startsWith("http://")) { |
---|
| 365 | calledMethod = calledMethod.split("/")[calledMethod.split("/").length-1]; |
---|
| 366 | } |
---|
[1600] | 367 | } |
---|
| 368 | return calledMethod; |
---|
| 369 | } |
---|
[1417] | 370 | } |
---|