[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 |
|
---|
| 40 | /** */
|
---|
| 41 | private static final long serialVersionUID = 1L;
|
---|
[1987] | 42 |
|
---|
[1984] | 43 | /**
|
---|
[1635] | 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;
|
---|
[1924] | 56 |
|
---|
[1913] | 57 | /**
|
---|
| 58 | * <p>
|
---|
[1924] | 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
|
---|
[1913] | 61 | * </p>
|
---|
| 62 | */
|
---|
| 63 | private final String clientName;
|
---|
[1635] | 64 |
|
---|
| 65 | /**
|
---|
| 66 | * <p>
|
---|
[1924] | 67 | * the body of the SOAP request; storage as serialized XML string to allow object serialization
|
---|
| 68 | * </p>
|
---|
| 69 | */
|
---|
| 70 | private final String soapRequestBody;
|
---|
[1988] | 71 |
|
---|
| 72 | /**
|
---|
| 73 | * TODO
|
---|
| 74 | */
|
---|
| 75 | private final EqualSOAPDataMap equalRequestsMap;
|
---|
[1924] | 76 |
|
---|
| 77 | /**
|
---|
| 78 | * <p>
|
---|
[1635] | 79 | * Constructor. Creates a new SimpleSOAPEventType.
|
---|
| 80 | * </p>
|
---|
| 81 | *
|
---|
| 82 | */
|
---|
[1924] | 83 | public SimpleSOAPEventType(String calledMethod,
|
---|
| 84 | String serviceName,
|
---|
| 85 | String clientName,
|
---|
| 86 | Element soapRequestBody)
|
---|
| 87 | {
|
---|
[1988] | 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 | {
|
---|
[1635] | 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 | }
|
---|
[1913] | 109 | if (clientName == null) {
|
---|
| 110 | throw new IllegalArgumentException("clientName must not be null");
|
---|
| 111 | }
|
---|
[1635] | 112 | this.calledMethod = calledMethod;
|
---|
| 113 | this.serviceName = serviceName;
|
---|
[1913] | 114 | this.clientName = clientName;
|
---|
[1924] | 115 | this.soapRequestBody = SOAPUtils.getSerialization(soapRequestBody);
|
---|
[1988] | 116 | this.equalRequestsMap = equalRequestsMap;
|
---|
| 117 | if( equalRequestsMap!=null ) {
|
---|
| 118 | equalRequestsMap.add(this, this.soapRequestBody);
|
---|
| 119 | }
|
---|
[1635] | 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 | }
|
---|
[1924] | 143 |
|
---|
[1913] | 144 | /**
|
---|
| 145 | * <p>
|
---|
| 146 | * the name of the client calling in this request
|
---|
| 147 | * </p>
|
---|
[1924] | 148 | *
|
---|
[1913] | 149 | * @return the name of the client calling in this request
|
---|
| 150 | */
|
---|
| 151 | public String getClientName() {
|
---|
| 152 | return clientName;
|
---|
| 153 | }
|
---|
[1635] | 154 |
|
---|
[1984] | 155 | /**
|
---|
| 156 | * <p>
|
---|
[1987] | 157 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
| 158 | * {@link RequestBodyMode}.
|
---|
[1984] | 159 | * </p>
|
---|
[1987] | 160 | *
|
---|
[1984] | 161 | * @return body of the SOAP request
|
---|
| 162 | */
|
---|
[1924] | 163 | public Element getSoapRequestBody() {
|
---|
[1988] | 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");
|
---|
[1984] | 177 | }
|
---|
[1988] | 178 | return createDOMElement(equalRequestsMap.getRandom(this));
|
---|
[1924] | 179 | }
|
---|
[1988] | 180 |
|
---|
| 181 | public EqualSOAPDataMap getEqualSOAPDataMap() {
|
---|
| 182 | return equalRequestsMap;
|
---|
| 183 | }
|
---|
[1924] | 184 |
|
---|
[1635] | 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) {
|
---|
[1642] | 206 | return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
[1924] | 207 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
| 208 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
[1635] | 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() {
|
---|
[1924] | 222 | int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
[1635] | 223 | return hashCode;
|
---|
| 224 | }
|
---|
[1924] | 225 |
|
---|
| 226 | /*
|
---|
| 227 | * (non-Javadoc)
|
---|
| 228 | *
|
---|
[1642] | 229 | * @see java.lang.Object#toString()
|
---|
| 230 | */
|
---|
| 231 | @Override
|
---|
| 232 | public String toString() {
|
---|
| 233 | return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
|
---|
| 234 | }
|
---|
[1987] | 235 |
|
---|
[1988] | 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 | }
|
---|
[1984] | 244 | }
|
---|
[1988] | 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 | // }
|
---|
[1635] | 261 | }
|
---|