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

Last change on this file since 1924 was 1924, checked in by sherbold, 9 years ago
  • SimpleSOAPEvent now contains body of the SOAP request
  • SOAPUtils created
File size: 11.5 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 org.w3c.dom.Element;
26
27import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
28import de.ugoe.cs.autoquest.plugin.http.logdata.Header;
29import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
30import 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 */
41public final class SOAPEventType extends HTTPEventType {
42
43    /**  */
44    private static final long serialVersionUID = 1L;
45   
46    /**
47     * <p>
48     * the SOAP request belonging to the event
49     * </p>
50     */
51    transient private final SOAPMessage soapRequest;
52   
53    /**
54     * <p>
55     * the SOAP response belonging to the event
56     * </p>
57     */
58    transient private final SOAPMessage soapResponse;
59   
60    /**
61     * <p>
62     * the SOAP method called in this request
63     * </p>
64     */
65    private final String calledMethod;
66   
67    /**
68     * <p>
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
71     * </p>
72     */
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     */
81    private final String serviceName;
82   
83    /**
84     * <p>
85     * the human readable name of this event type
86     * </p>
87     */
88    private final String name;
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
99     * @param urlNameMap   used to map paths of servives to logical names
100     */
101    public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse, Properties urlNameMap) {
102        super(exchange);
103        this.soapRequest = soapRequest;
104        this.soapResponse = soapResponse;
105        this.calledMethod = normalizeCalledMethod(determineCalledMethod(exchange, soapRequest));
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       
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) {
135                key = "clientName.receiver.server." + exchange.getReceiver().getHost();
136                value = urlNameMap.getProperty(key);
137            }
138           
139            if (value == null) {
140                key += ":" + exchange.getReceiver().getPort();
141                value = urlNameMap.getProperty(key);
142            }
143           
144            if (value == null) {
145                key = "clientName.receiver.port." + exchange.getReceiver().getPort();
146                value = urlNameMap.getProperty(key);
147            }
148           
149            if (value == null) {
150                key = "clientName.default";
151                value = urlNameMap.getProperty(key);
152            }
153
154            if (value != null) {
155                clientName = value;
156            }
157            else {
158                clientName = exchange.getSender().getHost() + ":" + exchange.getSender().getPort();
159            }
160        }
161
162        nameBuffer.append("(");
163        nameBuffer.append(clientName);
164       
165        // determine the service name
166        if (path != null) {
167            nameBuffer.append(", ");
168            if (urlNameMap == null) {
169                serviceName = path;
170            }
171            else {
172                String value = urlNameMap.getProperty("serviceName.path." + path);
173                if (value != null) {
174                    serviceName = value;
175                }
176                else {
177                    serviceName = path;
178                }
179            }
180            nameBuffer.append(serviceName);
181        }
182        else {
183            serviceName = "NA";
184        }
185       
186        if (calledMethod != null) {
187            nameBuffer.append(", ");
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)) {
195            nameBuffer.append(", ");
196            nameBuffer.append(senderStr);
197            nameBuffer.append(" --> ");
198            nameBuffer.append(receiverStr);
199        }
200        else if (senderStr != null) {
201            nameBuffer.append(", ");
202            nameBuffer.append(senderStr);
203        }
204        else if (receiverStr != null) {
205            nameBuffer.append(", ");
206            nameBuffer.append(receiverStr);
207        }
208       
209        nameBuffer.append(")");
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    }
224   
225    /**
226     * <p>
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>
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    }
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    }
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    }
272
273    /* (non-Javadoc)
274     * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
275     */
276    @Override
277    public String getName() {
278        return name;
279    }
280
281    /* (non-Javadoc)
282     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#toString()
283     */
284    @Override
285    public String toString() {
286        return name;
287    }
288
289    /* (non-Javadoc)
290     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
291     */
292    @Override
293    public boolean equals(Object obj) {
294        if (this == obj) {
295            return true;
296        }
297        else if (obj instanceof SOAPEventType) {
298            if (!obj.getClass().isAssignableFrom(this.getClass())) {
299                return false;
300            }
301           
302            return
303                super.equals(obj) &&
304                HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod) &&
305                HTTPUtils.equals(serviceName, ((SOAPEventType) obj).serviceName) &&
306                HTTPUtils.equals(clientName, ((SOAPEventType) obj).clientName);
307        }
308        else {
309            return false;
310        }
311    }
312
313    /* (non-Javadoc)
314     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
315     */
316    @Override
317    public int hashCode() {
318        int hashCode = super.hashCode();
319        if (calledMethod != null) {
320            hashCode += calledMethod.hashCode();
321        }
322        if( serviceName != null ) {
323            hashCode += serviceName.hashCode();
324        }
325        if( clientName != null ) {
326            hashCode += clientName.hashCode();
327        }
328        return hashCode;
329    }
330
331    /**
332     * <p>
333     * determines the name of the method called in a SOAP request either through the HTTP header
334     * or through the SOAP body
335     * </p>
336     */
337    private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) {
338        // first check for a header containing the SOAP action
339       
340        if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) &&
341            (exchange.getRequest().getHeaders().getHeader() != null))
342        {
343            for (Header header : exchange.getRequest().getHeaders().getHeader()) {
344                if ("SOAPAction".equalsIgnoreCase(header.getKey())) {
345                    return header.getValue();
346                }
347            }
348        }
349       
350        // if there is none, use the root element of the body
351        try {
352            if ((soapRequest.getSOAPBody() != null) &&
353                (soapRequest.getSOAPBody().getChildNodes() != null) &&
354                (soapRequest.getSOAPBody().getChildNodes().getLength() > 0))
355            {
356                return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName();
357            }
358        }
359        catch (SOAPException e) {
360            Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e);
361            Console.logException(e);
362        }
363       
364        return null;
365    }
366   
367    /**
368     * removes unnecessary characters from the method name
369     */
370    private String normalizeCalledMethod(String calledMethod) {
371        if (calledMethod != null) {
372            if (calledMethod.startsWith("\"") && calledMethod.endsWith("\"")) {
373                calledMethod = calledMethod.substring(1, calledMethod.length() - 1);
374            }
375            if (calledMethod.startsWith("urn:")) {
376                calledMethod = calledMethod.substring(4);
377            }
378            if (calledMethod.startsWith("http://")) {
379                calledMethod = calledMethod.split("/")[calledMethod.split("/").length-1];
380            }
381        }
382        return calledMethod;
383    }
384}
Note: See TracBrowser for help on using the repository browser.