You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by bw...@apache.org on 2003/05/25 16:54:55 UTC
cvs commit: maven-new/fetch/src/main/java/org/apache/maven/fetch/util IOUtility.java
bwalding 2003/05/25 07:54:54
Modified: fetch/xdocs examples.xml
fetch/src/main/java/org/apache/maven/fetch/fetchers
FileFetcher.java HttpFetcher.java
fetch/src/test/java/org/apache/maven/fetch FetchTest.java
fetch/src/main/java/org/apache/maven/fetch/util
IOUtility.java
Removed: fetch/src/main/java/org/apache/maven/fetch/fetchers
HttpUtils.java DownloadBean.java
fetch/src/main/java/org/apache/maven/fetch/exceptions
TransportFetchException.java
Log:
Add file:// support - untested (haven't even run it yet)
More doc
Revision Changes Path
1.2 +6 -0 maven-new/fetch/xdocs/examples.xml
Index: examples.xml
===================================================================
RCS file: /home/cvs/maven-new/fetch/xdocs/examples.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- examples.xml 25 May 2003 10:00:08 -0000 1.1
+++ examples.xml 25 May 2003 14:54:54 -0000 1.2
@@ -8,6 +8,12 @@
</properties>
<body>
+ <section name="Overview">
+ <p>
+ The best bet for the time being is to check the FetchTest to
+ see how to use the Fetch module
+ </p>
+ </section>
<section name="Example 1">
<p>
1.2 +53 -6 maven-new/fetch/src/main/java/org/apache/maven/fetch/fetchers/FileFetcher.java
Index: FileFetcher.java
===================================================================
RCS file: /home/cvs/maven-new/fetch/src/main/java/org/apache/maven/fetch/fetchers/FileFetcher.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileFetcher.java 25 May 2003 10:00:11 -0000 1.1
+++ FileFetcher.java 25 May 2003 14:54:54 -0000 1.2
@@ -55,11 +55,19 @@
*
* ====================================================================
*/
-
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
import org.apache.maven.fetch.FetchRequest;
import org.apache.maven.fetch.exceptions.FetchException;
-
+import org.apache.maven.fetch.exceptions.NotModifiedFetchException;
+import org.apache.maven.fetch.util.IOUtility;
/**
* @author <a href="bwalding@jakarta.org">Ben Walding</a>
@@ -68,13 +76,52 @@
public class FileFetcher implements Fetcher
{
- /* (non-Javadoc)
+ /**
* @see org.apache.maven.fetch.fetchers.Fetcher#fetchUrl(java.lang.String, java.io.OutputStream)
*/
public void fetchUrl(FetchRequest request) throws FetchException
{
- // TODO Auto-generated method stub
-
+
+ try
+ {
+ URL url = new URL(request.getUrl());
+
+ File f = new File(url.toExternalForm());
+
+ if (!f.exists())
+ {
+ throw new FetchException("Couldn't find " + f);
+ }
+
+ if (request.getOnlyIfModifiedSinceDate() != null)
+ {
+ long ftime = f.lastModified();
+ if (ftime > 0 && ftime > request.getOnlyIfModifiedSinceDate().getTime())
+ {
+ //Actually get the file
+ OutputStream os = request.getFinalOutputStream();
+ InputStream is = new FileInputStream(f);
+ IOUtility.transferStream(is, os);
+ IOUtility.close(os);
+ IOUtility.close(is);
+ }
+ else
+ {
+ throw new NotModifiedFetchException("Not modified");
+ }
+ }
+
+ }
+ catch (MalformedURLException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
}
}
1.6 +13 -9 maven-new/fetch/src/main/java/org/apache/maven/fetch/fetchers/HttpFetcher.java
Index: HttpFetcher.java
===================================================================
RCS file: /home/cvs/maven-new/fetch/src/main/java/org/apache/maven/fetch/fetchers/HttpFetcher.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- HttpFetcher.java 25 May 2003 14:38:12 -0000 1.5
+++ HttpFetcher.java 25 May 2003 14:54:54 -0000 1.6
@@ -94,10 +94,11 @@
processUser(request, state);
processProxy(request, state);
+ HttpURLConnection connection = null;
try
{
URL url = new URL(state.url);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection = (HttpURLConnection) url.openConnection();
if (state.user != null)
{
@@ -123,21 +124,16 @@
if (connection.getResponseCode() == 304)
{
- connection.disconnect();
- //ByteArrayOutputStream baos = new ByteArrayOutputStream();
- //IOUtility.transferStream(connection.getInputStream(), baos);
throw new NotModifiedFetchException("Not Modified");
}
if (connection.getResponseCode() == 401)
{
- connection.disconnect();
throw new NotAuthorizedFetchException(connection.getResponseMessage());
}
if (connection.getResponseCode() == 407)
{
- connection.disconnect();
throw new ProxyNotAuthorizedFetchException(connection.getResponseMessage());
}
@@ -145,7 +141,6 @@
IOUtility.transferStream(connection.getInputStream(), os);
os.flush();
os.close();
- connection.disconnect();
}
catch (FetchException e)
{
@@ -155,6 +150,13 @@
{
throw new FetchException(e.getLocalizedMessage(), e);
}
+ finally
+ {
+ if (connection != null)
+ {
+ connection.disconnect();
+ }
+ }
}
private String getAuthorization(State state)
@@ -216,7 +218,9 @@
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", request.getProxyHost());
System.getProperties().put("proxyPort", "" + request.getProxyPort());
- } else {
+ }
+ else
+ {
System.getProperties().put("proxySet", "false");
}
1.4 +2 -2 maven-new/fetch/src/test/java/org/apache/maven/fetch/FetchTest.java
Index: FetchTest.java
===================================================================
RCS file: /home/cvs/maven-new/fetch/src/test/java/org/apache/maven/fetch/FetchTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FetchTest.java 25 May 2003 14:33:21 -0000 1.3
+++ FetchTest.java 25 May 2003 14:54:54 -0000 1.4
@@ -73,7 +73,7 @@
dreq.setOnlyIfModifiedSinceDate(sdf.parse("20000101"));
try
{
- FetchResponse dresp = bean.performDownload(dreq);
+ bean.performDownload(dreq);
fail("The resource was not modified and should have thrown NotModified exception");
}
catch (NotModifiedFetchException e)
1.3 +21 -1 maven-new/fetch/src/main/java/org/apache/maven/fetch/util/IOUtility.java
Index: IOUtility.java
===================================================================
RCS file: /home/cvs/maven-new/fetch/src/main/java/org/apache/maven/fetch/util/IOUtility.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- IOUtility.java 25 May 2003 12:07:49 -0000 1.2
+++ IOUtility.java 25 May 2003 14:54:54 -0000 1.3
@@ -107,4 +107,24 @@
}
}
+ /**
+ * Closes an OutputStream without throwing an IOException.
+ * The IOException is swallowed.
+ * @param in the OutputStream to close. If null, nothing is closed.
+ */
+ public static final void close(OutputStream out)
+ {
+ try
+ {
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ catch (IOException ioex)
+ {
+ //Do nothing
+ }
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org