[1635] | 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 |
|
---|
[1924] | 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 |
|
---|
[1635] | 26 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
| 27 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
[1924] | 28 | import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
|
---|
[1635] | 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 |
|
---|
[1993] | 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 |
|
---|
[1635] | 51 | /** */
|
---|
| 52 | private static final long serialVersionUID = 1L;
|
---|
[1987] | 53 |
|
---|
[1984] | 54 | /**
|
---|
[1635] | 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;
|
---|
[1924] | 67 |
|
---|
[1913] | 68 | /**
|
---|
| 69 | * <p>
|
---|
[1924] | 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
|
---|
[1913] | 72 | * </p>
|
---|
| 73 | */
|
---|
| 74 | private final String clientName;
|
---|
[1635] | 75 |
|
---|
| 76 | /**
|
---|
| 77 | * <p>
|
---|
[1993] | 78 | * the body of the SOAP message; storage as serialized XML string to allow object serialization
|
---|
[1924] | 79 | * </p>
|
---|
| 80 | */
|
---|
[1993] | 81 | private final String soapMsgBody;
|
---|
| 82 |
|
---|
[1988] | 83 | /**
|
---|
[1993] | 84 | * <p>
|
---|
| 85 | * defines whether this event represents a request or a response
|
---|
| 86 | * </p>
|
---|
[1988] | 87 | */
|
---|
[1993] | 88 | private final CallType callType;
|
---|
[1924] | 89 |
|
---|
| 90 | /**
|
---|
[1993] | 91 | * reference to the {@link EqualSOAPDataMap} associated with the sequence this event belongs to.
|
---|
| 92 | */
|
---|
| 93 | private final EqualSOAPDataMap equalBodyMap;
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
[1924] | 96 | * <p>
|
---|
[1635] | 97 | * Constructor. Creates a new SimpleSOAPEventType.
|
---|
| 98 | * </p>
|
---|
| 99 | *
|
---|
| 100 | */
|
---|
[1924] | 101 | public SimpleSOAPEventType(String calledMethod,
|
---|
| 102 | String serviceName,
|
---|
| 103 | String clientName,
|
---|
[1993] | 104 | Element soapMsgBody)
|
---|
[1924] | 105 | {
|
---|
[1993] | 106 | this(calledMethod, serviceName, clientName, soapMsgBody, null, CallType.REQUEST);
|
---|
[1988] | 107 | }
|
---|
[1993] | 108 |
|
---|
[1988] | 109 | /**
|
---|
| 110 | * <p>
|
---|
[1993] | 111 | * Constructor. Creates a new SimpleSOAPEventType with a EqualSOAPDATAMap.
|
---|
[1988] | 112 | * </p>
|
---|
| 113 | *
|
---|
| 114 | */
|
---|
| 115 | public SimpleSOAPEventType(String calledMethod,
|
---|
| 116 | String serviceName,
|
---|
| 117 | String clientName,
|
---|
[1993] | 118 | Element soapMsgBody,
|
---|
[1988] | 119 | EqualSOAPDataMap equalRequestsMap)
|
---|
| 120 | {
|
---|
[1993] | 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 | {
|
---|
[1635] | 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 | }
|
---|
[1913] | 144 | if (clientName == null) {
|
---|
| 145 | throw new IllegalArgumentException("clientName must not be null");
|
---|
| 146 | }
|
---|
[1635] | 147 | this.calledMethod = calledMethod;
|
---|
| 148 | this.serviceName = serviceName;
|
---|
[1913] | 149 | this.clientName = clientName;
|
---|
[1993] | 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);
|
---|
[1988] | 158 | }
|
---|
[1635] | 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 | }
|
---|
[1924] | 182 |
|
---|
[1913] | 183 | /**
|
---|
| 184 | * <p>
|
---|
| 185 | * the name of the client calling in this request
|
---|
| 186 | * </p>
|
---|
[1924] | 187 | *
|
---|
[1913] | 188 | * @return the name of the client calling in this request
|
---|
| 189 | */
|
---|
| 190 | public String getClientName() {
|
---|
| 191 | return clientName;
|
---|
| 192 | }
|
---|
[1635] | 193 |
|
---|
[1984] | 194 | /**
|
---|
| 195 | * <p>
|
---|
[1987] | 196 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
| 197 | * {@link RequestBodyMode}.
|
---|
[1984] | 198 | * </p>
|
---|
[1987] | 199 | *
|
---|
[1984] | 200 | * @return body of the SOAP request
|
---|
| 201 | */
|
---|
[1993] | 202 | public Element getSoapMsgBody() {
|
---|
| 203 | return createDOMElement(soapMsgBody);
|
---|
[1988] | 204 | }
|
---|
[1993] | 205 |
|
---|
[1988] | 206 | /**
|
---|
| 207 | * <p>
|
---|
[1993] | 208 | * returns a randomly draw request body for the called method using
|
---|
| 209 | * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}
|
---|
[1988] | 210 | * </p>
|
---|
[1993] | 211 | *
|
---|
[1988] | 212 | * @return randomly drawn body of the SOAP request
|
---|
| 213 | */
|
---|
[1993] | 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");
|
---|
[1984] | 218 | }
|
---|
[1993] | 219 | return createDOMElement(equalBodyMap.getRandom(this));
|
---|
[1924] | 220 | }
|
---|
[1993] | 221 |
|
---|
| 222 | /**
|
---|
| 223 | * <p>
|
---|
| 224 | * returns the {@link EqualSOAPDataMap} associated with this event
|
---|
| 225 | * </p>
|
---|
| 226 | *
|
---|
| 227 | * @return the {@link EqualSOAPDataMap}
|
---|
| 228 | */
|
---|
[1988] | 229 | public EqualSOAPDataMap getEqualSOAPDataMap() {
|
---|
[1993] | 230 | return equalBodyMap;
|
---|
[1988] | 231 | }
|
---|
[1924] | 232 |
|
---|
[1993] | 233 | public CallType getCallType() {
|
---|
| 234 | return callType;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[1635] | 237 | /*
|
---|
| 238 | * (non-Javadoc)
|
---|
| 239 | *
|
---|
| 240 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
| 241 | */
|
---|
| 242 | @Override
|
---|
| 243 | public String getName() {
|
---|
[1993] | 244 | return "(" + callType + ":" + clientName + "->" + serviceName + ", " + calledMethod + ")";
|
---|
[1635] | 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) {
|
---|
[1993] | 258 | return callType.equals(((SimpleSOAPEventType) obj).callType) &&
|
---|
| 259 | HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
[1924] | 260 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
| 261 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
[1635] | 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() {
|
---|
[1993] | 275 | return callType.hashCode() + calledMethod.hashCode() + serviceName.hashCode() +
|
---|
| 276 | clientName.hashCode();
|
---|
[1635] | 277 | }
|
---|
[1924] | 278 |
|
---|
| 279 | /*
|
---|
| 280 | * (non-Javadoc)
|
---|
| 281 | *
|
---|
[1642] | 282 | * @see java.lang.Object#toString()
|
---|
| 283 | */
|
---|
| 284 | @Override
|
---|
| 285 | public String toString() {
|
---|
[1993] | 286 | return "SimpleSOAPEventType" + getName();
|
---|
[1642] | 287 | }
|
---|
[1987] | 288 |
|
---|
[1988] | 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 | }
|
---|
[1984] | 297 | }
|
---|
[1635] | 298 | }
|
---|