[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> |
---|
[1622] | 67 | * the name of the service; this is either the path or, if a path map is available |
---|
[1635] | 68 | * </p> |
---|
[1622] | 69 | */ |
---|
[1635] | 70 | private final String serviceName; |
---|
[1622] | 71 | |
---|
| 72 | /** |
---|
| 73 | * <p> |
---|
[1417] | 74 | * the human readable name of this event type |
---|
| 75 | * </p> |
---|
| 76 | */ |
---|
[1635] | 77 | private final String name; |
---|
[1417] | 78 | |
---|
| 79 | /** |
---|
| 80 | * <p> |
---|
| 81 | * initializes the event type with the represented HTTP exchange and the already extracted |
---|
| 82 | * SOAP request and response. |
---|
| 83 | * </p> |
---|
| 84 | * |
---|
| 85 | * @param exchange the represented HTTP exchange |
---|
| 86 | * @param soapRequest the already extracted SOAP request |
---|
| 87 | * @param soapResponse the already extracted SOAP response |
---|
[1622] | 88 | * @param urlNameMap used to map paths of servives to logical names |
---|
[1417] | 89 | */ |
---|
[1622] | 90 | public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) { |
---|
[1417] | 91 | super(exchange); |
---|
| 92 | this.soapRequest = soapRequest; |
---|
| 93 | this.soapResponse = soapResponse; |
---|
[1600] | 94 | this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest)); |
---|
[1417] | 95 | |
---|
| 96 | String path = null; |
---|
| 97 | |
---|
| 98 | if ((exchange.getRequest() != null) && (exchange.getRequest().getUrl() != null)) { |
---|
| 99 | try { |
---|
| 100 | path = new URL(exchange.getRequest().getUrl()).getPath(); |
---|
| 101 | } |
---|
| 102 | catch (MalformedURLException e) { |
---|
| 103 | // ignore and continue |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | StringBuffer nameBuffer = new StringBuffer("SOAPEvent"); |
---|
| 108 | |
---|
| 109 | boolean somethingAdded = false; |
---|
| 110 | |
---|
| 111 | if (path != null) { |
---|
| 112 | nameBuffer.append("("); |
---|
[1622] | 113 | if( urlNameMap==null ) { |
---|
| 114 | serviceName = path; |
---|
| 115 | } else { |
---|
| 116 | String value = urlNameMap.getProperty(path); |
---|
| 117 | if( value!=null ) { |
---|
| 118 | serviceName = value; |
---|
| 119 | } else { |
---|
| 120 | serviceName = path; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | nameBuffer.append(serviceName); |
---|
[1417] | 124 | somethingAdded = true; |
---|
[1622] | 125 | } else { |
---|
| 126 | serviceName = "NA"; |
---|
[1417] | 127 | } |
---|
| 128 | |
---|
| 129 | if (calledMethod != null) { |
---|
| 130 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
| 131 | nameBuffer.append(calledMethod); |
---|
| 132 | somethingAdded = true; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | String senderStr = HTTPUtils.toString(exchange.getSender()); |
---|
| 136 | String receiverStr = HTTPUtils.toString(exchange.getReceiver()); |
---|
| 137 | |
---|
| 138 | if ((senderStr != null) && (receiverStr != null)) { |
---|
| 139 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
| 140 | nameBuffer.append(senderStr); |
---|
| 141 | nameBuffer.append(" --> "); |
---|
| 142 | nameBuffer.append(receiverStr); |
---|
| 143 | somethingAdded = true; |
---|
| 144 | } |
---|
| 145 | else if (senderStr != null) { |
---|
| 146 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
| 147 | nameBuffer.append(senderStr); |
---|
| 148 | somethingAdded = true; |
---|
| 149 | } |
---|
| 150 | else if (receiverStr != null) { |
---|
| 151 | nameBuffer.append(somethingAdded ? ", " : "("); |
---|
| 152 | nameBuffer.append(receiverStr); |
---|
| 153 | somethingAdded = true; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | if (somethingAdded) { |
---|
| 157 | nameBuffer.append(")"); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | this.name = nameBuffer.toString(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * <p> |
---|
| 165 | * the SOAP method called in this request |
---|
| 166 | * </p> |
---|
| 167 | * |
---|
| 168 | * @return the SOAP method called in this request |
---|
| 169 | */ |
---|
| 170 | public String getCalledMethod() { |
---|
| 171 | return calledMethod; |
---|
| 172 | } |
---|
[1622] | 173 | |
---|
| 174 | /** |
---|
| 175 | * <p> |
---|
| 176 | * the name of the service called in this request |
---|
| 177 | * </p> |
---|
| 178 | * |
---|
| 179 | * @return the name of the service called in this request |
---|
| 180 | */ |
---|
| 181 | public String getServiceName() { |
---|
| 182 | return serviceName; |
---|
| 183 | } |
---|
[1417] | 184 | |
---|
| 185 | /** |
---|
| 186 | * @return the soapRequest |
---|
| 187 | */ |
---|
| 188 | public SOAPMessage getSoapRequest() { |
---|
| 189 | return soapRequest; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * @return the soapResponse |
---|
| 194 | */ |
---|
| 195 | public SOAPMessage getSoapResponse() { |
---|
| 196 | return soapResponse; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /* (non-Javadoc) |
---|
| 200 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName() |
---|
| 201 | */ |
---|
| 202 | @Override |
---|
| 203 | public String getName() { |
---|
| 204 | return name; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /* (non-Javadoc) |
---|
| 208 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#toString() |
---|
| 209 | */ |
---|
| 210 | @Override |
---|
| 211 | public String toString() { |
---|
| 212 | return name; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /* (non-Javadoc) |
---|
| 216 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object) |
---|
| 217 | */ |
---|
| 218 | @Override |
---|
| 219 | public boolean equals(Object obj) { |
---|
| 220 | if (this == obj) { |
---|
| 221 | return true; |
---|
| 222 | } |
---|
| 223 | else if (obj instanceof SOAPEventType) { |
---|
[1599] | 224 | if (!obj.getClass().isAssignableFrom(this.getClass())) { |
---|
| 225 | return false; |
---|
| 226 | } |
---|
| 227 | |
---|
[1417] | 228 | return |
---|
| 229 | super.equals(obj) && |
---|
[1635] | 230 | HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) && |
---|
| 231 | HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName); |
---|
[1417] | 232 | } |
---|
| 233 | else { |
---|
| 234 | return false; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /* (non-Javadoc) |
---|
| 239 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode() |
---|
| 240 | */ |
---|
| 241 | @Override |
---|
| 242 | public int hashCode() { |
---|
[1635] | 243 | int hashCode = super.hashCode(); |
---|
[1599] | 244 | if (calledMethod != null) { |
---|
[1635] | 245 | hashCode += calledMethod.hashCode(); |
---|
[1599] | 246 | } |
---|
[1635] | 247 | if( serviceName != null ) { |
---|
| 248 | hashCode += serviceName.hashCode(); |
---|
[1599] | 249 | } |
---|
[1635] | 250 | return hashCode; |
---|
[1417] | 251 | } |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * <p> |
---|
| 255 | * determines the name of the method called in a SOAP request either through the HTTP header |
---|
| 256 | * or through the SOAP body |
---|
| 257 | * </p> |
---|
| 258 | */ |
---|
| 259 | private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) { |
---|
| 260 | // first check for a header containing the SOAP action |
---|
| 261 | |
---|
| 262 | if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) && |
---|
| 263 | (exchange.getRequest().getHeaders().getHeader() != null)) |
---|
| 264 | { |
---|
| 265 | for (Header header : exchange.getRequest().getHeaders().getHeader()) { |
---|
| 266 | if ("SOAPAction".equalsIgnoreCase(header.getKey())) { |
---|
| 267 | return header.getValue(); |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | // if there is none, use the root element of the body |
---|
| 273 | try { |
---|
| 274 | if ((soapRequest.getSOAPBody() != null) && |
---|
| 275 | (soapRequest.getSOAPBody().getChildNodes() != null) && |
---|
| 276 | (soapRequest.getSOAPBody().getChildNodes().getLength() > 0)) |
---|
| 277 | { |
---|
| 278 | return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName(); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | catch (SOAPException e) { |
---|
| 282 | Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e); |
---|
| 283 | Console.logException(e); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | return null; |
---|
| 287 | } |
---|
[1600] | 288 | |
---|
| 289 | /** |
---|
| 290 | * removes unnecessary characters from the method name |
---|
| 291 | */ |
---|
| 292 | private String normalizeCalledMethod(String calledMethod) { |
---|
| 293 | if (calledMethod != null) { |
---|
| 294 | if (calledMethod.startsWith("\"") && calledMethod.endsWith("\"")) { |
---|
| 295 | calledMethod = calledMethod.substring(1, calledMethod.length() - 1); |
---|
| 296 | } |
---|
| 297 | if (calledMethod.startsWith("urn:")) { |
---|
| 298 | calledMethod = calledMethod.substring(4); |
---|
| 299 | } |
---|
[1884] | 300 | if (calledMethod.startsWith("http://")) { |
---|
| 301 | calledMethod = calledMethod.split("/")[calledMethod.split("/").length-1]; |
---|
| 302 | } |
---|
[1600] | 303 | } |
---|
| 304 | return calledMethod; |
---|
| 305 | } |
---|
[1417] | 306 | } |
---|