source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/eventcore/SimpleSOAPEventType.java @ 1986

Last change on this file since 1986 was 1986, checked in by sherbold, 9 years ago
  • EqualSOAPDataMap now serializable
  • Property svn:mime-type set to text/plain
File size: 7.4 KB
Line 
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
15package de.ugoe.cs.autoquest.plugin.http.eventcore;
16
17import java.io.ByteArrayInputStream;
18import java.io.IOException;
19
20import javax.xml.parsers.DocumentBuilderFactory;
21import javax.xml.parsers.ParserConfigurationException;
22
23import org.w3c.dom.Element;
24import org.xml.sax.SAXException;
25
26import de.ugoe.cs.autoquest.eventcore.IEventType;
27import de.ugoe.cs.autoquest.plugin.http.HTTPUtils;
28import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
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 */
38public class SimpleSOAPEventType implements IEventType {
39
40    /**  */
41    private static final long serialVersionUID = 1L;
42   
43    /**
44     * see {@link RequestBodyMode}
45     */
46    private static RequestBodyMode requestBodyMode = RequestBodyMode.LOCALEVENT;
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;
61
62    /**
63     * <p>
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
66     * </p>
67     */
68    private final String clientName;
69
70    /**
71     * <p>
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>
79     * Constructor. Creates a new SimpleSOAPEventType.
80     * </p>
81     *
82     */
83    public SimpleSOAPEventType(String calledMethod,
84                               String serviceName,
85                               String clientName,
86                               Element soapRequestBody)
87    {
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        }
94        if (clientName == null) {
95            throw new IllegalArgumentException("clientName must not be null");
96        }
97        this.calledMethod = calledMethod;
98        this.serviceName = serviceName;
99        this.clientName = clientName;
100        this.soapRequestBody = SOAPUtils.getSerialization(soapRequestBody);
101        EqualSOAPDataMap.getInstance().add(this, this.soapRequestBody);
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    }
125
126    /**
127     * <p>
128     * the name of the client calling in this request
129     * </p>
130     *
131     * @return the name of the client calling in this request
132     */
133    public String getClientName() {
134        return clientName;
135    }
136
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     */
144    public Element getSoapRequestBody() {
145        String requestBody;
146        switch (requestBodyMode)
147        {
148            case LOCALEVENT:
149                requestBody = soapRequestBody;
150                break;
151            case RANDOM:
152                requestBody = EqualSOAPDataMap.getInstance().getRandom(this);
153                break;
154            default:
155                throw new RuntimeException("undefined RequestBodyMode");
156        }
157        if( requestBody==null ) {
158            System.err.println("foobar" + this);
159            System.err.println(EqualSOAPDataMap.getInstance().getAll(this));
160        }
161        try {
162            return DocumentBuilderFactory.newInstance().newDocumentBuilder()
163                .parse(new ByteArrayInputStream(requestBody.getBytes())).getDocumentElement();
164        }
165        catch (SAXException | IOException | ParserConfigurationException e) {
166            return null;
167        }
168    }
169   
170   
171
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) {
193            return HTTPUtils.equals(calledMethod, ((SimpleSOAPEventType) obj).calledMethod) &&
194                HTTPUtils.equals(serviceName, ((SimpleSOAPEventType) obj).serviceName) &&
195                HTTPUtils.equals(clientName, ((SimpleSOAPEventType) obj).clientName);
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() {
209        int hashCode = calledMethod.hashCode() + serviceName.hashCode() + clientName.hashCode();
210        return hashCode;
211    }
212
213    /*
214     * (non-Javadoc)
215     *
216     * @see java.lang.Object#toString()
217     */
218    @Override
219    public String toString() {
220        return "SimpleSOAPEventType(" + serviceName + ", " + calledMethod + ")";
221    }
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     */
256    public static enum RequestBodyMode {LOCALEVENT, RANDOM}
257}
Note: See TracBrowser for help on using the repository browser.