[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 | /**
|
---|
| 44 | * see {@link RequestBodyMode}
|
---|
| 45 | */
|
---|
| 46 | private static RequestBodyMode requestBodyMode = RequestBodyMode.LOCALEVENT;
|
---|
[1635] | 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;
|
---|
[1924] | 61 |
|
---|
[1913] | 62 | /**
|
---|
| 63 | * <p>
|
---|
[1924] | 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
|
---|
[1913] | 66 | * </p>
|
---|
| 67 | */
|
---|
| 68 | private final String clientName;
|
---|
[1635] | 69 |
|
---|
| 70 | /**
|
---|
| 71 | * <p>
|
---|
[1924] | 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>
|
---|
[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 | {
|
---|
[1635] | 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 | }
|
---|
[1913] | 94 | if (clientName == null) {
|
---|
| 95 | throw new IllegalArgumentException("clientName must not be null");
|
---|
| 96 | }
|
---|
[1635] | 97 | this.calledMethod = calledMethod;
|
---|
| 98 | this.serviceName = serviceName;
|
---|
[1913] | 99 | this.clientName = clientName;
|
---|
[1924] | 100 | this.soapRequestBody = SOAPUtils.getSerialization(soapRequestBody);
|
---|
[1986] | 101 | EqualSOAPDataMap.getInstance().add(this, this.soapRequestBody);
|
---|
[1635] | 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 | }
|
---|
[1924] | 125 |
|
---|
[1913] | 126 | /**
|
---|
| 127 | * <p>
|
---|
| 128 | * the name of the client calling in this request
|
---|
| 129 | * </p>
|
---|
[1924] | 130 | *
|
---|
[1913] | 131 | * @return the name of the client calling in this request
|
---|
| 132 | */
|
---|
| 133 | public String getClientName() {
|
---|
| 134 | return clientName;
|
---|
| 135 | }
|
---|
[1635] | 136 |
|
---|
[1984] | 137 | /**
|
---|
| 138 | * <p>
|
---|
[1987] | 139 | * returns the body of the SOAP request; how the body is determined is defined by the
|
---|
| 140 | * {@link RequestBodyMode}.
|
---|
[1984] | 141 | * </p>
|
---|
[1987] | 142 | *
|
---|
[1984] | 143 | * @return body of the SOAP request
|
---|
| 144 | */
|
---|
[1924] | 145 | public Element getSoapRequestBody() {
|
---|
[1984] | 146 | String requestBody;
|
---|
| 147 | switch (requestBodyMode)
|
---|
| 148 | {
|
---|
| 149 | case LOCALEVENT:
|
---|
| 150 | requestBody = soapRequestBody;
|
---|
| 151 | break;
|
---|
| 152 | case RANDOM:
|
---|
[1986] | 153 | requestBody = EqualSOAPDataMap.getInstance().getRandom(this);
|
---|
[1984] | 154 | break;
|
---|
| 155 | default:
|
---|
| 156 | throw new RuntimeException("undefined RequestBodyMode");
|
---|
| 157 | }
|
---|
[1987] | 158 | if (requestBody == null) {
|
---|
[1984] | 159 | System.err.println("foobar" + this);
|
---|
[1986] | 160 | System.err.println(EqualSOAPDataMap.getInstance().getAll(this));
|
---|
[1984] | 161 | }
|
---|
[1924] | 162 | try {
|
---|
| 163 | return DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
---|
[1984] | 164 | .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
|
---|
[1924] | 165 | }
|
---|
| 166 | catch (SAXException | IOException | ParserConfigurationException e) {
|
---|
| 167 | return null;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[1635] | 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) {
|
---|
[1642] | 192 | return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
[1924] | 193 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
| 194 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
[1635] | 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() {
|
---|
[1924] | 208 | int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
[1635] | 209 | return hashCode;
|
---|
| 210 | }
|
---|
[1924] | 211 |
|
---|
| 212 | /*
|
---|
| 213 | * (non-Javadoc)
|
---|
| 214 | *
|
---|
[1642] | 215 | * @see java.lang.Object#toString()
|
---|
| 216 | */
|
---|
| 217 | @Override
|
---|
| 218 | public String toString() {
|
---|
| 219 | return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
|
---|
| 220 | }
|
---|
[1987] | 221 |
|
---|
[1984] | 222 | /**
|
---|
| 223 | * <p>
|
---|
| 224 | * returns the current {@link RequestBodyMode}
|
---|
| 225 | * </p>
|
---|
[1987] | 226 | *
|
---|
[1984] | 227 | * @return the requestBodyMode
|
---|
| 228 | */
|
---|
| 229 | public static RequestBodyMode getRequestBodyMode() {
|
---|
| 230 | return requestBodyMode;
|
---|
| 231 | }
|
---|
[1987] | 232 |
|
---|
[1984] | 233 | /**
|
---|
| 234 | * <p>
|
---|
| 235 | * sets the {@link RequestBodyMode}
|
---|
| 236 | * </p>
|
---|
[1987] | 237 | *
|
---|
[1984] | 238 | * @param new requestBodyMode
|
---|
| 239 | */
|
---|
| 240 | public static void setRequestBodyMode(RequestBodyMode requestBodyMode) {
|
---|
| 241 | SimpleSOAPEventType.requestBodyMode = requestBodyMode;
|
---|
| 242 | }
|
---|
[1987] | 243 |
|
---|
[1984] | 244 | /**
|
---|
| 245 | * <p>
|
---|
| 246 | * Determines how getSoapRequestBody works.
|
---|
| 247 | * <ul>
|
---|
| 248 | * <li>LOCALEVENT: returns the request body of the event type itself</li>
|
---|
[1987] | 249 | * <li>RANDOM: returns a randomly draw request body for the called method using
|
---|
| 250 | * {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}.
|
---|
| 251 | * </ul>
|
---|
[1984] | 252 | * </p>
|
---|
| 253 | *
|
---|
| 254 | * @author Steffen Herbold
|
---|
| 255 | */
|
---|
[1987] | 256 | public static enum RequestBodyMode {
|
---|
| 257 | LOCALEVENT, RANDOM
|
---|
| 258 | }
|
---|
[1635] | 259 | }
|
---|