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

Last change on this file since 1993 was 1993, checked in by sherbold, 9 years ago
  • SimpleSOAPEventType can now either be a request or a response and contains the appropriate soap message body
  • SOAPUtils offer function to convert SOAPEventType into SimpleSOAPEventType and meanwhile split into request and response and sort the SimpleSOAPEvents in the appropriate order
  • Property svn:mime-type set to text/plain
File size: 8.7 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.io.ByteArrayInputStream;
18import java.io.IOException;
19
20import javax.xml.parsers.DocumentBuilderFactory;
21import javax.xml.parsers.ParserConfigurationException;
22
23import org.w3c.dom.Element;
24import org.xml.sax.SAXException;
25
26import de.ugoe.cs.autoquest.eventcore.IEventType;
27import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
28import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
29
30/**
31 * <p>
32 * A simplified SOAP event type that only contains the name of the called method and the name of the
33 * service.
34 * </p>
35 *
36 * @author Steffen Herbold
37 */
38public class SimpleSOAPEventType implements IEventType {
39
40    /**
41     * <p>
42     * Defines if this event represents an request or an response.
43     * </p>
44     *
45     * @author Steffen Herbold
46     */
47    public enum CallType {
48        REQUEST, RESPONSE
49    }
50
51    /**  */
52    private static final long serialVersionUID = 1L;
53
54    /**
55     * <p>
56     * the SOAP method called in this request
57     * </p>
58     */
59    private final String calledMethod;
60
61    /**
62     * <p>
63     * the name of the service; this is either the path or, if a path map is available
64     * </p>
65     */
66    private final String serviceName;
67
68    /**
69     * <p>
70     * the name of the client; this is either the host/ip and port of the sender or, if a path map
71     * is available, a human readable name that may be based also on the receiver
72     * </p>
73     */
74    private final String clientName;
75
76    /**
77     * <p>
78     * the body of the SOAP message; storage as serialized XML string to allow object serialization
79     * </p>
80     */
81    private final String soapMsgBody;
82
83    /**
84     * <p>
85     * defines whether this event represents a request or a response
86     * </p>
87     */
88    private final CallType callType;
89
90    /**
91     * reference to the {@link EqualSOAPDataMap} associated with the sequence this event belongs to.
92     */
93    private final EqualSOAPDataMap equalBodyMap;
94
95    /**
96     * <p>
97     * Constructor. Creates a new SimpleSOAPEventType.
98     * </p>
99     *
100     */
101    public SimpleSOAPEventType(String calledMethod,
102                               String serviceName,
103                               String clientName,
104                               Element soapMsgBody)
105    {
106        this(calledMethod, serviceName, clientName, soapMsgBody, null, CallType.REQUEST);
107    }
108
109    /**
110     * <p>
111     * Constructor. Creates a new SimpleSOAPEventType with a EqualSOAPDATAMap.
112     * </p>
113     *
114     */
115    public SimpleSOAPEventType(String calledMethod,
116                               String serviceName,
117                               String clientName,
118                               Element soapMsgBody,
119                               EqualSOAPDataMap equalRequestsMap)
120    {
121        this(calledMethod, serviceName, clientName, soapMsgBody, equalRequestsMap,
122             CallType.REQUEST);
123    }
124
125    /**
126     * <p>
127     * Constructor. Creates a new SimpleSOAPEventType with a EqualSOAPDATAMap.
128     * </p>
129     *
130     */
131    public SimpleSOAPEventType(String calledMethod,
132                               String serviceName,
133                               String clientName,
134                               Element soapMsgBody,
135                               EqualSOAPDataMap equalRequestsMap,
136                               CallType callType)
137    {
138        if (calledMethod == null) {
139            throw new IllegalArgumentException("called method must not be null");
140        }
141        if (serviceName == null) {
142            throw new IllegalArgumentException("serviceName must not be null");
143        }
144        if (clientName == null) {
145            throw new IllegalArgumentException("clientName must not be null");
146        }
147        this.calledMethod = calledMethod;
148        this.serviceName = serviceName;
149        this.clientName = clientName;
150        this.equalBodyMap = equalRequestsMap;
151        this.soapMsgBody = SOAPUtils.getSerialization(soapMsgBody);
152        this.callType = callType;
153
154        // this must be the last part of the constructor and only be called after all variables are
155        // initialized
156        if (equalRequestsMap != null) {
157            equalRequestsMap.add(this, this.soapMsgBody);
158        }
159    }
160
161    /**
162     * <p>
163     * the SOAP method called in this request
164     * </p>
165     *
166     * @return the SOAP method called in this request
167     */
168    public String getCalledMethod() {
169        return calledMethod;
170    }
171
172    /**
173     * <p>
174     * the name of the service called in this request
175     * </p>
176     *
177     * @return the name of the service called in this request
178     */
179    public String getServiceName() {
180        return serviceName;
181    }
182
183    /**
184     * <p>
185     * the name of the client calling in this request
186     * </p>
187     *
188     * @return the name of the client calling in this request
189     */
190    public String getClientName() {
191        return clientName;
192    }
193
194    /**
195     * <p>
196     * returns the body of the SOAP request; how the body is determined is defined by the
197     * {@link RequestBodyMode}.
198     * </p>
199     *
200     * @return body of the SOAP request
201     */
202    public Element getSoapMsgBody() {
203        return createDOMElement(soapMsgBody);
204    }
205
206    /**
207     * <p>
208     * returns a randomly draw request body for the called method using
209     * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}
210     * </p>
211     *
212     * @return randomly drawn body of the SOAP request
213     */
214    public Element getRandomSoapMsgBody() {
215        if (equalBodyMap == null) {
216            throw new RuntimeException(
217                                       "cannot use random mode is no request map is available; different data missing");
218        }
219        return createDOMElement(equalBodyMap.getRandom(this));
220    }
221
222    /**
223     * <p>
224     * returns the {@link EqualSOAPDataMap} associated with this event
225     * </p>
226     *
227     * @return the {@link EqualSOAPDataMap}
228     */
229    public EqualSOAPDataMap getEqualSOAPDataMap() {
230        return equalBodyMap;
231    }
232
233    public CallType getCallType() {
234        return callType;
235    }
236
237    /*
238     * (non-Javadoc)
239     *
240     * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
241     */
242    @Override
243    public String getName() {
244        return "(" + callType + ":" + clientName + "->" + serviceName + ", " + calledMethod + ")";
245    }
246
247    /*
248     * (non-Javadoc)
249     *
250     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
251     */
252    @Override
253    public boolean equals(Object obj) {
254        if (this == obj) {
255            return true;
256        }
257        else if (obj instanceof SimpleSOAPEventType) {
258            return callType.equals(((SimpleSOAPEventType) obj).callType) &&
259                HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
260                HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
261                HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
262        }
263        else {
264            return false;
265        }
266    }
267
268    /*
269     * (non-Javadoc)
270     *
271     * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
272     */
273    @Override
274    public int hashCode() {
275        return callType.hashCode() + calledMethod.hashCode() + serviceName.hashCode() +
276            clientName.hashCode();
277    }
278
279    /*
280     * (non-Javadoc)
281     *
282     * @see java.lang.Object#toString()
283     */
284    @Override
285    public String toString() {
286        return "SimpleSOAPEventType" + getName();
287    }
288
289    private Element createDOMElement(String requestBody) {
290        try {
291            return DocumentBuilderFactory.newInstance().newDocumentBuilder()
292                .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
293        }
294        catch (SAXException | IOException | ParserConfigurationException e) {
295            return null;
296        }
297    }
298}
Note: See TracBrowser for help on using the repository browser.