ruper2/src/java/core/org/krysalis/ruper2/util/net/http HttpClientUserAgent.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/ruper2/src/java/core/org/krysalis/ruper2/util/net/http
In directory sc8-pr-cvs1:/tmp/cvs-serv16855/src/java/core/org/krysalis/ruper2/util/net/http

Added Files:
	HttpClientUserAgent.java 
Log Message:
1) Monitoring (so we can sniff out failures in testing, to improve)
2) First step in removing VFS...

--- NEW FILE: HttpClientUserAgent.java ---
/*
 * Created on Aug 23, 2003
 *
 */
package org.krysalis.ruper2.util.net.http;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.util.HttpURLConnection;
import org.krysalis.ruper2.version.Version;
import org.krysalis.version.util.SystemUtils;

/**
 * @author arb_jack
 * 
 * 
 */
public class HttpClientUserAgent {
	private HttpClient m_client = null;

	public HttpClientUserAgent() {
		initClass();
	}

	private void initClass() {
		m_client = new HttpClient();

		//
		// Enable a timeout
		//
		m_client.getParams().setConnectionTimeout(30000);
	}

	public boolean exists(String url) {
		boolean exists = false;

		HeadMethod head = getHead(url);

		try {
			int resultCode = m_client.executeMethod(head);

			exists = (HttpURLConnection.HTTP_OK == resultCode);
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		finally {
			head.releaseConnection();
		}

		return exists;
	}

	public String getContents(String url) {
		String content = null;

		GetMethod get = getGet(url);

		try {
			int resultCode = m_client.executeMethod(get);

			if (HttpURLConnection.HTTP_OK == resultCode)
				content = get.getResponseBodyAsString();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		finally {
			get.releaseConnection();
		}

		return content;
	}

	public void transferContents(String url, OutputStream output) {
		GetMethod get = getGet(url);

		try {
			int resultCode = m_client.executeMethod(get);

			if (HttpURLConnection.HTTP_OK == resultCode) {

				InputStream input = get.getResponseBodyAsStream();

				// Transfer the bytes in blocks..
				byte b[] = new byte[1000];
				int numRead = -1;
				do {
					numRead = input.read(b);
					if (numRead != -1)
						output.write(b, 0, numRead);
				}
				while (numRead != -1);
			}
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		finally {
			get.releaseConnection();
		}

	}

	private GetMethod getGet(String url) {
		GetMethod get = new GetMethod(url);
		configureMethod(get);
		return get;
	}

	private HeadMethod getHead(String url) {
		HeadMethod head = new HeadMethod(url);
		configureMethod(head);
		return head;
	}

	/**
	 * Prepares a Method object.
	*/
	private void configureMethod(final HttpMethod method) {
		//method.setPath( );
		method.setFollowRedirects(true);
		method.setRequestHeader(
			"User-Agent",
			"Krysalis-Ruper/" + Version.VERSION.getLongVersion());
	}

	public static void main(String args[]) {
		HttpClientUserAgent agent = new HttpClientUserAgent();

		String url = "http://www.ibiblio.org/maven/junit/jars";

		PrintWriter out = SystemUtils.getSystemOut();

		if (agent.exists(url)) {
			out.println("Exists: " + url);
			out.println("Contents : " + agent.getContents(url));
		}
	}
}



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.