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 | * see {@link RequestBodyMode}
|
---|
45 | */
|
---|
46 | private static RequestBodyMode requestBodyMode = RequestBodyMode.LOCALEVENT;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * <p>
|
---|
50 | * the SOAP method called in this request
|
---|
51 | * </p>
|
---|
52 | */
|
---|
53 | private final String calledMethod;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * <p>
|
---|
57 | * the name of the service; this is either the path or, if a path map is available
|
---|
58 | * </p>
|
---|
59 | */
|
---|
60 | private final String serviceName;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * <p>
|
---|
64 | * the name of the client; this is either the host/ip and port of the sender or, if a path map
|
---|
65 | * is available, a human readable name that may be based also on the receiver
|
---|
66 | * </p>
|
---|
67 | */
|
---|
68 | private final String clientName;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * <p>
|
---|
72 | * the body of the SOAP request; storage as serialized XML string to allow object serialization
|
---|
73 | * </p>
|
---|
74 | */
|
---|
75 | private final String soapRequestBody;
|
---|
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 | if (calledMethod == null) {
|
---|
89 | throw new IllegalArgumentException("called method must not be null");
|
---|
90 | }
|
---|
91 | if (serviceName == null) {
|
---|
92 | throw new IllegalArgumentException("serviceName must not be null");
|
---|
93 | }
|
---|
94 | if (clientName == null) {
|
---|
95 | throw new IllegalArgumentException("clientName must not be null");
|
---|
96 | }
|
---|
97 | this.calledMethod = calledMethod;
|
---|
98 | this.serviceName = serviceName;
|
---|
99 | this.clientName = clientName;
|
---|
100 | this.soapRequestBody = SOAPUtils.getSerialization(soapRequestBody);
|
---|
101 | EqualSOAPDataMap.getInstance().add(this, this.soapRequestBody);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * <p>
|
---|
106 | * the SOAP method called in this request
|
---|
107 | * </p>
|
---|
108 | *
|
---|
109 | * @return the SOAP method called in this request
|
---|
110 | */
|
---|
111 | public String getCalledMethod() {
|
---|
112 | return calledMethod;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * <p>
|
---|
117 | * the name of the service called in this request
|
---|
118 | * </p>
|
---|
119 | *
|
---|
120 | * @return the name of the service called in this request
|
---|
121 | */
|
---|
122 | public String getServiceName() {
|
---|
123 | return serviceName;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * <p>
|
---|
128 | * the name of the client calling in this request
|
---|
129 | * </p>
|
---|
130 | *
|
---|
131 | * @return the name of the client calling in this request
|
---|
132 | */
|
---|
133 | public String getClientName() {
|
---|
134 | return clientName;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * <p>
|
---|
139 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
140 | * {@link RequestBodyMode}.
|
---|
141 | * </p>
|
---|
142 | *
|
---|
143 | * @return body of the SOAP request
|
---|
144 | */
|
---|
145 | public Element getSoapRequestBody() {
|
---|
146 | String requestBody;
|
---|
147 | switch (requestBodyMode)
|
---|
148 | {
|
---|
149 | case LOCALEVENT:
|
---|
150 | requestBody = soapRequestBody;
|
---|
151 | break;
|
---|
152 | case RANDOM:
|
---|
153 | requestBody = EqualSOAPDataMap.getInstance().getRandom(this);
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | throw new RuntimeException("undefined RequestBodyMode");
|
---|
157 | }
|
---|
158 | if (requestBody == null) {
|
---|
159 | System.err.println("foobar" + this);
|
---|
160 | System.err.println(EqualSOAPDataMap.getInstance().getAll(this));
|
---|
161 | }
|
---|
162 | try {
|
---|
163 | return DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
---|
164 | .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
|
---|
165 | }
|
---|
166 | catch (SAXException | IOException | ParserConfigurationException e) {
|
---|
167 | return null;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * (non-Javadoc)
|
---|
173 | *
|
---|
174 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
175 | */
|
---|
176 | @Override
|
---|
177 | public String getName() {
|
---|
178 | return "(" + serviceName + ", " + calledMethod + ")";
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * (non-Javadoc)
|
---|
183 | *
|
---|
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 SimpleSOAPEventType) {
|
---|
192 | return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
193 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
194 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
195 | }
|
---|
196 | else {
|
---|
197 | return false;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * (non-Javadoc)
|
---|
203 | *
|
---|
204 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
|
---|
205 | */
|
---|
206 | @Override
|
---|
207 | public int hashCode() {
|
---|
208 | int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
209 | return hashCode;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * (non-Javadoc)
|
---|
214 | *
|
---|
215 | * @see java.lang.Object#toString()
|
---|
216 | */
|
---|
217 | @Override
|
---|
218 | public String toString() {
|
---|
219 | return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * <p>
|
---|
224 | * returns the current {@link RequestBodyMode}
|
---|
225 | * </p>
|
---|
226 | *
|
---|
227 | * @return the requestBodyMode
|
---|
228 | */
|
---|
229 | public static RequestBodyMode getRequestBodyMode() {
|
---|
230 | return requestBodyMode;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * <p>
|
---|
235 | * sets the {@link RequestBodyMode}
|
---|
236 | * </p>
|
---|
237 | *
|
---|
238 | * @param new requestBodyMode
|
---|
239 | */
|
---|
240 | public static void setRequestBodyMode(RequestBodyMode requestBodyMode) {
|
---|
241 | SimpleSOAPEventType.requestBodyMode = requestBodyMode;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * <p>
|
---|
246 | * Determines how getSoapRequestBody works.
|
---|
247 | * <ul>
|
---|
248 | * <li>LOCALEVENT: returns the request body of the event type itself</li>
|
---|
249 | * <li>RANDOM: returns a randomly draw request body for the called method using
|
---|
250 | * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}.
|
---|
251 | * </ul>
|
---|
252 | * </p>
|
---|
253 | *
|
---|
254 | * @author Steffen Herbold
|
---|
255 | */
|
---|
256 | public static enum RequestBodyMode {
|
---|
257 | LOCALEVENT, RANDOM
|
---|
258 | }
|
---|
259 | }
|
---|