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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.http.eventcore;
|
---|
16 |
|
---|
17 | import java.io.ByteArrayInputStream;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.io.InvalidObjectException;
|
---|
20 | import java.io.ObjectInputStream;
|
---|
21 | import java.io.ObjectOutputStream;
|
---|
22 |
|
---|
23 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
24 | import javax.xml.parsers.ParserConfigurationException;
|
---|
25 |
|
---|
26 | import org.w3c.dom.Element;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 |
|
---|
29 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
30 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
31 | import 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 | */
|
---|
41 | public 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 | if( soapMsgBody == null ) {
|
---|
192 | return null;
|
---|
193 | }
|
---|
194 | return createDOMElement(equalBodyMap.getRandom(this));
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * <p>
|
---|
199 | * returns the {@link EqualSOAPDataMap} associated with this event
|
---|
200 | * </p>
|
---|
201 | *
|
---|
202 | * @return the {@link EqualSOAPDataMap}
|
---|
203 | */
|
---|
204 | public EqualSOAPDataMap getEqualSOAPDataMap() {
|
---|
205 | return equalBodyMap;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public CallType getCallType() {
|
---|
209 | return callType;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * (non-Javadoc)
|
---|
214 | *
|
---|
215 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
216 | */
|
---|
217 | @Override
|
---|
218 | public String getName() {
|
---|
219 | return "(" + callType + ":" + clientName + "->" + serviceName + ", " + calledMethod + ")";
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * (non-Javadoc)
|
---|
224 | *
|
---|
225 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
|
---|
226 | */
|
---|
227 | @Override
|
---|
228 | public boolean equals(Object obj) {
|
---|
229 | if (this == obj) {
|
---|
230 | return true;
|
---|
231 | }
|
---|
232 | else if (obj instanceof SimpleSOAPEventType) {
|
---|
233 | boolean callTypesEqual = true;
|
---|
234 | if (callType == null) {
|
---|
235 | if (((SimpleSOAPEventType) obj).callType == null) {
|
---|
236 | callTypesEqual = true;
|
---|
237 | }
|
---|
238 | else {
|
---|
239 | callTypesEqual = false;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | else {
|
---|
243 | callTypesEqual = callType.equals(((SimpleSOAPEventType) obj).callType);
|
---|
244 | }
|
---|
245 | return callTypesEqual &&
|
---|
246 | HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
247 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
248 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
249 | }
|
---|
250 | else {
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * (non-Javadoc)
|
---|
257 | *
|
---|
258 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
|
---|
259 | */
|
---|
260 | @Override
|
---|
261 | public int hashCode() {
|
---|
262 | int hashCode = 13;
|
---|
263 | if (callType != null) {
|
---|
264 | hashCode += callType.hashCode();
|
---|
265 | }
|
---|
266 | if (calledMethod == null || serviceName == null || clientName == null) {
|
---|
267 | System.out.print("fu!");
|
---|
268 | }
|
---|
269 | return hashCode + calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
270 | }
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * (non-Javadoc)
|
---|
274 | *
|
---|
275 | * @see java.lang.Object#toString()
|
---|
276 | */
|
---|
277 | @Override
|
---|
278 | public String toString() {
|
---|
279 | return "SimpleSOAPEventType" + getName();
|
---|
280 | }
|
---|
281 |
|
---|
282 | private Element createDOMElement(String requestBody) {
|
---|
283 | if( requestBody==null ) {
|
---|
284 | return null;
|
---|
285 | }
|
---|
286 | try {
|
---|
287 | return DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
---|
288 | .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
|
---|
289 | }
|
---|
290 | catch (SAXException | IOException | ParserConfigurationException e) {
|
---|
291 | return null;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | private void writeObject(ObjectOutputStream out) throws IOException {
|
---|
296 | out.defaultWriteObject();
|
---|
297 | out.writeObject(equalBodyMap);
|
---|
298 | }
|
---|
299 |
|
---|
300 | private void readObject(ObjectInputStream in)
|
---|
301 | throws ClassNotFoundException, IOException
|
---|
302 | {
|
---|
303 | in.defaultReadObject();
|
---|
304 | equalBodyMap = (EqualSOAPDataMap) in.readObject();
|
---|
305 | if ( equalBodyMap == null) {
|
---|
306 | throw new InvalidObjectException("null");
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|