krysalis-update/src/java/org/krysalis/depot/update/util/net/http HttpClientUserAgent.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:45 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/net/http
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/net/http
Added Files:
HttpClientUserAgent.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: HttpClientUserAgent.java ---
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.krysalis.depot.update.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.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.util.HttpURLConnection;
import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.SystemUtils;
import org.krysalis.depot.common.util.io.IOUtils;
/**
* @author arb_jack
*
* A simple UserAgent
*
*/
public class HttpClientUserAgent {
private HttpClient m_client = null;
public HttpClientUserAgent() {
m_client = new HttpClient(new MultiThreadedHttpConnectionManager());
//
// Enable a timeout
//
// :TODO: Restore when a release has this
// m_client.getParams().setConnectionManagerTimeout(30000);
}
/**
* Test if a URL 'exists' by performing an HTTP HEAD and testing the status
* code response.
*
* @param url
* @return
*/
public boolean exists(String url) throws Exception {
boolean exists = false;
HeadMethod head = null;
try {
head = getHead(url);
int resultCode = m_client.executeMethod(head);
exists = (HttpURLConnection.HTTP_OK == resultCode);
} catch (Exception ex) {
Logger.getLogger().error("Failed to HTTP HEAD [" + url + "]", ex);
throw ex;
} finally {
if (null != head)
head.releaseConnection();
}
return exists;
}
/**
* Return the contents of a GET to a URL as a String
*
* @param url
* @return
*/
public String getContents(String url) throws Exception {
String content = null;
GetMethod get = null;
try {
get = getGet(url);
int resultCode = m_client.executeMethod(get);
if (HttpURLConnection.HTTP_OK == resultCode)
content = get.getResponseBodyAsString();
} catch (Exception ex) {
Logger.getLogger().error("Failed to HTTP GET [" + url + "]", ex);
throw ex;
} finally {
if (null != get)
get.releaseConnection();
}
return content;
}
/**
* Copy the contents of a GET of a URL to another stream
*
* @param url
* @param output
*/
public void transferContents(String url, OutputStream output)
throws Exception {
GetMethod get = null;
try {
get = getGet(url);
int resultCode = m_client.executeMethod(get);
if (HttpURLConnection.HTTP_OK == resultCode) {
InputStream input = get.getResponseBodyAsStream();
IOUtils.transfer(input, output);
IOUtils.close(input);
}
} catch (Exception ex) {
Logger.getLogger().error("Failed to HTTP GET [" + url + "]", ex);
throw ex;
} finally {
if (null != get)
get.releaseConnection();
}
}
/**
* Return a configured HEAD method to a URL
*
* @param url
* @return
*/
private GetMethod getGet(String url) {
GetMethod get = new GetMethod(url);
configureMethod(get);
return get;
}
/**
* Return a configured HEAD method to a URL
*
* @param url
* @return
*/
private HeadMethod getHead(String url) {
HeadMethod head = new HeadMethod(url);
configureMethod(head);
return head;
}
/**
*
* Prepares a method with out standard preferences.
*
*/
private void configureMethod(HttpMethod method) {
//
method.setFollowRedirects(true);
//
// Identify use (in case we upset any folks).
//
method.setRequestHeader("User-Agent", "Apache-Depot-Update/"
+ org.apache.acorn.version.version.Version.getInstance().getLongVersion());
}
public static void main(String args[]) throws Exception {
HttpClientUserAgent agent = new HttpClientUserAgent();
String url = "http://www.ibiblio.org/maven/junit/jars";
if (args.length > 0)
url = args[0];
PrintWriter out = SystemUtils.getSystemOut();
if (agent.exists(url)) {
out.println("Exists: " + url);
out.println("Contents : " + agent.getContents(url));
}
}
}
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/