[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 | |
---|
[1635] | 17 | import java.util.LinkedList; |
---|
| 18 | import java.util.List; |
---|
| 19 | |
---|
| 20 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 21 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SOAPEventType; |
---|
| 22 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType; |
---|
[1561] | 23 | import de.ugoe.cs.autoquest.plugin.http.logdata.Address; |
---|
[1367] | 24 | |
---|
| 25 | /** |
---|
| 26 | * <p> |
---|
[1383] | 27 | * contains convenience methods used for processing HTTP exchanges |
---|
[1367] | 28 | * </p> |
---|
| 29 | * |
---|
| 30 | * @author Patrick Harms |
---|
| 31 | */ |
---|
| 32 | public class HTTPUtils { |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * <p> |
---|
[1635] | 36 | * converts an address to a simple string containing either host or ip and the port number if |
---|
| 37 | * any. |
---|
[1367] | 38 | * </p> |
---|
| 39 | * |
---|
[1635] | 40 | * @param address |
---|
| 41 | * the address to convert |
---|
[1367] | 42 | * |
---|
| 43 | * @return either "host:port" or "ip:port" or "host" or "ip" or "port" or null |
---|
| 44 | */ |
---|
| 45 | public static String toString(Address address) { |
---|
| 46 | if (address != null) { |
---|
| 47 | StringBuffer buffer = new StringBuffer(); |
---|
| 48 | String prefix = ""; |
---|
| 49 | if (address.getHost() != null) { |
---|
| 50 | buffer.append(address.getHost()); |
---|
| 51 | prefix = ":"; |
---|
| 52 | } |
---|
| 53 | else if (address.getIp() != null) { |
---|
| 54 | buffer.append(prefix); |
---|
| 55 | buffer.append(address.getIp()); |
---|
| 56 | prefix = ":"; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | if (address.getPort() != null) { |
---|
| 60 | buffer.append(prefix); |
---|
| 61 | buffer.append(address.getPort()); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if (buffer.length() > 0) { |
---|
| 65 | return buffer.toString(); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | return null; |
---|
| 70 | } |
---|
[1417] | 71 | |
---|
| 72 | /** |
---|
| 73 | * <p> |
---|
[1635] | 74 | * compares two addresses and returns true, if they are equal and false else. The addresses are |
---|
| 75 | * equal, if either the ip-addresses and the ports match or the host names and the ports match. |
---|
[1417] | 76 | * </p> |
---|
| 77 | * |
---|
[1635] | 78 | * @param address1 |
---|
| 79 | * the first address to compare |
---|
| 80 | * @param address2 |
---|
| 81 | * the second address to compare |
---|
| 82 | * |
---|
[1417] | 83 | * @return as described |
---|
| 84 | */ |
---|
| 85 | public static boolean equals(Address address1, Address address2) { |
---|
| 86 | if (address1 == null) { |
---|
| 87 | return address2 == null; |
---|
| 88 | } |
---|
| 89 | else if (address2 == null) { |
---|
| 90 | return false; |
---|
| 91 | } |
---|
[1635] | 92 | |
---|
[1417] | 93 | if (!equals(address1.getPort(), address2.getPort())) { |
---|
| 94 | return false; |
---|
| 95 | } |
---|
[1635] | 96 | |
---|
[1417] | 97 | if (address1.getIp() != null) { |
---|
| 98 | return equals(address1.getIp(), address2.getIp()); |
---|
| 99 | } |
---|
| 100 | else { |
---|
| 101 | return equals(address1.getHost(), address2.getHost()); |
---|
| 102 | } |
---|
| 103 | } |
---|
[1635] | 104 | |
---|
[1367] | 105 | /** |
---|
| 106 | * <p> |
---|
[1635] | 107 | * convenience method to compare to objects. They are considered equal if they both are null, or |
---|
| 108 | * if their equals method returns true. |
---|
[1417] | 109 | * </p> |
---|
| 110 | * |
---|
[1635] | 111 | * @param object1 |
---|
| 112 | * the first object to compare |
---|
| 113 | * @param object2 |
---|
| 114 | * the second object to compare |
---|
| 115 | * |
---|
[1417] | 116 | * @return as described |
---|
| 117 | */ |
---|
| 118 | public static <T> boolean equals(T object1, T object2) { |
---|
[1635] | 119 | if (object1 == null) { |
---|
[1417] | 120 | return object2 == null; |
---|
| 121 | } |
---|
| 122 | else { |
---|
| 123 | return object1.equals(object2); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * <p> |
---|
[1635] | 129 | * Replaces events with a {@link SOAPEventType} with new events of {@link SimpleSOAPEventType} |
---|
| 130 | * and no target. |
---|
| 131 | * </p> |
---|
| 132 | * |
---|
| 133 | * @param sequence |
---|
| 134 | * sequence where the events are replaced |
---|
| 135 | * @return sequence with {@link SimpleSOAPEventType}s instead of {@link SOAPEventType}s |
---|
| 136 | */ |
---|
| 137 | public static List<Event> convertToSimpleSOAPEvent(List<Event> sequence) { |
---|
| 138 | List<Event> newSequence = null; |
---|
| 139 | if (sequence != null) { |
---|
| 140 | newSequence = new LinkedList<>(); |
---|
| 141 | for (Event event : sequence) { |
---|
| 142 | if (event.getType() instanceof SOAPEventType) { |
---|
| 143 | SOAPEventType eventType = (SOAPEventType) event.getType(); |
---|
| 144 | newSequence.add(new Event(new SimpleSOAPEventType(eventType.getCalledMethod(), |
---|
| 145 | eventType.getServiceName()))); |
---|
| 146 | } |
---|
| 147 | else { |
---|
| 148 | newSequence.add(event); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | return newSequence; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * <p> |
---|
[1367] | 157 | * prevent instantiation |
---|
| 158 | * </p> |
---|
| 159 | */ |
---|
[1635] | 160 | private HTTPUtils() {} |
---|
| 161 | |
---|
[1367] | 162 | } |
---|