1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.urlchecker.commands;
21
22 import java.io.IOException;
23 import java.net.ConnectException;
24 import java.util.Arrays;
25
26 import net.sf.urlchecker.communication.CommunicationFactory;
27
28 import org.apache.commons.configuration.ConfigurationException;
29 import org.apache.commons.httpclient.HttpClient;
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.HttpMethod;
32 import org.apache.commons.lang.ArrayUtils;
33 import org.apache.log4j.Logger;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class CheckUrlsProcess implements Runnable {
48
49
50 private static final Logger LOGGER = Logger.getLogger(CheckUrlsProcess.class
51 .getName());
52
53
54 private final HttpClient client;
55
56
57 private final Result result;
58
59
60 private final Integer[] validCodes;
61
62
63 private final HttpMethod method;
64
65
66
67
68
69
70
71
72
73
74
75 public CheckUrlsProcess(final HttpClient aClient, final Result res)
76 throws ConfigurationException {
77 this.client = aClient;
78 result = res;
79 if (CommunicationFactory.getInstance().isConfigured()) {
80 validCodes = CommunicationFactory.getInstance().getOnlyValidCodes(
81 result.getTarget());
82
83 method = CommunicationFactory.getInstance().getMethod(
84 result.getTarget());
85 } else {
86 throw new ConfigurationException();
87 }
88
89 }
90
91
92
93
94
95
96 public Result getResult() {
97 return result;
98 }
99
100
101
102
103
104
105 public void run() {
106
107
108
109
110 result.setValid(false);
111 try {
112 result.setResult(client.executeMethod(method));
113 if (LOGGER.isDebugEnabled()) {
114 LOGGER.debug("Return code:" + result.getResult());
115 LOGGER.debug("valid codes:" + Arrays.toString(validCodes));
116 }
117 result.setValid(ArrayUtils.contains(validCodes, result.getResult()));
118
119 } catch (final ConnectException e) {
120 result.setUserData(e);
121 LOGGER.error("Error occured in process:", e);
122 } catch (final HttpException e) {
123 result.setUserData(e);
124 LOGGER.error("Error occured in process:", e);
125 } catch (final IOException e) {
126 result.setUserData(e);
127 LOGGER.error("Error occured in process:", e);
128 } finally {
129 method.releaseConnection();
130 }
131
132 }
133
134 }