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 |
|
---|
20 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
21 | import javax.xml.parsers.ParserConfigurationException;
|
---|
22 |
|
---|
23 | import org.w3c.dom.Element;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
27 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
28 | import 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 | */
|
---|
38 | public class SimpleSOAPEventType implements IEventType {
|
---|
39 |
|
---|
40 | /** */
|
---|
41 | private static final long serialVersionUID = 1L;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * <p>
|
---|
45 | * the SOAP method called in this request
|
---|
46 | * </p>
|
---|
47 | */
|
---|
48 | private final String calledMethod;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * <p>
|
---|
52 | * the name of the service; this is either the path or, if a path map is available
|
---|
53 | * </p>
|
---|
54 | */
|
---|
55 | private final String serviceName;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * the name of the client; this is either the host/ip and port of the sender or, if a path map
|
---|
60 | * is available, a human readable name that may be based also on the receiver
|
---|
61 | * </p>
|
---|
62 | */
|
---|
63 | private final String clientName;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * <p>
|
---|
67 | * the body of the SOAP request; storage as serialized XML string to allow object serialization
|
---|
68 | * </p>
|
---|
69 | */
|
---|
70 | private final String soapRequestBody;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * TODO
|
---|
74 | */
|
---|
75 | private final EqualSOAPDataMap equalRequestsMap;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * <p>
|
---|
79 | * Constructor. Creates a new SimpleSOAPEventType.
|
---|
80 | * </p>
|
---|
81 | *
|
---|
82 | */
|
---|
83 | public SimpleSOAPEventType(String calledMethod,
|
---|
84 | String serviceName,
|
---|
85 | String clientName,
|
---|
86 | Element soapRequestBody)
|
---|
87 | {
|
---|
88 | this(calledMethod, serviceName, clientName, soapRequestBody, null);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * <p>
|
---|
93 | * Constructor. Creates a new SimpleSOAPEventType with a EqualSOAPDATAMap.
|
---|
94 | * </p>
|
---|
95 | *
|
---|
96 | */
|
---|
97 | public SimpleSOAPEventType(String calledMethod,
|
---|
98 | String serviceName,
|
---|
99 | String clientName,
|
---|
100 | Element soapRequestBody,
|
---|
101 | EqualSOAPDataMap equalRequestsMap)
|
---|
102 | {
|
---|
103 | if (calledMethod == null) {
|
---|
104 | throw new IllegalArgumentException("called method must not be null");
|
---|
105 | }
|
---|
106 | if (serviceName == null) {
|
---|
107 | throw new IllegalArgumentException("serviceName must not be null");
|
---|
108 | }
|
---|
109 | if (clientName == null) {
|
---|
110 | throw new IllegalArgumentException("clientName must not be null");
|
---|
111 | }
|
---|
112 | this.calledMethod = calledMethod;
|
---|
113 | this.serviceName = serviceName;
|
---|
114 | this.clientName = clientName;
|
---|
115 | this.soapRequestBody = SOAPUtils.getSerialization(soapRequestBody);
|
---|
116 | this.equalRequestsMap = equalRequestsMap;
|
---|
117 | if( equalRequestsMap!=null ) {
|
---|
118 | equalRequestsMap.add(this, this.soapRequestBody);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * <p>
|
---|
124 | * the SOAP method called in this request
|
---|
125 | * </p>
|
---|
126 | *
|
---|
127 | * @return the SOAP method called in this request
|
---|
128 | */
|
---|
129 | public String getCalledMethod() {
|
---|
130 | return calledMethod;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * <p>
|
---|
135 | * the name of the service called in this request
|
---|
136 | * </p>
|
---|
137 | *
|
---|
138 | * @return the name of the service called in this request
|
---|
139 | */
|
---|
140 | public String getServiceName() {
|
---|
141 | return serviceName;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * <p>
|
---|
146 | * the name of the client calling in this request
|
---|
147 | * </p>
|
---|
148 | *
|
---|
149 | * @return the name of the client calling in this request
|
---|
150 | */
|
---|
151 | public String getClientName() {
|
---|
152 | return clientName;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * <p>
|
---|
157 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
158 | * {@link RequestBodyMode}.
|
---|
159 | * </p>
|
---|
160 | *
|
---|
161 | * @return body of the SOAP request
|
---|
162 | */
|
---|
163 | public Element getSoapRequestBody() {
|
---|
164 | return createDOMElement(soapRequestBody);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * <p>
|
---|
169 | * returns a randomly draw request body for the called method using {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}
|
---|
170 | * </p>
|
---|
171 | *
|
---|
172 | * @return randomly drawn body of the SOAP request
|
---|
173 | */
|
---|
174 | public Element getRandomSoapRequestBody() {
|
---|
175 | if( equalRequestsMap==null ) {
|
---|
176 | throw new RuntimeException("cannot use random mode is no request map is available; different data missing");
|
---|
177 | }
|
---|
178 | return createDOMElement(equalRequestsMap.getRandom(this));
|
---|
179 | }
|
---|
180 |
|
---|
181 | public EqualSOAPDataMap getEqualSOAPDataMap() {
|
---|
182 | return equalRequestsMap;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * (non-Javadoc)
|
---|
187 | *
|
---|
188 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
189 | */
|
---|
190 | @Override
|
---|
191 | public String getName() {
|
---|
192 | return "(" + serviceName + ", " + calledMethod + ")";
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * (non-Javadoc)
|
---|
197 | *
|
---|
198 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
|
---|
199 | */
|
---|
200 | @Override
|
---|
201 | public boolean equals(Object obj) {
|
---|
202 | if (this == obj) {
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 | else if (obj instanceof SimpleSOAPEventType) {
|
---|
206 | return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
207 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
208 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
209 | }
|
---|
210 | else {
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * (non-Javadoc)
|
---|
217 | *
|
---|
218 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
|
---|
219 | */
|
---|
220 | @Override
|
---|
221 | public int hashCode() {
|
---|
222 | int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
223 | return hashCode;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /*
|
---|
227 | * (non-Javadoc)
|
---|
228 | *
|
---|
229 | * @see java.lang.Object#toString()
|
---|
230 | */
|
---|
231 | @Override
|
---|
232 | public String toString() {
|
---|
233 | return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
|
---|
234 | }
|
---|
235 |
|
---|
236 | private Element createDOMElement(String requestBody) {
|
---|
237 | try {
|
---|
238 | return DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
---|
239 | .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
|
---|
240 | }
|
---|
241 | catch (SAXException | IOException | ParserConfigurationException e) {
|
---|
242 | return null;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | //
|
---|
246 | // /**
|
---|
247 | // * <p>
|
---|
248 | // * Determines how getSoapRequestBody works.
|
---|
249 | // * <ul>
|
---|
250 | // * <li>LOCALEVENT: returns the request body of the event type itself</li>
|
---|
251 | // * <li>RANDOM: returns a randomly draw request body for the called method using
|
---|
252 | // * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}.
|
---|
253 | // * </ul>
|
---|
254 | // * </p>
|
---|
255 | // *
|
---|
256 | // * @author Steffen Herbold
|
---|
257 | // */
|
---|
258 | // public static enum RequestBodyMode {
|
---|
259 | // LOCALEVENT, RANDOM
|
---|
260 | // }
|
---|
261 | }
|
---|