1 package org.apache.maven.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.Authenticator;
24 import java.net.PasswordAuthentication;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.maven.wagon.ConnectionException;
29 import org.apache.maven.wagon.Wagon;
30 import org.apache.maven.wagon.providers.file.FileWagon;
31 import org.apache.maven.wagon.providers.http.HttpWagon;
32 import org.apache.maven.wagon.proxy.ProxyInfo;
33 import org.apache.maven.wagon.repository.Repository;
34
35 /**
36 * Http utils for retrieving files.
37 *
38 * This is now a simple wrapper around Wagon to preserve the interface for old code. Heavily deprecated, will be removed
39 * next release.
40 *
41 * @author costin@dnt.ro
42 * @author gg@grtmail.com (Added Java 1.1 style HTTP basic auth)
43 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
44 * @deprecated please use Wagon instead
45 */
46 public class HttpUtils
47 {
48 /** Logger */
49 private static final Log LOGGER = LogFactory.getLog( HttpUtils.class );
50
51 public static void useProxyUser( final String proxyHost, final String proxyPort, final String proxyUserName,
52 final String proxyPassword )
53 {
54 if ( ( proxyHost != null ) && ( proxyPort != null ) )
55 {
56 System.getProperties().put( "proxySet", "true" );
57 System.getProperties().put( "proxyHost", proxyHost );
58 System.getProperties().put( "proxyPort", proxyPort );
59
60 if ( proxyUserName != null )
61 {
62 Authenticator.setDefault( new Authenticator()
63 {
64 protected PasswordAuthentication getPasswordAuthentication()
65 {
66 return new PasswordAuthentication( proxyUserName, proxyPassword == null ? new char[0]
67 : proxyPassword
68 .toCharArray() );
69 }
70 } );
71 }
72 }
73 }
74
75 public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
76 String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
77 boolean useChecksum )
78 throws IOException
79 {
80 getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
81 null, null, useChecksum );
82 }
83
84 public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
85 String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
86 String loginHost, String loginDomain, boolean useChecksum )
87 throws IOException
88 {
89
90 getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
91 loginHost, loginDomain );
92
93
94 if ( useChecksum )
95 {
96 File checksumFile = new File( destinationFile + ".md5" );
97
98 try
99 {
100 getFile( url + ".md5", checksumFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName,
101 proxyPassword, loginHost, loginDomain );
102 }
103 catch ( Exception e )
104 {
105
106
107 }
108 }
109 }
110
111 public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
112 String proxyHost, String proxyPort, String proxyUserName, String proxyPassword )
113 throws IOException
114 {
115 getFile( url, destinationFile, ignoreErrors, useTimestamp, proxyHost, proxyPort, proxyUserName, proxyPassword,
116 null, null );
117 }
118
119 public static void getFile( String url, File destinationFile, boolean ignoreErrors, boolean useTimestamp,
120 String proxyHost, String proxyPort, String proxyUserName, String proxyPassword,
121 String loginHost, String loginDomain )
122 throws IOException
123 {
124
125 long timestamp = -1;
126 if ( useTimestamp && destinationFile.exists() )
127 {
128 timestamp = destinationFile.lastModified();
129 }
130
131 try
132 {
133 getFile( url, destinationFile, timestamp, proxyHost, proxyPort, proxyUserName, proxyPassword, loginHost,
134 loginDomain );
135 }
136 catch ( IOException ex )
137 {
138 if ( !ignoreErrors )
139 {
140 throw ex;
141 }
142 }
143 }
144
145 public static void getFile( String url, File destinationFile, long timestamp, String proxyHost, String proxyPort,
146 String proxyUserName, String proxyPassword, String loginHost, String loginDomain )
147 throws IOException
148 {
149 int index = url.lastIndexOf( "/" );
150 String file = url.substring( index + 1 );
151 url = url.substring( 0, index );
152
153 Repository repository = new Repository( "httputils", url );
154
155 Wagon wagon;
156 if ( "http".equals( repository.getProtocol() ) )
157 {
158 wagon = new HttpWagon();
159 }
160 else
161 {
162 wagon = new FileWagon();
163 }
164 wagon.addTransferListener( new BootstrapDownloadMeter() );
165
166 ProxyInfo proxyInfo = null;
167 if ( proxyHost != null )
168 {
169 proxyInfo = new ProxyInfo();
170 proxyInfo.setHost( proxyHost );
171 proxyInfo.setPort( Integer.valueOf( proxyPort ).intValue() );
172 proxyInfo.setUserName( proxyUserName );
173 proxyInfo.setPassword( proxyPassword );
174 proxyInfo.setNtlmHost( loginHost );
175 proxyInfo.setNtlmDomain( loginDomain );
176 }
177
178 try
179 {
180 wagon.connect( repository, proxyInfo );
181 wagon.getIfNewer( file, destinationFile, timestamp );
182 }
183 catch ( Exception e )
184 {
185 throw new IOException( "Transfer failure: " + e );
186 }
187 finally
188 {
189 try
190 {
191 wagon.disconnect();
192 }
193 catch ( ConnectionException e )
194 {
195 LOGGER.debug( "Failure to disconnect", e );
196 }
197 }
198 }
199
200 public static String[] parseUrl( String url )
201 {
202 String[] parsedUrl = new String[3];
203 parsedUrl[0] = null;
204 parsedUrl[1] = null;
205 parsedUrl[2] = url;
206
207
208
209
210
211
212
213 int i = url.indexOf( "@" );
214 if ( i > 0 )
215 {
216 String protocol = url.substring( 0, url.indexOf( "://" ) ) + "://";
217 String s = url.substring( protocol.length(), i );
218 int j = s.indexOf( ":" );
219 parsedUrl[0] = s.substring( 0, j );
220 parsedUrl[1] = s.substring( j + 1 );
221 parsedUrl[2] = protocol + url.substring( i + 1 );
222 }
223
224 return parsedUrl;
225 }
226 }