1 /*
2 * (C)opyright 2010, Nikolaos Georgosopoulos
3 *
4 * This file is part of URLChecker.
5
6 URLChecker is free software: you can redistribute it and/or modify
7 it under the terms of the Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 URLChecker is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the Lesser General Public License
17 along with URLChecker. If not, see <http://www.gnu.org/licenses/>.
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 * The Class CheckUrlsProcess executes an Internet check for a given result
37 * using a configured client. This implementation uses the old httpClient from
38 * jakarta.
39 *
40 * <p>
41 * <b> $Id: CheckUrlsProcess.java 181 2010-12-12 23:39:00Z georgosn $</b>
42 * </p>
43 *
44 * @author $LastChangedBy: georgosn $
45 * @version $LastChangedRevision: 181 $
46 */
47 public class CheckUrlsProcess implements Runnable {
48
49 /** The Constant LOGGER. */
50 private static final Logger LOGGER = Logger.getLogger(CheckUrlsProcess.class
51 .getName());
52
53 /** The client. */
54 private final HttpClient client;
55
56 /** The result. */
57 private final Result result;
58
59 /** The valid codes. */
60 private final Integer[] validCodes;
61
62 /** The method. */
63 private final HttpMethod method;
64
65 /**
66 * Instantiates a new process.
67 *
68 * @param aClient
69 * the client
70 * @param res
71 * the res
72 * @throws org.apache.commons.configuration.ConfigurationException
73 * the configuration exception
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 * Gets the result.
93 *
94 * @return the result
95 */
96 public Result getResult() {
97 return result;
98 }
99
100 /*
101 * (non-Javadoc)
102 *
103 * @see java.lang.Runnable#run()
104 */
105 public void run() {
106 // TODO here the method can be specific to the source. This must come
107 // with some useful extension that allows the passage of valuable
108 // information especially for the POST and PUT methods.
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 }