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

Last change on this file since 1591 was 1591, checked in by sherbold, 10 years ago
  • made the soapRequest and soapResponse fields of the SOAPEventType transient. It is as of now unclear whether we need them as part of Serialization and my be changed later. This should, at least, not affect the equals and hashCode operations, as both ignore the soapRequest and soapResponse fields.
File size: 7.2 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 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            return
193                super.equals(obj) &&
194                HTTPUtils.equals(calledMethod, ((SOAPEventType) obj).calledMethod);
195        }
196        else {
197            return false;
198        }
199    }
200
201    /* (non-Javadoc)
202     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
203     */
204    @Override
205    public int hashCode() {
206        return super.hashCode() + calledMethod.hashCode();
207    }
208
209    /**
210     * <p>
211     * determines the name of the method called in a SOAP request either through the HTTP header
212     * or through the SOAP body
213     * </p>
214     */
215    private String determineCalledMethod(HttpExchange exchange, SOAPMessage soapRequest) {
216        // first check for a header containing the SOAP action
217       
218        if ((exchange.getRequest() != null) && (exchange.getRequest().getHeaders() != null) &&
219            (exchange.getRequest().getHeaders().getHeader() != null))
220        {
221            for (Header header : exchange.getRequest().getHeaders().getHeader()) {
222                if ("SOAPAction".equalsIgnoreCase(header.getKey())) {
223                    return header.getValue();
224                }
225            }
226        }
227       
228        // if there is none, use the root element of the body
229        try {
230            if ((soapRequest.getSOAPBody() != null) &&
231                (soapRequest.getSOAPBody().getChildNodes() != null) &&
232                (soapRequest.getSOAPBody().getChildNodes().getLength() > 0))
233            {
234                return soapRequest.getSOAPBody().getChildNodes().item(0).getNodeName();
235            }
236        }
237        catch (SOAPException e) {
238            Console.traceln(Level.WARNING, "could not process SOAP message correctly: " + e);
239            Console.logException(e);
240        }
241       
242        return null;
243    }
244}
Note: See TracBrowser for help on using the repository browser.