package org.apache.maven.util;
/* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
* ====================================================================
*/
import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.providers.file.FileWagon;
import org.apache.maven.wagon.providers.http.HttpWagon;
import org.apache.maven.wagon.proxy.ProxyInfo;
import org.apache.maven.wagon.repository.Repository;
/**
* Http utils for retrieving files.
*
* This is now a simple wrapper around Wagon to preserve the interface for old code. Heavily deprecated, will be removed
* next release.
*
* @author costin@dnt.ro
* @author gg@grtmail.com (Added Java 1.1 style HTTP basic auth)
* @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
* @deprecated please use Wagon instead
*/
public class HttpUtils
{
/** Logger */
private static final Log LOGGER = LogFactory.getLog( HttpUtils.class );
public static void useProxyUser( final String proxyHost, final String proxyPort, final String proxyUserName,
final String proxyPassword )
{
if ( ( proxyHost != null ) && ( proxyPort != null ) )
{
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", proxyHost );
System.getProperties().put( "proxyPort", proxyPort );
if ( proxyUserName != null )
{
Authenticator.setDefault( new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication( proxyUserName, proxyPassword == null ? new char[0]
: proxyPassword
.toCharArray() );
}
} );
}
}
}
public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
boolean useChecksum )
throws IOException
{
getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
null, null, useChecksum );
}
public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
String loginHost, String loginDomain, boolean useChecksum )
throws IOException
{
// Get the requested file.
getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
loginHost, loginDomain );
// Get the checksum if requested.
if ( useChecksum )
{
File checksumFile = new File( destinationFile + ".md5" );
try
{
getFile( url + ".md5", checksumFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName,
proxyPassword, loginHost, loginDomain );
}
catch ( Exception e )
{
// do nothing we will check later in the process
// for the checksums.
}
}
}
public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
String proxyHost, String proxyPort, String proxyUserName, String proxyPassword )
throws IOException
{
getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
null, null );
}
public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
String loginHost, String loginDomain )
throws IOException
{
//set the timestamp to the file date.
long timestamp = -1;
if ( useTimestamp && destinationFile.exists() )
{
timestamp = destinationFile.lastModified();
}
try
{
getFile( url, destinationFile, timestamp, proxyHost, proxyPort, proxyUserName, proxyPassword, loginHost,
loginDomain );
}
catch ( IOException ex )
{
if ( !ignoreErrors )
{
throw ex;
}
}
}
public static void getFile( String url, File destinationFile, long timestamp, String proxyHost, String proxyPort,
String proxyUserName, String proxyPassword, String loginHost, String loginDomain )
throws IOException
{
int index = url.lastIndexOf( "/" );
String file = url.substring( index + 1 );
url = url.substring( 0, index );
Repository repository = new Repository( "httputils", url );
Wagon wagon;
if ( "http".equals( repository.getProtocol() ) )
{
wagon = new HttpWagon();
}
else
{
wagon = new FileWagon();
}
wagon.addTransferListener( new BootstrapDownloadMeter() );
ProxyInfo proxyInfo = null;
if ( proxyHost != null )
{
proxyInfo = new ProxyInfo();
proxyInfo.setHost( proxyHost );
proxyInfo.setPort( Integer.valueOf( proxyPort ).intValue() );
proxyInfo.setUserName( proxyUserName );
proxyInfo.setPassword( proxyPassword );
proxyInfo.setNtlmHost( loginHost );
proxyInfo.setNtlmDomain( loginDomain );
}
try
{
wagon.connect( repository, proxyInfo );
wagon.getIfNewer( file, destinationFile, timestamp );
}
catch ( Exception e )
{
throw new IOException( "Transfer failure: " + e );
}
finally
{
try
{
wagon.disconnect();
}
catch ( ConnectionException e )
{
LOGGER.debug( "Failure to disconnect", e );
}
}
}
public static String[] parseUrl( String url )
{
String[] parsedUrl = new String[3];
parsedUrl[0] = null;
parsedUrl[1] = null;
parsedUrl[2] = url;
// We want to be able to deal with Basic Auth where the username
// and password are part of the URL. An example of the URL string
// we would like to be able to parse is like the following:
//
// http://username:password@repository.mycompany.com
int i = url.indexOf( "@" );
if ( i > 0 )
{
String protocol = url.substring( 0, url.indexOf( "://" ) ) + "://";
String s = url.substring( protocol.length(), i );
int j = s.indexOf( ":" );
parsedUrl[0] = s.substring( 0, j );
parsedUrl[1] = s.substring( j + 1 );
parsedUrl[2] = protocol + url.substring( i + 1 );
}
return parsedUrl;
}
}