Pretext

The following is mainly extracted from the javadoc but it seems more and more people hit this site presumably to find information as to how to use the library.

Configurations

In order to be able to operate, the library needs an xml file in the classpath or elsewhere. the xml file provide the configurations for the httpclient in order to reach the internet from wherever this library is operating (usually a server). The following is a sample of such a file.

        	<?xml version="1.0" encoding="UTF-8"?>
			<client xmlns="checkerSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ng.lu/checkerSchema checker.xsd ">
				<!-- <method>HEAD<method> -->
				<preemptive>false</preemptive>
				<hosts>
					<host>
						<preemptive>false</preemptive>
						<username>admin</username>
						<password>admin</password>
						<server>localhost</server>
						<port>80</port>
					</host>
				</hosts>
				<proxy>
					<username>admin</username>
					<password>admin</password>
					<server>localhost</server>
					<port>80</port>
				</proxy>
			</client>
        

Notice that there is an xsd for validation of the configuration file (the checker.xsd). Bear in mind that if you provide no configuration file the library will use this one exactly as it is in its class path.

Coding with the old version of HttpClient (3.1)

using the library in code is all about creating the chain of commands you want to use and executing it as below

import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import net.sf.urlchecker.commands.CheckUrlsCommand;
import net.sf.urlchecker.commands.Command;
import net.sf.urlchecker.commands.Context;
import net.sf.urlchecker.commands.Result;
import net.sf.urlchecker.commands.StandardContext;
import net.sf.urlchecker.commands.URIValidateCommand;
import net.sf.urlchecker.commands.URLMatchCommand;
import net.sf.urlchecker.communication.CommunicationBuilder;
import net.sf.urlchecker.events.ChainEvent;
import net.sf.urlchecker.events.ChainListener;
import net.sf.urlchecker.events.EventTypes;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.TransformerUtils;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;

public class example{
		private Command              resolver;
		private Command              resolver2;
		private CommunicationBuilder builder;
		private Set<ChainEvent>      chainListenerMonitor;

		public void main(Object[] args){
			builder = new CommunicationBuilder();
			resolver = new URLMatchCommand(new CheckUrlsCommand(null,
                builder.getMultithreadedClient()));


			chainListenerMonitor = new LinkedHashSet<ChainEvent>();

			//this you can avoid or get a stream from elsewhere in the system
			final InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("dummyData.xml");

			final Context context = new StandardContext();
			context.setSource(new InputStreamReader(inStream, "UTF-8"));

			Set<Result> results;
			try {
				context.setClient(builder.getMultithreadedClient());
				resolver.getSuccessor().setSuccessor(new CheckUrlsCommand());

				results = resolver.process(context);

				int validResults = 0;
				int invalidResults = 0;
				for (final Result r : results) {
					LOGGER.info("Result:" + r.toString());
					if (r.isValid()) {
						validResults++;
					} else {
						invalidResults++;
					}
				}

			} catch (final ConfigurationException e) {
				//the configuration is invalid, correct it.
			}finally{
				//do not forget to shutdown communications or your connections may be stay alive.
				builder.shutdownCommunication();
			}

		}
	}
    

Note that commons collections, commons configuration, commons components and log4j must be in your class path to use this library.

Coding with the new HttpClient (4.0.1)

Much of the coding for the new version of the HttpClient is the same as in the old,the library makes sure to use the proper abstractions. The key part is to use the CheckUrlsCommand and the CommunicationBuilder from the v2 package.