1
2
3
4
5
6 package us.johnmeyer.utilities;
7
8 import java.net.URL;
9 import java.net.HttpURLConnection;
10 import org.apache.commons.validator.routines.UrlValidator;
11
12
13
14
15
16
17
18
19
20 public final class UrlWrapper {
21
22
23
24
25
26
27
28
29
30 public enum Status {
31
32
33
34
35
36
37
38
39
40
41 OK(HttpURLConnection.HTTP_OK),
42
43
44
45
46 UNSUPPORTED(-1),
47
48
49 UNKNOWN_HOST(-1),
50
51
52 NOT_FOUND(HttpURLConnection.HTTP_NOT_FOUND);
53
54
55 private int code;
56
57
58
59
60 private String reason;
61
62
63
64
65
66 Status(final int codeParam) {
67
68 this.code = codeParam;
69 if (codeParam != -1) {
70 this.reason = "HTTP_" + this.toString();
71 } else {
72 this.reason = this.toString();
73 }
74
75 }
76
77
78
79
80
81 public int getCode() {
82 return code;
83 }
84
85
86
87
88
89 public String getReason() {
90 return reason;
91 }
92
93 }
94
95
96 private URL url;
97
98
99
100
101
102 private UrlWrapper(final URL urlParam) {
103
104 this.url = urlParam;
105
106 }
107
108
109
110
111
112
113
114
115
116
117
118 public static UrlWrapper newUrlWrapper(final String urlStringParam) {
119
120 URL url = toValidatedUrl(urlStringParam);
121 String assertUrlString = url.toExternalForm();
122
123 if (url == null) {
124 throw new IllegalArgumentException("Param " + urlStringParam
125 + " output is null.");
126 }
127
128
129
130 if (!urlStringParam.equals(assertUrlString)) {
131 throw new IllegalStateException("String input " + urlStringParam
132 + " java.net.URL output " + assertUrlString
133 + ". Match input syntax to this output.");
134 }
135
136
137
138 return new UrlWrapper(url);
139
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 private static java.net.URL toValidatedUrl(final String newUrl) {
155
156 UrlValidator urlValidator = new UrlValidator();
157
158 URL returnUrl;
159
160 String invalidMessage
161 = " (URL) is invalid per "
162 + "org.apache.commons.validator.routines.UrlValidator.";
163
164 if (!urlValidator.isValid(newUrl)) {
165
166 String validationIssue = newUrl
167 + invalidMessage;
168
169 throw new IllegalArgumentException(validationIssue);
170
171 }
172
173 try {
174 returnUrl = new URL(newUrl);
175 } catch (java.net.MalformedURLException m) {
176
177 throw new IllegalArgumentException(m.getMessage());
178
179 }
180
181 return returnUrl;
182
183 }
184
185
186
187
188
189
190
191
192 public static boolean isValidUrl(final String urlParam) {
193
194 try {
195
196 UrlWrapper urlWrapper = UrlWrapper.newUrlWrapper(urlParam);
197
198 return true;
199
200 } catch (IllegalArgumentException e) {
201
202 return false;
203 }
204
205 }
206
207
208
209
210
211 public URL getUrl() {
212
213 return url;
214
215 }
216
217
218
219
220
221 public String toString() {
222 return url.toExternalForm();
223 }
224
225
226
227
228
229
230
231
232 public Status getResponse() throws java.io.IOException {
233
234 Integer responseCode = null;
235 Status statusLocal = null;
236 StringBuilder builder = new StringBuilder();
237 HttpURLConnection connection;
238
239
240
241
242
243 connection = (HttpURLConnection) url.openConnection();
244
245 try {
246 responseCode = connection.getResponseCode();
247 } catch (java.net.UnknownHostException e) {
248 statusLocal = Status.UNKNOWN_HOST;
249 } catch (java.io.IOException e) {
250 e.printStackTrace();
251 statusLocal = Status.UNSUPPORTED;
252 }
253
254
255 if (statusLocal == null) {
256
257 for (Status status : Status.values()) {
258 if (status.getCode() == responseCode) {
259 statusLocal = status;
260 break;
261 }
262 }
263
264 if (statusLocal == null) {
265
266 statusLocal = Status.UNSUPPORTED;
267 }
268
269 }
270
271 return statusLocal;
272 }
273
274 }