[1367] | 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 | |
---|
[1923] | 17 | import java.util.Iterator; |
---|
[1635] | 18 | import java.util.LinkedList; |
---|
| 19 | import java.util.List; |
---|
| 20 | |
---|
| 21 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 22 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; |
---|
| 23 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; |
---|
[1561] | 24 | import de.ugoe.cs.autoquest.plugin.http.logdata.Address; |
---|
[1367] | 25 | |
---|
| 26 | /** |
---|
| 27 | * <p> |
---|
[1383] | 28 | * contains convenience methods used for processing HTTP exchanges |
---|
[1367] | 29 | * </p> |
---|
| 30 | * |
---|
| 31 | * @author Patrick Harms |
---|
| 32 | */ |
---|
| 33 | public class HTTPUtils { |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * <p> |
---|
[1635] | 37 | * converts an address to a simple string containing either host or ip and the port number if |
---|
| 38 | * any. |
---|
[1367] | 39 | * </p> |
---|
| 40 | * |
---|
[1635] | 41 | * @param address |
---|
| 42 | * the address to convert |
---|
[1367] | 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 | } |
---|
[1417] | 72 | |
---|
| 73 | /** |
---|
| 74 | * <p> |
---|
[1635] | 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. |
---|
[1417] | 77 | * </p> |
---|
| 78 | * |
---|
[1635] | 79 | * @param address1 |
---|
| 80 | * the first address to compare |
---|
| 81 | * @param address2 |
---|
| 82 | * the second address to compare |
---|
| 83 | * |
---|
[1417] | 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 | } |
---|
[1635] | 93 | |
---|
[1417] | 94 | if (!equals(address1.getPort(), address2.getPort())) { |
---|
| 95 | return false; |
---|
| 96 | } |
---|
[1635] | 97 | |
---|
[1417] | 98 | if (address1.getIp() != null) { |
---|
| 99 | return equals(address1.getIp(), address2.getIp()); |
---|
| 100 | } |
---|
| 101 | else { |
---|
| 102 | return equals(address1.getHost(), address2.getHost()); |
---|
| 103 | } |
---|
| 104 | } |
---|
[1635] | 105 | |
---|
[1367] | 106 | /** |
---|
| 107 | * <p> |
---|
[1635] | 108 | * convenience method to compare to objects. They are considered equal if they both are null, or |
---|
| 109 | * if their equals method returns true. |
---|
[1417] | 110 | * </p> |
---|
| 111 | * |
---|
[1635] | 112 | * @param object1 |
---|
| 113 | * the first object to compare |
---|
| 114 | * @param object2 |
---|
| 115 | * the second object to compare |
---|
| 116 | * |
---|
[1417] | 117 | * @return as described |
---|
| 118 | */ |
---|
| 119 | public static <T> boolean equals(T object1, T object2) { |
---|
[1635] | 120 | if (object1 == null) { |
---|
[1417] | 121 | return object2 == null; |
---|
| 122 | } |
---|
| 123 | else { |
---|
| 124 | return object1.equals(object2); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * <p> |
---|
[1635] | 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 |
---|
[1915] | 136 | * @param keepOnlySOAPEvents |
---|
| 137 | * if true, all events that are not of SOAPEventType or SimpleSOAPEventType are |
---|
| 138 | * removed |
---|
[1635] | 139 | * @return sequence with {@link SimpleSOAPEventType}s instead of {@link SOAPEventType}s |
---|
| 140 | */ |
---|
[1915] | 141 | public static List<Event> convertToSimpleSOAPEvent(List<Event> sequence, |
---|
| 142 | boolean keepOnlySOAPEvents) |
---|
| 143 | { |
---|
[1635] | 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(), |
---|
[1913] | 151 | eventType.getServiceName(), |
---|
| 152 | eventType.getClientName()))); |
---|
[1635] | 153 | } |
---|
| 154 | else { |
---|
[1915] | 155 | if (!keepOnlySOAPEvents || event.getType() instanceof SimpleSOAPEventType) { |
---|
| 156 | newSequence.add(event); |
---|
| 157 | } |
---|
[1635] | 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | return newSequence; |
---|
| 162 | } |
---|
[1923] | 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 | } |
---|
[1635] | 181 | |
---|
| 182 | /** |
---|
| 183 | * <p> |
---|
[1367] | 184 | * prevent instantiation |
---|
| 185 | * </p> |
---|
| 186 | */ |
---|
[1635] | 187 | private HTTPUtils() {} |
---|
| 188 | |
---|
[1367] | 189 | } |
---|