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

Last change on this file since 1599 was 1599, checked in by pharms, 10 years ago
  • equals check for SOAP events
File size: 7.4 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.logging.Level;
20
21import javax.xml.soap.SOAPException;
22import javax.xml.soap.SOAPMessage;
23
24import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
25import de.ugoe.cs.autoquest.plugin.http.logdata.Header;
26import de.ugoe.cs.autoquest.plugin.http.logdata.HttpExchange;
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * represents the specific HTTP event for a SOAP message exchange. It contains the SOAP request
32 * and response envelopes and is more concrete when comparing object. E.g., it considers the
33 * called SOAP operation on performing comparison.
34 * </p>
35 *
36 * @author Patrick Harms
37 */
38public final class SOAPEventType extends HTTPEventType {
39
40    /**  */
41    private static final long serialVersionUID = 1L;
42   
43    /**
44     * <p>
45     * the SOAP request belonging to the event
46     * </p>
47     */
48    transient private SOAPMessage soapRequest;
49   
50    /**
51     * <p>
52     * the SOAP response belonging to the event
53     * </p>
54     */
55    transient private SOAPMessage soapResponse;
56   
57    /**
58     * <p>
59     * the SOAP method called in this request
60     * </p>
61     */
62    private String calledMethod;
63   
64    /**
65     * <p>
66     * the human readable name of this event type
67     * </p>
68     */
69    private String name;
70
71    /**
72     * <p>
73     * initializes the event type with the represented HTTP exchange and the already extracted
74     * SOAP request and response.
75     * </p>
76     *
77     * @param exchange     the represented HTTP exchange
78     * @param soapRequest  the already extracted SOAP request
79     * @param soapResponse the already extracted SOAP response
80     */
81    public SOAPEventType(HttpExchange exchange, SOAPMessage soapRequest, SOAPMessage soapResponse) {
82        super(exchange);
83        this.soapRequest = soapRequest;
84        this.soapResponse = soapResponse;
85        this.calledMethod = determineCalledMethod(exchange, soapRequest);
86       
87        String path = null;
88       
89        if ((exchange.getRequest() != null) && (exchange.getRequest().getUrl() != null)) {
90            try {
91                path = new URL(exchange.getRequest().getUrl()).getPath();
92            }
93            catch (MalformedURLException e) {
94                // ignore and continue
95            }
96        }
97       
98        StringBuffer nameBuffer = new StringBuffer("SOAPEvent");
99       
100        boolean somethingAdded = false;
101       
102        if (path != null) {
103            nameBuffer.append("(");
104            nameBuffer.append(path);
105            somethingAdded = true;
106        }
107       
108        if (calledMethod != null) {
109            nameBuffer.append(somethingAdded ? ", " : "(");
110            nameBuffer.append(calledMethod);
111            somethingAdded = true;
112        }
113       
114        String senderStr = HTTPUtils.toString(exchange.getSender());
115        String receiverStr = HTTPUtils.toString(exchange.getReceiver());
116       
117        if ((senderStr != null) && (receiverStr != null)) {
118            nameBuffer.append(somethingAdded ? ", " : "(");
119            nameBuffer.append(senderStr);
120            nameBuffer.append(" --> ");
121            nameBuffer.append(receiverStr);
122            somethingAdded = true;
123        }
124        else if (senderStr != null) {
125            nameBuffer.append(somethingAdded ? ", " : "(");
126            nameBuffer.append(senderStr);
127            somethingAdded = true;
128        }
129        else if (receiverStr != null) {
130            nameBuffer.append(somethingAdded ? ", " : "(");
131            nameBuffer.append(receiverStr);
132            somethingAdded = true;
133        }
134       
135        if (somethingAdded) {
136            nameBuffer.append(")");
137        }
138       
139        this.name = nameBuffer.toString();
140    }
141
142    /**
143     * <p>
144     * the SOAP method called in this request
145     * </p>
146     *
147     * @return the SOAP method called in this request
148     */
149    public String getCalledMethod() {
150        return calledMethod;
151    }
152
153    /**
154     * @return the soapRequest
155     */
156    public SOAPMessage getSoapRequest() {
157        return soapRequest;
158    }
159
160    /**
161     * @return the soapResponse
162     */
163    public SOAPMessage getSoapResponse() {
164        return soapResponse;
165    }
166
167    /* (non-Javadoc)
168     * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
169     */
170    @Override
171    public String getName() {
172        return name;
173    }
174
175    /* (non-Javadoc)
176     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#toString()
177     */
178    @Override
179    public String toString() {
180        return name;
181    }
182
183    /* (non-Javadoc)
184     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
185     */
186    @Override
187    public boolean equals(Object obj) {
188        if (this == obj) {
189            return true;
190        }
191        else if (obj instanceof SOAPEventType) {
192            if (!obj.getClass().isAssignableFrom(this.getClass())) {
193                return false;
194            }
195           
196            return
197                super.equals(obj) &&
198                HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod);
199        }
200        else {
201            return false;
202        }
203    }
204
205    /* (non-Javadoc)
206     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
207     */
208    @Override
209    public int hashCode() {
210        if (calledMethod != null) {
211            return super.hashCode() + calledMethod.hashCode();
212        }
213        else {
214            return super.hashCode();
215        }
216    }
217
218    /**
219     * <p>
220     * determines the name of the method called in a SOAP request either through the HTTP header
221     * or through the SOAP body
222     * </p>
223     */
224    private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) {
225        // first check for a header containing the SOAP action
226       
227        if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) &&
228            (exchange.getRequest().getHeaders().getHeader() != null))
229        {
230            for (Header header : exchange.getRequest().getHeaders().getHeader()) {
231                if ("SOAPAction".equalsIgnoreCase(header.getKey())) {
232                    return header.getValue();
233                }
234            }
235        }
236       
237        // if there is none, use the root element of the body
238        try {
239            if ((soapRequest.getSOAPBody() != null) &&
240                (soapRequest.getSOAPBody().getChildNodes() != null) &&
241                (soapRequest.getSOAPBody().getChildNodes().getLength() > 0))
242            {
243                return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName();
244            }
245        }
246        catch (SOAPException e) {
247            Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e);
248            Console.logException(e);
249        }
250       
251        return null;
252    }
253}
Note: See TracBrowser for help on using the repository browser.