source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/eventcore/SOAPEventType.java @ 1635

Last change on this file since 1635 was 1635, checked in by sherbold, 10 years ago
  • fixed SOAP event types hashCode and equals method to account for the serviceName
  • added SimpleSOAPEventType that contains only the name of the called method and of the called service
  • added convertToSimpleSOAPEvent method to HTTPUtils
  • added tests for convertToSimpleSOAPEvent
File size: 9.0 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.http.eventcore;
16
17import java.net.MalformedURLException;
18import java.net.URL;
19import java.util.Properties;
20import java.util.logging.Level;
21
22import javax.xml.soap.SOAPException;
23import javax.xml.soap.SOAPMessage;
24
25import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
26import de.ugoe.cs.autoquest.plugin.http.logdata.Header;
27import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
28import 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 */
39public 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 service; this is either the path or, if a path map is available
68     * </p>
69     */
70    private final String serviceName;
71   
72    /**
73     * <p>
74     * the human readable name of this event type
75     * </p>
76     */
77    private final String name;
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
88     * @param urlNameMap   used to map paths of servives to logical names
89     */
90    public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) {
91        super(exchange);
92        this.soapRequest = soapRequest;
93        this.soapResponse = soapResponse;
94        this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest));
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("(");
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);
124            somethingAdded = true;
125        } else {
126            serviceName = "NA";
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    }
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    }
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) {
224            if (!obj.getClass().isAssignableFrom(this.getClass())) {
225                return false;
226            }
227           
228            return
229                super.equals(obj) &&
230                HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) &&
231                HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName);
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() {
243        int hashCode = super.hashCode();
244        if (calledMethod != null) {
245            hashCode += calledMethod.hashCode();
246        }
247        if( serviceName != null ) {
248            hashCode += serviceName.hashCode();
249        }
250        return hashCode;
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    }
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            }
300        }
301        return calledMethod;
302    }
303}
Note: See TracBrowser for help on using the repository browser.