You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by br...@apache.org on 2004/04/04 05:02:16 UTC
cvs commit: maven/src/java/org/apache/maven/util HttpUtils.java
brett 2004/04/03 19:02:16
Modified: src/java/org/apache/maven/util Tag: MAVEN-1_0-BRANCH
HttpUtils.java
Log:
PR: MAVEN-558
Submitted by: David Zeleznik
improve error handling in HttpUtils
Revision Changes Path
No revision
No revision
1.28.4.4 +64 -38 maven/src/java/org/apache/maven/util/HttpUtils.java
Index: HttpUtils.java
===================================================================
RCS file: /home/cvs/maven/src/java/org/apache/maven/util/HttpUtils.java,v
retrieving revision 1.28.4.3
retrieving revision 1.28.4.4
diff -u -r1.28.4.3 -r1.28.4.4
--- HttpUtils.java 1 Mar 2004 22:36:39 -0000 1.28.4.3
+++ HttpUtils.java 4 Apr 2004 03:02:16 -0000 1.28.4.4
@@ -24,9 +24,11 @@
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
+import java.util.Date;
/**
* Http utils for retrieving files.
@@ -104,7 +106,7 @@
String proxyUserName,
String proxyPassword,
boolean useChecksum )
- throws Exception
+ throws IOException
{
// Get the requested file.
getFile( url,
@@ -165,7 +167,58 @@
String proxyPort,
String proxyUserName,
String proxyPassword )
- throws Exception
+ throws IOException
+ {
+ //set the timestamp to the file date.
+ Date timestamp = null;
+ if ( useTimestamp && destinationFile.exists() )
+ {
+ timestamp = new Date(destinationFile.lastModified());
+ }
+
+ try
+ {
+ getFile(url,
+ destinationFile,
+ timestamp,
+ proxyHost,
+ proxyPort,
+ proxyUserName,
+ proxyPassword);
+ }
+ catch ( IOException ex )
+ {
+ if ( !ignoreErrors )
+ {
+ throw ex;
+ }
+ }
+ }
+
+ /**
+ * Retrieve a remote file.
+ *
+ * @param url the URL of the file to retrieve
+ * @param destinationFile where to store it
+ * @param timestamp if provided, the remote URL is only retrieved if it was
+ * modified more recently than timestamp. Otherwise, null indicates that
+ * the remote URL should be retrieved unconditionally.
+ * @param proxyHost Proxy Host (if proxy is required), or null
+ * @param proxyPort Proxy Port (if proxy is required), or null
+ * @param proxyUserName Proxy Username (if authentification is required),
+ * or null
+ * @param proxyPassword Proxy Password (if authentification is required),
+ * or null
+ * @exception IOException If an I/O exception occurs.
+ */
+ public static void getFile( String url,
+ File destinationFile,
+ Date timestamp,
+ String proxyHost,
+ String proxyPort,
+ String proxyUserName,
+ String proxyPassword )
+ throws IOException
{
String[] s = parseUrl( url );
String username = s[0];
@@ -174,25 +227,17 @@
URL source = new URL( parsedUrl );
- //set the timestamp to the file date.
- long timestamp = 0;
- boolean hasTimestamp = false;
- if ( useTimestamp && destinationFile.exists() )
- {
- timestamp = destinationFile.lastModified();
- hasTimestamp = true;
- }
-
//set proxy connection
useProxyUser( proxyHost, proxyPort, proxyUserName, proxyPassword );
//set up the URL connection
URLConnection connection = source.openConnection();
+
//modify the headers
//NB: things like user authentication could go in here too.
- if ( useTimestamp && hasTimestamp )
+ if ( timestamp != null )
{
- connection.setIfModifiedSince( timestamp );
+ connection.setIfModifiedSince( timestamp.getTime() );
}
// prepare Java 1.1 style credentials
if ( username != null || password != null )
@@ -228,10 +273,6 @@
// test for 404 ourselves, and throw FileNotFoundException as needed
if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
{
- if (ignoreErrors)
- {
- return;
- }
throw new FileNotFoundException(url.toString() + " (HTTP Error: "
+ httpConnection.getResponseCode() + " " + httpConnection.getResponseMessage() + ")");
}
@@ -242,20 +283,12 @@
// test for 401 result (HTTP only)
if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED )
{
- if (ignoreErrors)
- {
- return;
- }
- throw new Exception( "Not authorized." );
+ throw new IOException( "Not authorized." );
}
// test for 407 result (HTTP only)
if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH )
{
- if (ignoreErrors)
- {
- return;
- }
- throw new Exception( "Not authorized by proxy." );
+ throw new IOException( "Not authorized by proxy." );
}
}
@@ -265,6 +298,7 @@
// Some protocols (FTP) dont include dates, of course.
InputStream is = null;
+ IOException isException = null;
for ( int i = 0; i < 3; i++ )
{
try
@@ -274,18 +308,12 @@
}
catch ( IOException ex )
{
- // do nothing
+ isException = ex;
}
}
if ( is == null )
{
- if ( ignoreErrors )
- {
- return;
- }
-
- // This will never happen with maven's use of this class.
- throw new Exception( "Can't get " + destinationFile.getName() + " to " + destinationFile );
+ throw isException;
}
FileOutputStream fos = new FileOutputStream( destinationFile );
@@ -306,10 +334,9 @@
// if (and only if) the use file time option is set, then the
// saved file now has its timestamp set to that of the downloaded
// file
- if ( useTimestamp )
+ if ( timestamp != null )
{
long remoteTimestamp = connection.getLastModified();
-
if ( remoteTimestamp != 0 )
{
touchFile( destinationFile, remoteTimestamp );
@@ -363,7 +390,6 @@
* comes from file access failures.
*/
private static boolean touchFile( File file, long timemillis )
- throws Exception
{
long modifiedTime;
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org