[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 | |
---|
[1561] | 17 | import de.ugoe.cs.autoquest.plugin.http.logdata.Address; |
---|
[1367] | 18 | |
---|
| 19 | /** |
---|
| 20 | * <p> |
---|
[1383] | 21 | * contains convenience methods used for processing HTTP exchanges |
---|
[1367] | 22 | * </p> |
---|
| 23 | * |
---|
| 24 | * @author Patrick Harms |
---|
| 25 | */ |
---|
| 26 | public class HTTPUtils { |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * <p> |
---|
| 30 | * converts an address to a simple string containing either host or ip and the port number |
---|
| 31 | * if any. |
---|
| 32 | * </p> |
---|
| 33 | * |
---|
| 34 | * @param address the address to convert |
---|
| 35 | * |
---|
| 36 | * @return either "host:port" or "ip:port" or "host" or "ip" or "port" or null |
---|
| 37 | */ |
---|
| 38 | public static String toString(Address address) { |
---|
| 39 | if (address != null) { |
---|
| 40 | StringBuffer buffer = new StringBuffer(); |
---|
| 41 | String prefix = ""; |
---|
| 42 | if (address.getHost() != null) { |
---|
| 43 | buffer.append(address.getHost()); |
---|
| 44 | prefix = ":"; |
---|
| 45 | } |
---|
| 46 | else if (address.getIp() != null) { |
---|
| 47 | buffer.append(prefix); |
---|
| 48 | buffer.append(address.getIp()); |
---|
| 49 | prefix = ":"; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | if (address.getPort() != null) { |
---|
| 53 | buffer.append(prefix); |
---|
| 54 | buffer.append(address.getPort()); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | if (buffer.length() > 0) { |
---|
| 58 | return buffer.toString(); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | return null; |
---|
| 63 | } |
---|
[1417] | 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
| 67 | * compares two addresses and returns true, if they are equal and false else. The addresses |
---|
| 68 | * are equal, if either the ip-addresses and the ports match or the host names and the |
---|
| 69 | * ports match. |
---|
| 70 | * </p> |
---|
| 71 | * |
---|
| 72 | * @param address1 the first address to compare |
---|
| 73 | * @param address2 the second address to compare |
---|
| 74 | * |
---|
| 75 | * @return as described |
---|
| 76 | */ |
---|
| 77 | public static boolean equals(Address address1, Address address2) { |
---|
| 78 | if (address1 == null) { |
---|
| 79 | return address2 == null; |
---|
| 80 | } |
---|
| 81 | else if (address2 == null) { |
---|
| 82 | return false; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if (!equals(address1.getPort(), address2.getPort())) { |
---|
| 86 | return false; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if (address1.getIp() != null) { |
---|
| 90 | return equals(address1.getIp(), address2.getIp()); |
---|
| 91 | } |
---|
| 92 | else { |
---|
| 93 | return equals(address1.getHost(), address2.getHost()); |
---|
| 94 | } |
---|
| 95 | } |
---|
[1367] | 96 | |
---|
| 97 | /** |
---|
| 98 | * <p> |
---|
[1417] | 99 | * convenience method to compare to objects. They are considered equal if they both are null, |
---|
| 100 | * or if their equals method returns true. |
---|
| 101 | * </p> |
---|
| 102 | * |
---|
| 103 | * @param object1 the first object to compare |
---|
| 104 | * @param object2 the second object to compare |
---|
| 105 | * |
---|
| 106 | * @return as described |
---|
| 107 | */ |
---|
| 108 | public static <T> boolean equals(T object1, T object2) { |
---|
| 109 | if (object1 == null) { |
---|
| 110 | return object2 == null; |
---|
| 111 | } |
---|
| 112 | else { |
---|
| 113 | return object1.equals(object2); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * <p> |
---|
[1367] | 119 | * prevent instantiation |
---|
| 120 | * </p> |
---|
| 121 | */ |
---|
| 122 | private HTTPUtils() { } |
---|
| 123 | |
---|
| 124 | } |
---|