[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;
|
---|
[1984] | 42 |
|
---|
| 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>
|
---|
| 139 | * returns the body of the SOAP request; how the body is determined is defined by the {@link RequestBodyMode}.
|
---|
| 140 | * </p>
|
---|
| 141 | *
|
---|
| 142 | * @return body of the SOAP request
|
---|
| 143 | */
|
---|
[1924] | 144 | public Element getSoapRequestBody() {
|
---|
[1984] | 145 | String requestBody;
|
---|
| 146 | switch (requestBodyMode)
|
---|
| 147 | {
|
---|
| 148 | case LOCALEVENT:
|
---|
| 149 | requestBody = soapRequestBody;
|
---|
| 150 | break;
|
---|
| 151 | case RANDOM:
|
---|
[1986] | 152 | requestBody = EqualSOAPDataMap.getInstance().getRandom(this);
|
---|
[1984] | 153 | break;
|
---|
| 154 | default:
|
---|
| 155 | throw new RuntimeException("undefined RequestBodyMode");
|
---|
| 156 | }
|
---|
| 157 | if( requestBody==null ) {
|
---|
| 158 | System.err.println("foobar" + this);
|
---|
[1986] | 159 | System.err.println(EqualSOAPDataMap.getInstance().getAll(this));
|
---|
[1984] | 160 | }
|
---|
[1924] | 161 | try {
|
---|
| 162 | return DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
---|
[1984] | 163 | .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
|
---|
[1924] | 164 | }
|
---|
| 165 | catch (SAXException | IOException | ParserConfigurationException e) {
|
---|
| 166 | return null;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
[1984] | 169 |
|
---|
| 170 |
|
---|
[1924] | 171 |
|
---|
[1635] | 172 | /*
|
---|
| 173 | * (non-Javadoc)
|
---|
| 174 | *
|
---|
| 175 | * @see de.ugoe.cs.autoquest.eventcore.IEventType#getName()
|
---|
| 176 | */
|
---|
| 177 | @Override
|
---|
| 178 | public String getName() {
|
---|
| 179 | return "(" + serviceName + ", " + calledMethod + ")";
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /*
|
---|
| 183 | * (non-Javadoc)
|
---|
| 184 | *
|
---|
| 185 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#equals(java.lang.Object)
|
---|
| 186 | */
|
---|
| 187 | @Override
|
---|
| 188 | public boolean equals(Object obj) {
|
---|
| 189 | if (this == obj) {
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
| 192 | else if (obj instanceof SimpleSOAPEventType) {
|
---|
[1642] | 193 | return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
|
---|
[1924] | 194 | HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
|
---|
| 195 | HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
|
---|
[1635] | 196 | }
|
---|
| 197 | else {
|
---|
| 198 | return false;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /*
|
---|
| 203 | * (non-Javadoc)
|
---|
| 204 | *
|
---|
| 205 | * @see de.ugoe.cs.autoquest.plugin.http.eventcore.HTTPEventType#hashCode()
|
---|
| 206 | */
|
---|
| 207 | @Override
|
---|
| 208 | public int hashCode() {
|
---|
[1924] | 209 | int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
|
---|
[1635] | 210 | return hashCode;
|
---|
| 211 | }
|
---|
[1924] | 212 |
|
---|
| 213 | /*
|
---|
| 214 | * (non-Javadoc)
|
---|
| 215 | *
|
---|
[1642] | 216 | * @see java.lang.Object#toString()
|
---|
| 217 | */
|
---|
| 218 | @Override
|
---|
| 219 | public String toString() {
|
---|
| 220 | return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
|
---|
| 221 | }
|
---|
[1984] | 222 |
|
---|
| 223 | /**
|
---|
| 224 | * <p>
|
---|
| 225 | * returns the current {@link RequestBodyMode}
|
---|
| 226 | * </p>
|
---|
| 227 | *
|
---|
| 228 | * @return the requestBodyMode
|
---|
| 229 | */
|
---|
| 230 | public static RequestBodyMode getRequestBodyMode() {
|
---|
| 231 | return requestBodyMode;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * <p>
|
---|
| 236 | * sets the {@link RequestBodyMode}
|
---|
| 237 | * </p>
|
---|
| 238 | *
|
---|
| 239 | * @param new requestBodyMode
|
---|
| 240 | */
|
---|
| 241 | public static void setRequestBodyMode(RequestBodyMode requestBodyMode) {
|
---|
| 242 | SimpleSOAPEventType.requestBodyMode = requestBodyMode;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * <p>
|
---|
| 247 | * Determines how getSoapRequestBody works.
|
---|
| 248 | * <ul>
|
---|
| 249 | * <li>LOCALEVENT: returns the request body of the event type itself</li>
|
---|
| 250 | * <li>RANDOM: returns a randomly draw request body for the called method using {@link EqualSOAPDataMap#getRandom(SimpleSOAPEventType)}.
|
---|
| 251 | * </ul>
|
---|
| 252 | * </p>
|
---|
| 253 | *
|
---|
| 254 | * @author Steffen Herbold
|
---|
| 255 | */
|
---|
[1985] | 256 | public static enum RequestBodyMode {LOCALEVENT, RANDOM}
|
---|
[1635] | 257 | }
|
---|