From 8c83124f3ea243afae1f236c2375da71c528a687 Mon Sep 17 00:00:00 2001 From: bwalding Date: Sat, 13 Sep 2003 21:48:08 +0000 Subject: [PATCH] o Now follow 302 redirects (one level only) o Cleaned up logging a little bit and added more logging o Doesn't address the MacOSX bug (test failure bug), but the logging increase might help git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@114004 13f79535-47bb-0310-9956-ffa450edef68 --- linkcheck/.cvsignore | 2 + linkcheck/maven.xml | 11 ---- linkcheck/project.xml | 4 +- .../apache/maven/linkcheck/FileToCheck.java | 22 ++++--- .../org/apache/maven/linkcheck/LinkCheck.java | 23 ++++--- .../maven/linkcheck/LinkCheckResult.java | 7 ++- .../validation/HTTPLinkValidator.java | 62 ++++++++++++------- .../validation/LinkValidationItem.java | 26 ++------ .../validation/LinkValidationResult.java | 8 +-- .../validation/LinkValidatorCache.java | 12 ++-- .../src/plugin-resources/linkcheck-temp.xml | 6 -- 11 files changed, 88 insertions(+), 95 deletions(-) diff --git a/linkcheck/.cvsignore b/linkcheck/.cvsignore index e5e33942..7244964b 100644 --- a/linkcheck/.cvsignore +++ b/linkcheck/.cvsignore @@ -1,3 +1,5 @@ target velocity.log maven.log +.classpath +.project diff --git a/linkcheck/maven.xml b/linkcheck/maven.xml index 4aef5cc5..78ca961d 100644 --- a/linkcheck/maven.xml +++ b/linkcheck/maven.xml @@ -1,14 +1,3 @@ - - - - - - - - - - - diff --git a/linkcheck/project.xml b/linkcheck/project.xml index 2ce3a39f..0a033ecb 100644 --- a/linkcheck/project.xml +++ b/linkcheck/project.xml @@ -39,7 +39,7 @@ commons-httpclient - 2.0-alpha3 + 2.0-rc1 commons-jelly @@ -132,7 +132,7 @@ werkz - 1.0-beta-7 + 1.0-beta-10 http://werkz.sourceforge.net/ root.maven diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java b/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java index 03a79f6f..341ec8e6 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java @@ -78,7 +78,7 @@ import org.w3c.tidy.Tidy; /** * @author Ben Walding - * @version $Id: FileToCheck.java,v 1.11 2003/03/07 22:46:39 bwalding Exp $ + * @version $Id: FileToCheck.java,v 1.12 2003/09/13 21:48:08 bwalding Exp $ * */ public class FileToCheck @@ -146,9 +146,8 @@ public class FileToCheck { //We catch Throwable, because there is a chance that the domReader will throw //a stack overflow exception for some files - if (!(e instanceof Exception)) { - LOG.info("Caught " + e.toString()); - } + LOG.info("Caught " + e.toString() + " processing " + getName()); + LOG.info("Exception Message: " + e.getLocalizedMessage()); LinkCheckResult lcr = new LinkCheckResult(); lcr.setStatus("PARSE FAILURE"); lcr.setTarget("N/A"); @@ -179,7 +178,7 @@ public class FileToCheck { String href = (String) iter.next(); - //System.out.println("Link Found: " + href); + LOG.debug("Link Found: " + href); LinkCheckResult lcr = new LinkCheckResult(); @@ -189,21 +188,24 @@ public class FileToCheck switch (result.getStatus()) { - case LinkValidationResult.UNKNOWN : - unsuccessful++; - lcr.setStatus("UNKNOWN REF"); - break; case LinkValidationResult.VALID : successful++; lcr.setStatus("OK"); + this.links.add(lcr); //At some point we won't want to store valid links. The tests require that we do at present + break; + case LinkValidationResult.UNKNOWN : + unsuccessful++; + lcr.setStatus("UNKNOWN REF"); + this.links.add(lcr); break; case LinkValidationResult.INVALID : unsuccessful++; lcr.setStatus("NOT FOUND"); + this.links.add(lcr); break; } - this.links.add(lcr); + } } catch (Exception e) diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java index a433e035..19fa627c 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java @@ -79,19 +79,17 @@ import org.apache.maven.project.Project; * their links checked. * * @author Ben Walding - * @version $Id: LinkCheck.java,v 1.9 2003/04/17 12:58:33 bwalding Exp $ + * @version $Id: LinkCheck.java,v 1.10 2003/09/13 21:48:08 bwalding Exp $ */ public class LinkCheck { /** Log */ private static final Log LOG = LogFactory.getLog(LinkCheck.class); - /** - * Output file for xml document - */ + /** Output file for xml document */ private File output; - /** output encoding for the xml document */ + /** Output encoding for the xml document */ private String outputEncoding; private File baseDir; @@ -134,21 +132,21 @@ public class LinkCheck * @throws UnsupportedEncodingException if the underlying platform doesn't * support ISO-8859-1 encoding */ - List filesToCheck = null; //of FileToCheck + private List filesToCheck = null; //of FileToCheck public void doExecute() throws Exception { - - if (output == null) { throw new NullPointerException("output must be set"); } - LinkValidatorManager lvm = getLinkValidatorManager(); + LinkValidatorManager validator = getLinkValidatorManager(); filesToCheck = new ArrayList(); - lvm.loadCache(cache); + validator.loadCache(cache); List files = new ArrayList(); + LOG.debug("Locating all files to be checked..."); findFiles(files, baseDir); + LOG.debug("Located all files to be checked."); Iterator fileIter = files.iterator(); while (fileIter.hasNext()) { @@ -156,7 +154,8 @@ public class LinkCheck try { filesToCheck.add(flc); - flc.check(lvm); + LOG.info("Validating " + flc.getName()); + flc.check(validator); } catch (Exception e) { @@ -165,7 +164,7 @@ public class LinkCheck } createDocument(files); - lvm.saveCache(cache); + validator.saveCache(cache); } public List getFiles() diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheckResult.java b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheckResult.java index 62fcccbe..ca825884 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheckResult.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheckResult.java @@ -61,7 +61,7 @@ import org.apache.commons.util.XmlUtils; /** * An class containing the results of a single check of a link. * @author Ben Walding - * @version $Id: LinkCheckResult.java,v 1.3 2003/02/03 14:13:54 bwalding Exp $ + * @version $Id: LinkCheckResult.java,v 1.4 2003/09/13 21:48:08 bwalding Exp $ */ public class LinkCheckResult { @@ -104,6 +104,10 @@ public class LinkCheckResult this.target = target; } + /** + * Creates an XML representation of this link check result + * @return xml fragment representation of this result + */ public String toXML() { StringBuffer buf = new StringBuffer(); @@ -112,6 +116,7 @@ public class LinkCheckResult buf.append(" " + XmlUtils.escapeXml(getTarget()) + "\n"); buf.append(" " + getStatus() + "\n"); buf.append(" \n"); + return buf.toString(); } diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java b/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java index 3ef58def..0c206f5b 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java @@ -56,6 +56,7 @@ package org.apache.maven.linkcheck.validation; * ==================================================================== */ +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpState; @@ -67,7 +68,7 @@ import org.apache.commons.logging.LogFactory; /** * Checks links which are normal URLs * @author Ben Walding - * @version $Id: HTTPLinkValidator.java,v 1.7 2003/04/17 13:07:03 bwalding Exp $ + * @version $Id: HTTPLinkValidator.java,v 1.8 2003/09/13 21:48:08 bwalding Exp $ */ public class HTTPLinkValidator implements LinkValidator { @@ -81,7 +82,6 @@ public class HTTPLinkValidator implements LinkValidator private static final LinkValidationResult LVR_VALID = new LinkValidationResult(LinkValidationResult.VALID, true); - private boolean proxy; private String proxyHost; private int proxyPort; private String proxyUser; @@ -91,19 +91,18 @@ public class HTTPLinkValidator implements LinkValidator { if (proxyHost == null) { - proxy = false; - System.out.println("maven-linkcheck-plugin: Not using a proxy"); + LOG.info("maven-linkcheck-plugin: Not using a proxy"); + this.proxyHost = null; } else { - proxy = true; this.proxyHost = proxyHost; this.proxyPort = Integer.parseInt(proxyPort); this.proxyUser = proxyUser; this.proxyPass = proxyPass; - System.out.println("maven-linkcheck-plugin: Proxy Host:" + proxyHost); - System.out.println("maven-linkcheck-plugin: Proxy Port:" + proxyPort); - System.out.println("maven-linkcheck-plugin: Proxy User:" + proxyUser); + LOG.info("maven-linkcheck-plugin: Proxy Host:" + proxyHost); + LOG.info("maven-linkcheck-plugin: Proxy Port:" + proxyPort); + LOG.info("maven-linkcheck-plugin: Proxy User:" + proxyUser); } } @@ -125,36 +124,38 @@ public class HTTPLinkValidator implements LinkValidator hc.setProxy(proxyHost, proxyPort); } HttpState state = new HttpState(); - + if (proxyUser != null && proxyPass != null) { - state.setProxyCredentials(null, new UsernamePasswordCredentials(proxyUser, proxyPass)); + state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUser, proxyPass)); } - GetMethod get = new GetMethod(link); cl.setHostConfiguration(hc); cl.setState(state); - get.setFollowRedirects(true); - + // execute the GET - int status = 404; - try + GetMethod gm = checkLink(cl, link); + + if (gm.getStatusCode() == 302) { - status = cl.executeMethod(get); - } - catch (Exception e) - { - System.out.println(e); + Header locationHeader = gm.getResponseHeader("Location"); + if (locationHeader == null) { + LOG.info("Site sent redirect, but did not set Location header"); + } else { + String newLink = locationHeader.getValue(); + LOG.debug("Following 1 redirect to " + newLink); + gm = checkLink(cl, newLink); + } } //FIXME: This constant is defined somewhere, but I can't remember where... - if (status == 200) + if (gm.getStatusCode() == 200) { return LVR_VALID; } else { - String msg = "Received: [" + status + "] for " + link; + String msg = "Received: [" + gm.getStatusCode() + "] for " + link; LOG.info(msg); System.out.println(msg); return LVR_INVALID; @@ -170,6 +171,23 @@ public class HTTPLinkValidator implements LinkValidator } + private GetMethod checkLink(HttpClient cl, String link) + { + GetMethod gm = new GetMethod(link); + try + { + LOG.info("Checking link " + link); + gm.setFollowRedirects(true); + cl.executeMethod(gm); + LOG.debug("Checked link " + link); + } + catch (Exception e) + { + LOG.warn("Error validating " + link, e); + } + return gm; + } + /** * @see org.apache.maven.linkcheck.LinkValidator#getResourceKey(org.apache.maven.linkcheck.LinkValidationItem) */ diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationItem.java b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationItem.java index cd085d13..ff191600 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationItem.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationItem.java @@ -60,11 +60,11 @@ import java.io.File; /** * @author Ben Walding - * @version $Id: LinkValidationItem.java,v 1.2 2003/02/03 14:13:54 bwalding Exp $ + * @version $Id: LinkValidationItem.java,v 1.3 2003/09/13 21:48:08 bwalding Exp $ */ public class LinkValidationItem { - private File source; - private String link; + private final File source; + private final String link; public LinkValidationItem(File source, String link) { if (source == null) { @@ -89,7 +89,8 @@ public class LinkValidationItem { return false; if (!lvi.source.equals(source)) - return false; + return false; + return true; } @@ -116,22 +117,5 @@ public class LinkValidationItem { public File getSource() { return source; } - - /** - * Sets the link. - * @param link The link to set - */ - public void setLink(String link) { - this.link = link; - } - - /** - * Sets the source. - * @param source The source to set - */ - public void setSource(File source) { - this.source = source; - } - } diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationResult.java b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationResult.java index 66a1e3a8..50caad5f 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationResult.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidationResult.java @@ -57,14 +57,14 @@ package org.apache.maven.linkcheck.validation; */ /** - * @author Ben Walding - * @version $Id: LinkValidationResult.java,v 1.2 2003/02/03 14:13:54 bwalding Exp $ * This is an immutable class.
*

* This class is used to return status responses from the * validation handlers. A persistent result means that it * can be stored in the persistent cache and used across runs. *

+ * @author Ben Walding + * @version $Id: LinkValidationResult.java,v 1.3 2003/09/13 21:48:08 bwalding Exp $ */ public class LinkValidationResult { public static final int NOTMINE = 0; @@ -72,14 +72,14 @@ public class LinkValidationResult { public static final int VALID = 2; public static final int UNKNOWN = 3; - private int status = UNKNOWN; + private final boolean persistent; + private final int status; public LinkValidationResult(int status, boolean persistent) { this.status = status; this.persistent = persistent; } - boolean persistent; public boolean isPersistent() { return persistent; } diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidatorCache.java b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidatorCache.java index ab78f44d..03e3e0d5 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidatorCache.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/validation/LinkValidatorCache.java @@ -66,11 +66,11 @@ import java.util.Properties; /** * @author Ben Walding - * @version $Id: LinkValidatorCache.java,v 1.2 2003/02/03 14:13:54 bwalding Exp $ + * @version $Id: LinkValidatorCache.java,v 1.3 2003/09/13 21:48:08 bwalding Exp $ */ public class LinkValidatorCache { private LinkValidatorManager lvm; - private Map cache = new HashMap(); + private final Map cache = new HashMap(); public LinkValidatorCache(LinkValidatorManager lvm) { this.lvm = lvm; @@ -80,10 +80,10 @@ public class LinkValidatorCache { } /** - * - * @param lvi - * @return int Will return a status level, VALID, INVALID, UNKNOWN - */ + * + * @param lvi + * @return int Will return a status level, VALID, INVALID, UNKNOWN + */ public LinkValidationResult getCachedResult(LinkValidationItem lvi) { Iterator iter = lvm.getValidators().iterator(); diff --git a/linkcheck/src/plugin-resources/linkcheck-temp.xml b/linkcheck/src/plugin-resources/linkcheck-temp.xml index 9171eb0b..de2da9e6 100644 --- a/linkcheck/src/plugin-resources/linkcheck-temp.xml +++ b/linkcheck/src/plugin-resources/linkcheck-temp.xml @@ -10,12 +10,6 @@

This file is used as a placeholder until the final link check can occur.

- -

- At present, you MUST set maven.linkcheck.enable=true in your project properties - to have the linkchecker run. Once it has stabilised, it will be turned on by default - (except in offline mode). -