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

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