1 /*
2 * Copyright 2009-2013 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.validation.constraints.impl;
17
18 import java.net.HttpURLConnection;
19 import java.net.URL;
20
21 import javax.validation.ConstraintValidator;
22 import javax.validation.ConstraintValidatorContext;
23
24 import de.smartics.validation.constraints.Available;
25
26 /**
27 * Checks that the annotated resource is availability.
28 */
29 public class UrlAvailabilityValidator implements
30 ConstraintValidator<Available, URL>
31 {
32 // ******************************** Fields ********************************
33
34 // --- constants ----------------------------------------------------------
35
36 // --- members ------------------------------------------------------------
37
38 /**
39 * The expected return code to match with.
40 */
41 private String expectedReturnCode;
42
43 /**
44 * The connection timeout in milliseconds. A timeout of zero is interpreted as
45 * an infinite timeout.
46 */
47 private int timeoutInMs;
48
49 // ***************************** Initializer ******************************
50
51 // ***************************** Constructors *****************************
52
53 // ***************************** Inner Classes ****************************
54
55 // ******************************** Methods *******************************
56
57 // --- init ---------------------------------------------------------------
58
59 @Override
60 public void initialize(final Available constraintAnnotation)
61 {
62 expectedReturnCode = constraintAnnotation.value();
63 timeoutInMs = constraintAnnotation.timeout();
64 }
65
66 // --- get&set ------------------------------------------------------------
67
68 // --- business -----------------------------------------------------------
69
70 @Override
71 public boolean isValid(final URL value,
72 final ConstraintValidatorContext constraintValidatorContext)
73 {
74 HttpURLConnection connection = null;
75 try
76 {
77 connection = (HttpURLConnection) value.openConnection();
78 connection.setRequestMethod("HEAD");
79 connection.setUseCaches(false);
80 connection.connect();
81 connection.setConnectTimeout(timeoutInMs);
82 final int code = connection.getResponseCode();
83 final boolean valid = String.valueOf(code).equals(expectedReturnCode);
84 return valid;
85 }
86 catch (final Exception e)
87 {
88 return false;
89 }
90 finally
91 {
92 if (connection != null)
93 {
94 connection.disconnect();
95 }
96 }
97 }
98
99 // --- object basics ------------------------------------------------------
100
101 }