o MAVEN-313: Try and fix problems with classloaders and linkcheck

git-svn-id: https://svn.apache.org/repos/asf/maven/maven-1/plugins/trunk@113061 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
bwalding
2003-03-07 08:27:02 +00:00
parent 75dde06db4
commit 70a1e8305e
6 changed files with 244 additions and 208 deletions

View File

@@ -78,6 +78,12 @@
<classloader>root.maven</classloader>
</properties>
</dependency>
<dependency>
<id>commons-httpclient</id>
<version>SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly-tags-ant</artifactId>
@@ -101,13 +107,6 @@
<classloader>root.maven</classloader>
</properties>
</dependency>
<dependency>
<id>httpunit</id>
<version>1.5.1</version>
<properties>
<classloader>root.maven</classloader>
</properties>
</dependency>
<dependency>
<id>jtidy</id>
<version>4aug2000r7-dev</version>

View File

@@ -57,6 +57,8 @@ package org.apache.maven.linkcheck;
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -66,191 +68,214 @@ import java.util.Map;
import org.apache.maven.linkcheck.validation.LinkValidationItem;
import org.apache.maven.linkcheck.validation.LinkValidationResult;
import org.apache.maven.linkcheck.validation.LinkValidatorManager;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.DOMReader;
import org.w3c.tidy.Tidy;
/**
* @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
* @version $Id: FileToCheck.java,v 1.5 2003/02/22 00:37:31 bwalding Exp $
* @version $Id: FileToCheck.java,v 1.6 2003/03/07 08:27:02 bwalding Exp $
*
*/
public class FileToCheck
{
private File base;
private File fileToCheck;
private String status = STATUS_OK;
private String message = "";
private int successful;
private int unsuccessful;
public static final String STATUS_UNKNOWN = null;
public static final String STATUS_JTIDY_FAILURE = "Unable to tidy source";
public static final String STATUS_OK = "OK";
private File base;
private File fileToCheck;
private String status = STATUS_OK;
private String message = "";
private int successful;
private int unsuccessful;
public FileToCheck(File base, File fileToCheck)
{
this.base = base;
this.fileToCheck = fileToCheck;
}
public static final String STATUS_UNKNOWN = null;
public static final String STATUS_JTIDY_FAILURE = "Unable to tidy source";
public static final String STATUS_OK = "OK";
private List links = new ArrayList();
public void check(LinkValidatorManager lvm) throws Exception
{
successful = 0;
unsuccessful = 0;
status = STATUS_OK;
message = "";
try
public FileToCheck(File base, File fileToCheck)
{
WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest(fileToCheck.toURL().toString());
WebResponse resp = wc.getResponse(req);
WebLink[] wl = resp.getLinks();
Map uniqueLinks = new HashMap();
for (int i = 0; i < wl.length; i++)
{ //It puts the current URL in item 0
WebLink link = wl[i];
String href = link.getDOMSubtree().getAttributes().getNamedItem("href").getNodeValue();
this.base = base;
this.fileToCheck = fileToCheck;
}
uniqueLinks.put(href, href);
}
private List links = new ArrayList();
Iterator iter = uniqueLinks.keySet().iterator();
while (iter.hasNext())
{
String href = (String) iter.next();
public void check(LinkValidatorManager lvm) throws Exception
{
successful = 0;
unsuccessful = 0;
status = STATUS_OK;
message = "";
//System.out.println("Link Found: " + href);
LinkCheckResult lcr = new LinkCheckResult();
LinkValidationItem lvi = new LinkValidationItem(fileToCheck, href);
LinkValidationResult result = lvm.validateLink(lvi);
lcr.setTarget(href);
switch (result.getStatus())
try
{
case LinkValidationResult.UNKNOWN :
unsuccessful++;
lcr.setStatus("UNKNOWN REF");
break;
case LinkValidationResult.VALID :
successful++;
lcr.setStatus("OK");
break;
case LinkValidationResult.INVALID :
unsuccessful++;
lcr.setStatus("NOT FOUND");
break;
Tidy tidy = new Tidy();
Document doc = null;
try
{
//System.out.println("Parsing:" + fileToCheck);
FileInputStream in = new FileInputStream(fileToCheck);
tidy.setMakeClean(true);
tidy.setXmlTags(true);
tidy.setXmlOut(true);
tidy.setQuiet(false);
tidy.setXHTML(true);
org.w3c.dom.Document domDocument = tidy.parseDOM(in, null);
// now read a dom4j document from
// JTidy's W3C DOM object
DOMReader domReader = new DOMReader();
doc = domReader.read(domDocument);
}
catch (IOException e)
{
System.err.println(e.toString());
}
List xpathResults = new ArrayList();
xpathResults.addAll(doc.selectNodes("//a/@href"));
xpathResults.addAll(doc.selectNodes("//img/@src"));
Map uniqueLinks = new HashMap();
Iterator linkIter = xpathResults.iterator();
while (linkIter.hasNext())
{
Node node = (Node) linkIter.next();
String href = node.getText();
uniqueLinks.put(href, href);
}
Iterator iter = uniqueLinks.keySet().iterator();
while (iter.hasNext())
{
String href = (String) iter.next();
//System.out.println("Link Found: " + href);
LinkCheckResult lcr = new LinkCheckResult();
LinkValidationItem lvi = new LinkValidationItem(fileToCheck, href);
LinkValidationResult result = lvm.validateLink(lvi);
lcr.setTarget(href);
switch (result.getStatus())
{
case LinkValidationResult.UNKNOWN :
unsuccessful++;
lcr.setStatus("UNKNOWN REF");
break;
case LinkValidationResult.VALID :
successful++;
lcr.setStatus("OK");
break;
case LinkValidationResult.INVALID :
unsuccessful++;
lcr.setStatus("NOT FOUND");
break;
}
this.links.add(lcr);
}
}
catch (Exception e)
{
System.err.println(message);
throw (e);
}
}
/**
* Returns the message.
* @return String
*/
public String getMessage()
{
return message;
}
/**
* Returns the status.
* @return int
*/
public String getStatus()
{
return status;
}
/**
* Sets the message.
* @param message The message to set
*/
public void setMessage(String message)
{
this.message = message;
}
/**
* Sets the status.
* @param status The status to set
*/
public void setStatus(String status)
{
this.status = status;
}
public List getResults()
{
return links;
}
/**
* Returns the successful.
* @return int
*/
public int getSuccessful()
{
return successful;
}
/**
* Returns the unsuccessful.
* @return int
*/
public int getUnsuccessful()
{
return unsuccessful;
}
public String getName()
{
String baseName = base.getAbsolutePath();
String fileName = fileToCheck.getAbsolutePath();
if (fileName.startsWith(baseName))
fileName = fileName.substring(baseName.length() + 1);
fileName = fileName.replace('\\', '/');
return fileName;
}
public String toXML()
{
StringBuffer buf = new StringBuffer();
buf.append(" <file>\n");
buf.append(" <name>" + getName() + "</name>\n");
buf.append(" <successful>" + getSuccessful() + "</successful>\n");
buf.append(" <unsuccessful>" + getUnsuccessful() + "</unsuccessful>\n");
Iterator iter = getResults().iterator();
while (iter.hasNext())
{
LinkCheckResult result = (LinkCheckResult) iter.next();
buf.append(result.toXML());
}
links.add(lcr);
}
buf.append(" </file>\n");
return buf.toString();
}
catch (Exception e)
{
System.err.println(message);
throw (e);
}
}
/**
* Returns the message.
* @return String
*/
public String getMessage()
{
return message;
}
/**
* Returns the status.
* @return int
*/
public String getStatus()
{
return status;
}
/**
* Sets the message.
* @param message The message to set
*/
public void setMessage(String message)
{
this.message = message;
}
/**
* Sets the status.
* @param status The status to set
*/
public void setStatus(String status)
{
this.status = status;
}
public List getResults()
{
return links;
}
/**
* Returns the successful.
* @return int
*/
public int getSuccessful()
{
return successful;
}
/**
* Returns the unsuccessful.
* @return int
*/
public int getUnsuccessful()
{
return unsuccessful;
}
public String getName()
{
String baseName = base.getAbsolutePath();
String fileName = fileToCheck.getAbsolutePath();
if (fileName.startsWith(baseName))
fileName = fileName.substring(baseName.length() + 1);
fileName = fileName.replace('\\', '/');
return fileName;
}
public String toXML()
{
StringBuffer buf = new StringBuffer();
buf.append(" <file>\n");
buf.append(" <name>" + getName() + "</name>\n");
buf.append(" <successful>" + getSuccessful() + "</successful>\n");
buf.append(" <unsuccessful>" + getUnsuccessful() + "</unsuccessful>\n");
Iterator iter = getResults().iterator();
while (iter.hasNext())
{
LinkCheckResult result = (LinkCheckResult) iter.next();
buf.append(result.toXML());
}
buf.append(" </file>\n");
return buf.toString();
}
}

