source: trunk/autoquest-plugin-http/src/main/java/de/ugoe/cs/autoquest/plugin/http/HTTPUtils.java @ 1923

Last change on this file since 1923 was 1923, checked in by sherbold, 9 years ago
  • added convenience function to remove all non-SOAP events from a sequence
  • modified HTTP log parser to turn of the SOAP logging facility. This removes a lot of junk messages, but it is possible we overlook something in the future. In case of problems, the logger should be turned on again.
File size: 5.8 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;
16
17import java.util.Iterator;
18import java.util.LinkedList;
19import java.util.List;
20
21import de.ugoe.cs.autoquest.eventcore.Event;
22import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType;
23import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType;
24import de.ugoe.cs.autoquest.plugin.http.logdata.Address;
25
26/**
27 * <p>
28 * contains convenience methods used for processing HTTP exchanges
29 * </p>
30 *
31 * @author Patrick Harms
32 */
33public class HTTPUtils {
34
35    /**
36     * <p>
37     * converts an address to a simple string containing either host or ip and the port number if
38     * any.
39     * </p>
40     *
41     * @param address
42     *            the address to convert
43     *
44     * @return either "host:port" or "ip:port" or "host" or "ip" or "port" or null
45     */
46    public static String toString(Address address) {
47        if (address != null) {
48            StringBuffer buffer = new StringBuffer();
49            String prefix = "";
50            if (address.getHost() != null) {
51                buffer.append(address.getHost());
52                prefix = ":";
53            }
54            else if (address.getIp() != null) {
55                buffer.append(prefix);
56                buffer.append(address.getIp());
57                prefix = ":";
58            }
59
60            if (address.getPort() != null) {
61                buffer.append(prefix);
62                buffer.append(address.getPort());
63            }
64
65            if (buffer.length() > 0) {
66                return buffer.toString();
67            }
68        }
69
70        return null;
71    }
72
73    /**
74     * <p>
75     * compares two addresses and returns true, if they are equal and false else. The addresses are
76     * equal, if either the ip-addresses and the ports match or the host names and the ports match.
77     * </p>
78     *
79     * @param address1
80     *            the first address to compare
81     * @param address2
82     *            the second address to compare
83     *
84     * @return as described
85     */
86    public static boolean equals(Address address1, Address address2) {
87        if (address1 == null) {
88            return address2 == null;
89        }
90        else if (address2 == null) {
91            return false;
92        }
93
94        if (!equals(address1.getPort(), address2.getPort())) {
95            return false;
96        }
97
98        if (address1.getIp() != null) {
99            return equals(address1.getIp(), address2.getIp());
100        }
101        else {
102            return equals(address1.getHost(), address2.getHost());
103        }
104    }
105
106    /**
107     * <p>
108     * convenience method to compare to objects. They are considered equal if they both are null, or
109     * if their equals method returns true.
110     * </p>
111     *
112     * @param object1
113     *            the first object to compare
114     * @param object2
115     *            the second object to compare
116     *
117     * @return as described
118     */
119    public static <T> boolean equals(T object1, T object2) {
120        if (object1 == null) {
121            return object2 == null;
122        }
123        else {
124            return object1.equals(object2);
125        }
126    }
127
128    /**
129     * <p>
130     * Replaces events with a {@link SOAPEventType} with new events of {@link SimpleSOAPEventType}
131     * and no target.
132     * </p>
133     *
134     * @param sequence
135     *            sequence where the events are replaced
136     * @param keepOnlySOAPEvents
137     *            if true, all events that are not of SOAPEventType or SimpleSOAPEventType are
138     *            removed
139     * @return sequence with {@link SimpleSOAPEventType}s instead of {@link SOAPEventType}s
140     */
141    public static List<Event> convertToSimpleSOAPEvent(List<Event> sequence,
142                                                       boolean keepOnlySOAPEvents)
143    {
144        List<Event> newSequence = null;
145        if (sequence != null) {
146            newSequence = new LinkedList<>();
147            for (Event event : sequence) {
148                if (event.getType() instanceof SOAPEventType) {
149                    SOAPEventType eventType = (SOAPEventType) event.getType();
150                    newSequence.add(new Event(new SimpleSOAPEventType(eventType.getCalledMethod(),
151                                                                      eventType.getServiceName(),
152                                                                      eventType.getClientName())));
153                }
154                else {
155                    if (!keepOnlySOAPEvents || event.getType() instanceof SimpleSOAPEventType) {
156                        newSequence.add(event);
157                    }
158                }
159            }
160        }
161        return newSequence;
162    }
163   
164    /**
165     * <p>
166     * Removes all non SOAP events from a sequence. Warning: this change is performed in-place!
167     * </p>
168     *
169     * @param sequence
170     *            sequence where the events are replaced
171     */
172    public static void removeNonSOAPEvents(List<Event> sequence)
173    {
174        for (Iterator<Event> eventIter = sequence.iterator(); eventIter.hasNext();) {
175            Event event = eventIter.next();
176            if (!(event.getType() instanceof SOAPEventType)) {
177                eventIter.remove();
178            }
179        }
180    }
181
182    /**
183     * <p>
184     * prevent instantiation
185     * </p>
186     */
187    private HTTPUtils() {}
188
189}
Note: See TracBrowser for help on using the repository browser.