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

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