Java tutorial
/* * BSD License for Ivy * Copyright (c) 2005, JAYASOFT * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of JAYASOFT nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * version 0.5 */ package fr.jayasoft.ivy.url; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.GetMethod; import fr.jayasoft.ivy.util.FileUtil; import fr.jayasoft.ivy.util.Message; /** * @author Xavier Hanin * */ public class HttpClientDownloader implements URLDownloader { private String _realm = null; private String _host = null; private String _userName = null; private String _passwd = null; public HttpClientDownloader() { } /** * @param realm may be null * @param host may be null * @param userName may be null * @param passwd may be null */ public HttpClientDownloader(String realm, String host, String userName, String passwd) { _realm = realm; _host = host; _userName = userName; _passwd = passwd; if (useAuthentication()) { Message.verbose( "using authentication in realm " + _realm + " and host " + _host + " for user " + _userName); } else { Message.verbose("no http authentication will be used"); } } public InputStream openStream(URL url) throws IOException { GetMethod get = doGet(url); byte[] response = get.getResponseBody(); get.releaseConnection(); return new ByteArrayInputStream(response); } public void download(URL src, File dest) throws IOException { GetMethod get = doGet(src); FileUtil.copy(get.getResponseBodyAsStream(), dest); get.releaseConnection(); } private GetMethod doGet(URL url) throws IOException, HttpException { HttpClient client = new HttpClient(); if (useAuthentication()) { client.getState().setCredentials(_realm, _host, new UsernamePasswordCredentials(_userName, _passwd)); } GetMethod get = new GetMethod(url.toExternalForm()); get.setDoAuthentication(useAuthentication()); client.executeMethod(get); return get; } private boolean useAuthentication() { return _userName != null && _userName.length() > 0; } }