001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: *
019: * Created: 29-Nov-2004 by jejking
020: * Version: $Revision: 1.5 $
021: * Last Updated: $Date: 2005/01/10 12:28:36 $
022: */
023: package org.openharmonise.rm.resources.content.utils;
024:
025: import java.io.IOException;
026: import java.net.*;
027: import java.text.*;
028: import java.util.*;
029: import java.util.logging.*;
030: import java.util.regex.*;
031:
032: import javax.xml.parsers.*;
033:
034: import org.openharmonise.rm.DataAccessException;
035: import org.openharmonise.rm.resources.content.Asset;
036: import org.w3c.dom.*;
037:
038: /**
039: * URL Checking utility class.
040: *
041: * <p>
042: * Class runs through a list of Strings which are meant to be <code>URL</code>
043: * s, specifically http URLs (weblinks). For each String , it attempts to
044: * construct a <code>URL</code> object, and then attempts to connect to the
045: * resource represented by the URL. If any exceptions are thrown or if the HTTP
046: * response code is any other than 200 (OK), then the class records the String
047: * representing the URL and details about the issue. The code does <em>not</em>
048: * follow redirects as it cannot be assumed that all clients using the Timelines
049: * website will do so, but instead notes the new URL specified in the Location
050: * header.
051: * </p>
052: *
053: * <p>
054: * Once the class has finished connecting to all the URLs in the List passed in,
055: * it constructs a report on request. A standard "OK" report is produced if no
056: * issues were recorded, otherwise a report is constructed detailing the
057: * malfunctioning URLs and the problems encountered.
058: * </p>
059: *
060: * <p>
061: * <em>Note:</em> this class is part of the implementation of the BM
062: * requirement 3.41 as clarified in the SIM requirements clarification document,
063: * point 3.5.2.
064: * </p>
065: *
066: * Copyright SimulacraMedia 2003
067: *
068: * @author John King
069: * @version $Revision: 1.5 $
070: */
071: public class LinkChecker {
072: private List assetsToCheck;
073:
074: private List errorsList; // holds any LinkError objects produced
075:
076: private boolean bURLsChecked = false; // state flag, no reports can be
077: // produced until links have been
078: // checked
079:
080: private Date dateRun;
081:
082: private DateFormat dateFormat;
083:
084: private DateFormat xmlDateTimeFormat;
085:
086: private Hashtable errorCodes; // holds the http error codes
087:
088: /**
089: * Logger for this class
090: */
091: private static final Logger m_logger = Logger
092: .getLogger(LinkChecker.class.getName());
093:
094: {
095: errorCodes = new Hashtable();
096: errorCodes
097: .put(
098: new Integer(201),
099: "Following a POST command, this indicates success, but the textual part of the response line indicates the URI by which the newly created document should be known.");
100: errorCodes
101: .put(
102: new Integer(202),
103: "The request has been accepted for processing, but the processing has not been completed. The request may or may not eventually be acted upon, as it may be disallowed when processing actually takes place. there is no facility for status returns from asynchronous operations such as this.");
104: errorCodes
105: .put(
106: new Integer(203),
107: "When received in the response to a GET command, this indicates that the returned metainformation is not a definitive set of the object from a server with a copy of the object, but is from a private overlaid web. This may include annotation information about the object, for example.");
108: errorCodes
109: .put(
110: new Integer(204),
111: "Server has received the request but there is no information to send back, and the client should stay in the same document view. This is mainly to allow input for scripts without changing the document at the same time.");
112: errorCodes.put(new Integer(300), "Multiple Choices");
113: errorCodes
114: .put(new Integer(301),
115: "The requested resource has been assigned the following new URL: ");
116: errorCodes
117: .put(new Integer(302),
118: "The requested resource resides temporarily under the ");
119: errorCodes.put(new Integer(304), "304 Not Modified");
120: errorCodes
121: .put(new Integer(305),
122: "The requested resource MUST be accessed through the proxy given by ");
123: errorCodes.put(new Integer(306), "306 (Unused)");
124: errorCodes
125: .put(new Integer(307),
126: "The requested resource resides temporarily under the following URI: ");
127: errorCodes
128: .put(new Integer(400),
129: "The request had bad syntax or was inherently impossible to be satisfied.");
130: errorCodes
131: .put(
132: new Integer(401),
133: "The parameter to this message gives a specification of authorization schemes which are acceptable. The client should retry the request with a suitable Authorization header.");
134: errorCodes
135: .put(
136: new Integer(402),
137: "The parameter to this message gives a specification of charging schemes acceptable. The client may retry the request with a suitable ChargeTo header.");
138: errorCodes
139: .put(new Integer(403),
140: "The request is for something forbidden. Authorization will not help.");
141: errorCodes
142: .put(new Integer(404),
143: "The server has not found anything matching the URI given");
144: errorCodes
145: .put(
146: new Integer(500),
147: "The server encountered an unexpected condition which prevented it from fulfilling the request.");
148: errorCodes.put(new Integer(501),
149: "The server does not support the facility required.");
150: errorCodes
151: .put(
152: new Integer(502),
153: "The server cannot process the request due to a high load (whether HTTP servicing or other requests). The implication is that this is a temporary condition which maybe alleviated at other times.");
154: errorCodes
155: .put(
156: new Integer(503),
157: "This is equivalent to Internal Error 500, but in the case of a server which is in turn accessing some other service, this indicates that the respose from the other service did not return within a time that the gateway was prepared to wait. As from the point of view of the clientand the HTTP transaction the other service is hidden within the server, this maybe treated identically to Internal error 500, but has more diagnostic value.");
158: }
159:
160: /**
161: * Constructs a new <code>LinkChecker</code> instance.
162: *
163: * @param urlsToCheck
164: * list of URLs to check in the form of String objects
165: * @throws NullPointerException
166: * if urlsToCheck was null
167: */
168: public LinkChecker(List assetsToCheck) {
169: if (assetsToCheck == null) {
170: //TODO make this fit into Papaya exception handling patterns
171: throw new NullPointerException("urlsToCheck was null");
172: }
173: this .assetsToCheck = assetsToCheck;
174: errorsList = new ArrayList();
175: dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm");
176: xmlDateTimeFormat = new SimpleDateFormat(
177: "yyyy-MM-dd'T'HH:mm:ss");
178: }
179:
180: /**
181: * Checks the links specified in the list of URLs specified in the call to
182: * the constructor.
183: *
184: * <p>
185: * Iterates through the list of Strings supplied. It first attempts to
186: * construct a URL object using the string. If this fails, due to a
187: * MalformedURLException, a <code>LinkError</code> is created and added to
188: * the errors list. If the URL object is created successfully, it attempts
189: * to connect to to it. If an attempt to connect results in an Exception or
190: * if the connection succeeds but returns any HTTP status code other than
191: * 200 (OK), then a <code>LinkError</code> is created and noted in the
192: * errors list.
193: * </p>
194: *
195: * <p>
196: * This implementation attempts to be robust. It anticipates that users will
197: * not necessarily provide a full, compliant URL but may abbreviate web
198: * links to, say, www.example.com rather than http://www.example.com. It
199: * looks for the :// characters which mark the boundary between the protocol
200: * specifier and the machine address and if these are missing, prepends
201: * http:// to the string before checking to form, hopefully, a syntactically
202: * correct URL before attempting to resolve the link.
203: * </p>
204: *
205: * @see org.openharmonise.rm.resources.content.utils.LinkError
206: */
207: public void checkLinks() {
208: // go through the list of urls and check each one
209: Iterator it = assetsToCheck.iterator();
210: Pattern pattern = Pattern.compile(".*://.*");
211: HttpURLConnection.setFollowRedirects(false);
212: while (it.hasNext()) {
213: Asset asset = (Asset) it.next();
214: String URLString = null;
215: try {
216: URLString = asset.getFullURL();
217: } catch (DataAccessException dae) {
218: createError(asset, dae);
219: handleException(dae);
220: }
221:
222: /*
223: * it is a very strong possibility that users will not provide
224: * correctly formed URLs because they draw on their browsers which
225: * turn www.theregister.co.uk into http://www.theregister.co.uk
226: * behind the scenes. We must anticipate this and if a protocol
227: * element is missing, we will preprend http://
228: */
229: Matcher matcher = pattern.matcher(URLString);
230: if (!matcher.matches()) {
231: // we probably don't have a protocol element, so we'll prepend
232: // one
233: URLString = "http://" + URLString;
234: }
235:
236: try {
237:
238: URL url = new URL(URLString);
239:
240: String sHost = url.getHost();
241:
242: if (sHost.indexOf(" ") >= 0 || sHost.indexOf(".") < 0) {
243: throw new MalformedURLException("'" + URLString
244: + "' is a malformed URL");
245: }
246:
247: HttpURLConnection conn = (HttpURLConnection) url
248: .openConnection();
249: int status = conn.getResponseCode();
250: if (status != HttpURLConnection.HTTP_OK) {
251: createError(asset, conn);
252: }
253:
254: } catch (MalformedURLException mue) {
255: createError(asset, mue);
256: } catch (IOException ioe) {
257: createError(asset, ioe);
258: handleException(ioe);
259: } catch (ClassCastException cce) { // particularly indicative of non
260: // web URL
261: createError(asset, cce);
262: handleException(cce);
263: }
264: }
265: bURLsChecked = true;
266: dateRun = new Date();
267: }
268:
269: /**
270: * Creates a simple dated, plain text formatted report
271: *
272: * <p>
273: * Produces a dated simple report on the outcome of the link checking
274: * activity. If there were no errors, then this is stated. If there were
275: * errors, then each is noted along with the URL concerned.
276: * </p>
277: *
278: * @return a dated, plain text formatted report
279: * @throws IllegalStateException
280: * if the <code>checkLinks</code> method has not previously
281: * been called
282: */
283: public String getReport() { //TODO this is an interim and very basic plain
284: // text report
285: if (bURLsChecked == false) {
286: throw new IllegalStateException(
287: "URLs have not yet been checked");
288: }
289: StringBuffer report = new StringBuffer(1500);
290:
291: report.append("Link Checking Report\n");
292: report.append("Run at: ");
293: report.append(dateFormat.format(dateRun));
294: report.append("\n");
295:
296: if (errorsList.size() == 0) { // ie, no errors were recorded
297: report.append("No errors were detected");
298: } else { // else, we had errors, put them in the report
299: Iterator errorsIt = errorsList.iterator();
300: while (errorsIt.hasNext()) {
301: try {
302: report.append("\n");
303: LinkStatus status = (LinkStatus) errorsIt.next();
304: report.append(status.getAsset().getURI());
305: report.append("\n");
306: report.append(status.getErrorMessage());
307: if (status.getNewURL() != null) {
308: report.append("\nNew Location: "
309: + status.getNewURL());
310: }
311: report.append("\n");
312: } catch (DataAccessException e) {
313: m_logger.log(Level.WARNING,
314: e.getLocalizedMessage(), e);
315: }
316: }
317: }
318: return report.toString();
319: }
320:
321: public Document getXMLReport() {
322: if (bURLsChecked == false) {
323: throw new IllegalStateException(
324: "URLs have not yet been checked");
325: }
326: Document doc = null;
327:
328: try {
329: DocumentBuilderFactory factory = DocumentBuilderFactory
330: .newInstance();
331: DocumentBuilder docBuilder = factory.newDocumentBuilder();
332: doc = docBuilder.newDocument();
333: } catch (FactoryConfigurationError e) {
334: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
335: } catch (ParserConfigurationException e) {
336: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
337: }
338:
339: Element reportElement = doc.createElement("ReportInstance");
340: doc.appendChild(reportElement);
341: reportElement.setAttribute("date", xmlDateTimeFormat
342: .format(dateRun));
343: Element listElement = doc.createElement("List");
344: reportElement.appendChild(listElement);
345:
346: Iterator errorsIt = errorsList.iterator();
347: while (errorsIt.hasNext()) {
348: try {
349: LinkStatus status = (LinkStatus) errorsIt.next();
350: //Element errorElement = doc.createElement("error");
351: //errorElement.setAttribute("url", error.getURLString());
352: Element reportRowElement = doc
353: .createElement("ReportRow");
354: listElement.appendChild(reportRowElement);
355:
356: Element objectElement = doc.createElement("Object");
357: reportRowElement.appendChild(objectElement);
358: Asset asset = status.getAsset(); // get the asset object
359:
360: Element nameElement = null;
361: Text nameTxt = null;
362:
363: nameElement = doc.createElement("DisplayName");
364:
365: if (asset.getDisplayName() == null
366: || asset.getDisplayName().equals("")) {
367: nameTxt = doc.createTextNode(asset.getName());
368: } else {
369: nameTxt = doc
370: .createTextNode(asset.getDisplayName());
371: }
372:
373: nameElement.appendChild(nameTxt);
374: objectElement.appendChild(nameElement);
375:
376: Element pathElement = doc.createElement("Path");
377: objectElement.appendChild(pathElement);
378: Text pathTxt = doc.createTextNode(asset.getFullPath());
379: pathElement.appendChild(pathTxt);
380:
381: Element properiesElement = doc
382: .createElement("Properties");
383: reportRowElement.appendChild(properiesElement);
384:
385: Element userElement = doc.createElement("User");
386: reportRowElement.appendChild(userElement);
387:
388: Element dateModifiedElement = doc
389: .createElement("DateModified");
390: reportRowElement.appendChild(dateModifiedElement);
391:
392: Element actionElement = doc.createElement("Action");
393: reportRowElement.appendChild(actionElement);
394:
395: Element statusElement = doc.createElement("Status");
396: reportRowElement.appendChild(statusElement);
397: Text statusTxt = doc.createTextNode(status
398: .getErrorMessage());
399: statusElement.appendChild(statusTxt);
400: } catch (DOMException e) {
401: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
402: } catch (DataAccessException e) {
403: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
404: }
405: }
406:
407: return doc;
408: }
409:
410: /**
411: * Returns the results of the link checking activity as a List of
412: * <code>LinkError</code>s.
413: *
414: * @return unmodifiable List containing <code>LinkError</code> instances
415: * recording each error encountered. If no errors were encountered,
416: * this List will be empty.
417: * @throws IllegalStateException
418: * if the <code>checkLinks</code> method has not previously
419: * been called
420: * @see org.openharmonise.rm.resources.content.utils.LinkError
421: */
422: public List getErrorsList() {
423: if (bURLsChecked == false) {
424: throw new IllegalStateException(
425: "URLs have not yet been checked");
426: } else {
427: return Collections.unmodifiableList(errorsList);
428: }
429: }
430:
431: // temporary hack. this will log and rethrow the proper sort of Harmonise
432: // Exception
433: private void handleException(Exception e) {
434: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
435: }
436:
437: /**
438: * Creates and registers an error in connecting to a URL.
439: *
440: * <p>
441: * Creates a <code>LinkError</code> and stores it in the internal errors
442: * list. Uses the <code>HttpURLConnection</code> to obtain sufficient
443: * detail to provide an error that is more meaningful than an integer HTTP
444: * status code.
445: * </p>
446: *
447: * @param url
448: * URL where the problem was encountered
449: * @param conn
450: * HttpURLConnection to the problem URL, used to get more details
451: */
452: private void createError(Asset asset, HttpURLConnection conn) {
453: LinkStatus status = null;
454: try {
455: // response message is slightly more informative than the code alone
456: status = new LinkStatus(asset, (String) errorCodes
457: .get(new Integer(conn.getResponseCode()))
458: + conn.getHeaderField("Location"));
459: if (conn.getResponseCode() >= 300
460: && conn.getResponseCode() < 308) { // i.e.
461: // a
462: // redirect
463: status.setNewURL(conn.getHeaderField("Location"));
464: }
465: } catch (IOException ioe) {
466: status = new LinkStatus(asset, ioe.getMessage());
467: }
468: errorsList.add(status);
469: }
470:
471: /**
472: * Creates and registers an error caused by an exception thrown whilst
473: * attempting to connect to a URL.
474: *
475: * <p>
476: * Creates a <code>LinkError</code> and stores it in the internal errors
477: * list. Extracts the message from the exception to provide a meaningful
478: * error message.
479: * </p>
480: *
481: * @param url
482: * URL where the exception was encountered
483: * @param ex
484: * the exception thrown
485: */
486: private void createError(Asset asset, Exception ex) {
487: String errorMessage = ex.getMessage();
488:
489: try {
490: if (ex instanceof UnknownHostException) {
491: errorMessage = "The host " + asset.getURI()
492: + " is unknown.";
493: }
494: } catch (DataAccessException e) {
495: errorMessage = "There was a problem trying to get the resource URI.";
496: }
497:
498: LinkStatus status = new LinkStatus(asset, errorMessage);
499: errorsList.add(status);
500: }
501:
502: }
|