[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;
|
---|
[1994] | 19 | import java.io.InvalidObjectException;
|
---|
| 20 | import java.io.ObjectInputStream;
|
---|
| 21 | import java.io.ObjectOutputStream;
|
---|
[1924] | 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 |
|
---|
[1635] | 29 | import de.ugoe.cs.autoquest.eventcore.IEventType;
|
---|
| 30 | import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
|
---|
[1924] | 31 | import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
|
---|
[1635] | 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 |
|
---|
[1993] | 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 |
|
---|
[1635] | 54 | /** */
|
---|
| 55 | private static final long serialVersionUID = 1L;
|
---|
[1987] | 56 |
|
---|
[1984] | 57 | /**
|
---|
[1635] | 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;
|
---|
[1924] | 70 |
|
---|
[1913] | 71 | /**
|
---|
| 72 | * <p>
|
---|
[1924] | 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
|
---|
[1913] | 75 | * </p>
|
---|
| 76 | */
|
---|
| 77 | private final String clientName;
|
---|
[1635] | 78 |
|
---|
| 79 | /**
|
---|
| 80 | * <p>
|
---|
[1993] | 81 | * the body of the SOAP message; storage as serialized XML string to allow object serialization
|
---|
[1924] | 82 | * </p>
|
---|
| 83 | */
|
---|
[1993] | 84 | private final String soapMsgBody;
|
---|
| 85 |
|
---|
[1988] | 86 | /**
|
---|
[1993] | 87 | * <p>
|
---|
| 88 | * defines whether this event represents a request or a response
|
---|
| 89 | * </p>
|
---|
[1988] | 90 | */
|
---|
[1993] | 91 | private final CallType callType;
|
---|
[1924] | 92 |
|
---|
| 93 | /**
|
---|
[1993] | 94 | * reference to the {@link EqualSOAPDataMap} associated with the sequence this event belongs to.
|
---|
| 95 | */
|
---|
[1994] | 96 | private transient EqualSOAPDataMap equalBodyMap;
|
---|
[1993] | 97 |
|
---|
| 98 | /**
|
---|
[1924] | 99 | * <p>
|
---|
[1994] | 100 | * Constructor. Creates a new SimpleSOAPEventType with a EqualSOAPDATAMap. Should always call
|
---|
| 101 | * initEqualBodyMap afterwards.
|
---|
[1635] | 102 | * </p>
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
[1924] | 105 | public SimpleSOAPEventType(String calledMethod,
|
---|
| 106 | String serviceName,
|
---|
| 107 | String clientName,
|
---|
[1993] | 108 | Element soapMsgBody,
|
---|
| 109 | EqualSOAPDataMap equalRequestsMap,
|
---|
| 110 | CallType callType)
|
---|
| 111 | {
|
---|
[1635] | 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 | }
|
---|
[1913] | 118 | if (clientName == null) {
|
---|
| 119 | throw new IllegalArgumentException("clientName must not be null");
|
---|
| 120 | }
|
---|
[1635] | 121 | this.calledMethod = calledMethod;
|
---|
| 122 | this.serviceName = serviceName;
|
---|
[1913] | 123 | this.clientName = clientName;
|
---|
[1993] | 124 | this.equalBodyMap = equalRequestsMap;
|
---|
| 125 | this.soapMsgBody = SOAPUtils.getSerialization(soapMsgBody);
|
---|
| 126 | this.callType = callType;
|
---|
[1994] | 127 |
|
---|
| 128 | if (equalBodyMap != null) {
|
---|
| 129 | equalBodyMap.add(this, this.soapMsgBody);
|
---|
[1988] | 130 | }
|
---|
[1635] | 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 | }
|
---|
[1924] | 154 |
|
---|
[1913] | 155 | /**
|
---|
| 156 | * <p>
|
---|
| 157 | * the name of the client calling in this request
|
---|
| 158 | * </p>
|
---|
[1924] | 159 | *
|
---|
[1913] | 160 | * @return the name of the client calling in this request
|
---|
| 161 | */
|
---|
| 162 | public String getClientName() {
|
---|
| 163 | return clientName;
|
---|
| 164 | }
|
---|
[1635] | 165 |
|
---|
[1984] | 166 | /**
|
---|
| 167 | * <p>
|
---|
[1987] | 168 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
| 169 | * {@link RequestBodyMode}.
|
---|
[1984] | 170 | * </p>
|
---|
[1987] | 171 | *
|
---|
[1984] | 172 | * @return body of the SOAP request
|
---|
| 173 | */
|
---|
[1993] | 174 | public Element getSoapMsgBody() {
|
---|
| 175 | return createDOMElement(soapMsgBody);
|
---|
[1988] | 176 | }
|
---|
[1993] | 177 |
|
---|
[1988] | 178 | /**
|
---|
| 179 | * <p>
|
---|
[1993] | 180 | * returns a randomly draw request body for the called method using
|
---|
| 181 | * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}
|
---|
[1988] | 182 | * </p>
|
---|
[1993] | 183 | *
|
---|
[1988] | 184 | * @return randomly drawn body of the SOAP request
|
---|
| 185 | */
|
---|
[1993] | 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");
|
---|
[1984] | 190 | }
|
---|
[2037] | 191 | if( soapMsgBody == null ) {
|
---|
| 192 | return null;
|
---|
| 193 | }
|
---|
[1993] | 194 | return createDOMElement(equalBodyMap.getRandom(this));
|
---|
[1924] | 195 | }
|
---|
[1993] | 196 |
|
---|
| 197 | /**
|
---|
| 198 | * <p>
|
---|
| 199 | * returns the {@link EqualSOAPDataMap} associated with this event
|
---|
| 200 | * </p>
|
---|
| 201 | *
|
---|
| 202 | * @return the {@link EqualSOAPDataMap}
|
---|
| 203 | */
|
---|
[1988] | 204 | public EqualSOAPDataMap getEqualSOAPDataMap() {
|
---|
[1993] | 205 | return equalBodyMap;
|
---|
[1988] | 206 | }
|
---|
[1924] | 207 |
|
---|
[1993] | 208 | public CallType getCallType() {
|
---|
| 209 | return callType;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[1635] | 212 | /*
|
---|
| 213 | * (non-Javadoc)
|
---|
| 214 | *
|
---|
| 215 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
| 216 | */
|
---|
| 217 | @Override
|
---|
| 218 | public String getName() {
|
---|
[1993] | 219 | return "(" + callType + ":" + clientName + "->" + serviceName + ", " + calledMethod + ")";
|
---|
[1635] | 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) {
|
---|
[1994] | 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 &&
|
---|
[1993] | 246 | HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
[1924] | 247 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
| 248 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
[1635] | 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() {
|
---|
[1994] | 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();
|
---|
[1635] | 270 | }
|
---|
[1924] | 271 |
|
---|
| 272 | /*
|
---|
| 273 | * (non-Javadoc)
|
---|
| 274 | *
|
---|
[1642] | 275 | * @see java.lang.Object#toString()
|
---|
| 276 | */
|
---|
| 277 | @Override
|
---|
| 278 | public String toString() {
|
---|
[1993] | 279 | return "SimpleSOAPEventType" + getName();
|
---|
[1642] | 280 | }
|
---|
[1987] | 281 |
|
---|
[1988] | 282 | private Element createDOMElement(String requestBody) {
|
---|
[2037] | 283 | if( requestBody==null ) {
|
---|
| 284 | return null;
|
---|
| 285 | }
|
---|
[1988] | 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 | }
|
---|
[1984] | 293 | }
|
---|
[1994] | 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 | }
|
---|
[1635] | 309 | }
|
---|