View File

@@ -74,14 +74,12 @@ import org.apache.maven.linkcheck.validation.LinkValidatorManager;
import org.apache.maven.linkcheck.validation.MailtoLinkValidator;
import org.apache.maven.project.Project;
import com.meterware.httpunit.HttpUnitOptions;
/**
* The main bean to be called whenever a set of documents should have
* their links checked.
*
* @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
* @version $Id: LinkCheck.java,v 1.6 2003/02/22 00:37:31 bwalding Exp $
* @version $Id: LinkCheck.java,v 1.7 2003/03/07 08:27:02 bwalding Exp $
*/
public class LinkCheck
{
@@ -139,7 +137,8 @@ public class LinkCheck
List filesToCheck = null; //of FileToCheck
public void doExecute() throws Exception
{
HttpUnitOptions.setScriptingEnabled(false);
if (output == null)
{
throw new NullPointerException("output must be set");

View File

@@ -56,19 +56,24 @@ package org.apache.maven.linkcheck.validation;
* ====================================================================
*/
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.meterware.httpunit.Base64;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import org.apache.maven.util.HttpUtils;
/**
* Checks links which are normal URLs
* @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
* @version $Id: HTTPLinkValidator.java,v 1.3 2003/02/22 00:37:31 bwalding Exp $
* @version $Id: HTTPLinkValidator.java,v 1.4 2003/03/07 08:27:02 bwalding Exp $
*/
public class HTTPLinkValidator implements LinkValidator
{
@@ -77,10 +82,10 @@ public class HTTPLinkValidator implements LinkValidator
*/
private static Log LOG = LogFactory.getLog(HTTPLinkValidator.class);
private final static LinkValidationResult LVR_INVALID =
private static final LinkValidationResult LVR_INVALID =
new LinkValidationResult(LinkValidationResult.INVALID, true);
private final static LinkValidationResult LVR_VALID = new LinkValidationResult(LinkValidationResult.VALID, true);
private static final LinkValidationResult LVR_VALID = new LinkValidationResult(LinkValidationResult.VALID, true);
private boolean proxy;
private String proxyHost;
@@ -117,33 +122,48 @@ public class HTTPLinkValidator implements LinkValidator
{
String link = lvi.getLink();
LOG.debug("Checking web link:" + link);
WebConversation wc = new WebConversation();
if (proxy)
HttpClient cl = new HttpClient();
HostConfiguration hc = new HostConfiguration();
if (proxyHost != null)
{
wc.setProxyServer(proxyHost, proxyPort);
if (proxyUser != null)
{
String auth = proxyUser + ":" + proxyPass;
wc.setHeaderField("Proxy-Authorization", "Basic " + Base64.encode(auth));
}
hc.setProxy(proxyHost, proxyPort);
}
HttpState state = new HttpState();
if (proxyUser != null && proxyPass != null)
{
state.setProxyCredentials(null, new UsernamePasswordCredentials(proxyUser, proxyPass));
}
wc.setExceptionsThrownOnErrorStatus(false);
WebRequest req = new GetMethodWebRequest(link);
WebResponse resp = wc.getResponse(req);
GetMethod get = new GetMethod(link);
cl.setState(state);
// execute the GET
int status = 404;
try
{
status = cl.executeMethod(get);
}
catch (Exception e)
{
System.out.println(e);
}
//FIXME: This constant is defined somewhere, but I can't remember where...
if (resp.getResponseCode() == 200)
if (status == 200)
{
return LVR_VALID;
}
else
{
String msg = "Received: [" + resp.getResponseCode() + "] \"" + resp.getResponseMessage() + "\" for " + link;
String msg = "Received: [" + status + "] for " + link;
LOG.info(msg);
System.out.println(msg);
return LVR_INVALID;
}
}
catch (Exception e)
{

View File

@@ -1,6 +0,0 @@
<html>
<body>
</body>
</html>

View File

@@ -1 +0,0 @@
<a href="fred.html">Fred.html</a>