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;
|
---|
16 |
|
---|
17 | import java.io.StringWriter;
|
---|
18 | import java.util.Iterator;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 |
|
---|
22 | import javax.xml.transform.Transformer;
|
---|
23 | import javax.xml.transform.TransformerException;
|
---|
24 | import javax.xml.transform.TransformerFactory;
|
---|
25 | import javax.xml.transform.TransformerFactoryConfigurationError;
|
---|
26 | import javax.xml.transform.dom.DOMSource;
|
---|
27 | import javax.xml.transform.stream.StreamResult;
|
---|
28 |
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 | import org.w3c.dom.Node;
|
---|
31 |
|
---|
32 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
33 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
|
---|
34 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * <p>
|
---|
38 | * Utilities for working with SOAP events. Their main task is to simply working with
|
---|
39 | * {@link SimpleSOAPEventType} and {@link SOAPEventType}.
|
---|
40 | * </p>
|
---|
41 | *
|
---|
42 | * @author Steffen Herbold
|
---|
43 | */
|
---|
44 | public class SOAPUtils {
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * <p>
|
---|
48 | * Replaces events with a {@link SOAPEventType} with new events of {@link SimpleSOAPEventType}
|
---|
49 | * and no target.
|
---|
50 | * </p>
|
---|
51 | *
|
---|
52 | * @param sequence
|
---|
53 | * sequence where the events are replaced
|
---|
54 | * @param keepOnlySOAPEvents
|
---|
55 | * if true, all events that are not of SOAPEventType or SimpleSOAPEventType are
|
---|
56 | * removed
|
---|
57 | * @return sequence with {@link SimpleSOAPEventType}s instead of {@link SOAPEventType}s
|
---|
58 | */
|
---|
59 | public static List<Event> convertToSimpleSOAPEvent(List<Event> sequence,
|
---|
60 | boolean keepOnlySOAPEvents)
|
---|
61 | {
|
---|
62 | List<Event> newSequence = null;
|
---|
63 | if (sequence != null) {
|
---|
64 | newSequence = new LinkedList<>();
|
---|
65 | for (Event event : sequence) {
|
---|
66 | if (event.getType() instanceof SOAPEventType) {
|
---|
67 | SOAPEventType eventType = (SOAPEventType) event.getType();
|
---|
68 | newSequence.add(new Event(new SimpleSOAPEventType(eventType.getCalledMethod(),
|
---|
69 | eventType.getServiceName(),
|
---|
70 | eventType.getClientName(),
|
---|
71 | eventType.getSoapRequestBody())));
|
---|
72 | }
|
---|
73 | else {
|
---|
74 | if (!keepOnlySOAPEvents || event.getType() instanceof SimpleSOAPEventType) {
|
---|
75 | newSequence.add(event);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return newSequence;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * Removes all non SOAP events from a sequence. Warning: this change is performed in-place!
|
---|
86 | * </p>
|
---|
87 | *
|
---|
88 | * @param sequence
|
---|
89 | * sequence where the events are replaced
|
---|
90 | */
|
---|
91 | public static void removeNonSOAPEvents(List<Event> sequence)
|
---|
92 | {
|
---|
93 | for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
|
---|
94 | Event event = eventIter.next();
|
---|
95 | if (!(event.getType() instanceof SOAPEventType)) {
|
---|
96 | eventIter.remove();
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * <p>
|
---|
103 | * Helper function to get the name of a service from a SOAP event.
|
---|
104 | * </p>
|
---|
105 | *
|
---|
106 | * @param event
|
---|
107 | * event for which the service name is retrieved
|
---|
108 | * @return service name
|
---|
109 | */
|
---|
110 | public static String getServiceNameFromEvent(Event event) {
|
---|
111 | if (event.getType() instanceof SOAPEventType) {
|
---|
112 | return ((SOAPEventType) event.getType()).getServiceName();
|
---|
113 | }
|
---|
114 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
115 | return ((SimpleSOAPEventType) event.getType()).getServiceName();
|
---|
116 | }
|
---|
117 | else {
|
---|
118 | throw new RuntimeException(
|
---|
119 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
120 | event.getType().getClass().getName());
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | *
|
---|
126 | * <p>
|
---|
127 | * Helper function to get the called method from a SOAP event
|
---|
128 | * </p>
|
---|
129 | *
|
---|
130 | * @param event
|
---|
131 | * event for which the called method is retrieved
|
---|
132 | * @return called method
|
---|
133 | */
|
---|
134 | public static String getCalledMethodFromEvent(Event event) {
|
---|
135 | if (event.getType() instanceof SOAPEventType) {
|
---|
136 | return ((SOAPEventType) event.getType()).getCalledMethod();
|
---|
137 | }
|
---|
138 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
139 | return ((SimpleSOAPEventType) event.getType()).getCalledMethod();
|
---|
140 | }
|
---|
141 | else {
|
---|
142 | throw new RuntimeException(
|
---|
143 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
144 | event.getType().getClass().getName());
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * <p>
|
---|
150 | * Helper function to get the name of a client from a SOAP event.
|
---|
151 | * </p>
|
---|
152 | *
|
---|
153 | * @param event
|
---|
154 | * event for which the client name is retrieved
|
---|
155 | * @return service name
|
---|
156 | */
|
---|
157 | public static String getClientNameFromEvent(Event event) {
|
---|
158 | if (event.getType() instanceof SOAPEventType) {
|
---|
159 | return ((SOAPEventType) event.getType()).getClientName();
|
---|
160 | }
|
---|
161 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
162 | return ((SimpleSOAPEventType) event.getType()).getClientName();
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | throw new RuntimeException(
|
---|
166 | "Wrong event type. Only SOAPEventType and SimpleSOAPEventType supported but was: " +
|
---|
167 | event.getType().getClass().getName());
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * <p>
|
---|
173 | * Helper function to get the body of a SOAP request.
|
---|
174 | * </p>
|
---|
175 | *
|
---|
176 | * @param event
|
---|
177 | * event for which the SOAP request body is retrieved
|
---|
178 | * @return body of the SOAP event
|
---|
179 | */
|
---|
180 | public static Element getSoapRequestBodyFromEvent(Event event) {
|
---|
181 | Element requestBody = null;
|
---|
182 | if (event.getType() instanceof SOAPEventType) {
|
---|
183 | requestBody = ((SOAPEventType) event.getType()).getSoapRequestBody();
|
---|
184 | }
|
---|
185 | else if (event.getType() instanceof SimpleSOAPEventType) {
|
---|
186 | requestBody = ((SimpleSOAPEventType) event.getType()).getSoapRequestBody();
|
---|
187 | }
|
---|
188 | return requestBody;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * <p>
|
---|
193 | * returns the XML serialization of a DOM node; located here because it is used for SOAP request bodies
|
---|
194 | * </p>
|
---|
195 | *
|
---|
196 | * @param node
|
---|
197 | * DOM node that is serialized
|
---|
198 | * @return XML serialization as String; null if node is null
|
---|
199 | */
|
---|
200 | public static String getSerialization(Node node) {
|
---|
201 | if( node==null ) {
|
---|
202 | return null;
|
---|
203 | }
|
---|
204 | try {
|
---|
205 | StringWriter writer = new StringWriter();
|
---|
206 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
---|
207 | transformer.transform(new DOMSource(node), new StreamResult(writer));
|
---|
208 | return writer.toString();
|
---|
209 | }
|
---|
210 | catch (TransformerFactoryConfigurationError | TransformerException e) {
|
---|
211 | throw new IllegalArgumentException(
|
---|
212 | "could not create serialization for SOAP request body.",
|
---|
213 | e);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * <p>
|
---|
219 | * prevent instantiation
|
---|
220 | * </p>
|
---|
221 | */
|
---|
222 | private SOAPUtils() {}
|
---|
223 | }
|
---|