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. 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

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 lu.ng.urlchecker.commands.CheckUrlsCommand;
import lu.ng.urlchecker.commands.Command;
import lu.ng.urlchecker.commands.Context;
import lu.ng.urlchecker.commands.Result;
import lu.ng.urlchecker.commands.StandardContext;
import lu.ng.urlchecker.commands.URIValidateCommand;
import lu.ng.urlchecker.commands.URLMatchCommand;
import lu.ng.urlchecker.communication.CommunicationBuilder;
import lu.ng.urlchecker.events.ChainEvent;
import lu.ng.urlchecker.events.ChainListener;
import lu.ng.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();
			resolver.setSuccessor(new URIValidateCommand());

			resolver2 = new URLMatchCommand();
			resolver2.setSuccessor(new URIValidateCommand());
			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(inStream);

			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 stay alive.
				builder.shutdownCommunication();
			}

		}
	}
    

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