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