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