ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maart...@apache.org
Subject svn commit: r683715 - in /ant/ivy/core/trunk: ./ src/java/org/apache/ivy/plugins/repository/url/ src/java/org/apache/ivy/util/ src/java/org/apache/ivy/util/url/
Date Thu, 07 Aug 2008 21:08:35 GMT
Author: maartenc
Date: Thu Aug  7 14:08:34 2008
New Revision: 683715

URL: http://svn.apache.org/viewvc?rev=683715&view=rev
Log:
NEW: Add publish support to URL resolver (IVY-848) (thanks to Brian Sanders)

Modified:
    ant/ivy/core/trunk/CHANGES.txt
    ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/FileUtil.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/AbstractURLHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandler.java
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandlerDispatcher.java

Modified: ant/ivy/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/CHANGES.txt (original)
+++ ant/ivy/core/trunk/CHANGES.txt Thu Aug  7 14:08:34 2008
@@ -61,6 +61,7 @@
 	Geoff Reedy
 	Christian Riege
 	Andreas Sahlbach
+	Brian Sanders
 	Adrian Sandor
 	Ruslan Shevchenko
 	John Shields
@@ -75,6 +76,7 @@
 
    trunk version
 =====================================
+- NEW: Add publish support to URL resolver (IVY-848) (thanks to Brian Sanders)
 - NEW: Better support for local builds (IVY-857)
 - NEW: Retain original dependency constraint rules in resolved ivy file (IVY-739)
 - NEW: Add a new resolve mode (optionally per module) to utilize dynamic constraint rule
metadata (IVY-740)

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/repository/url/URLRepository.java Thu
Aug  7 14:08:34 2008
@@ -20,13 +20,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Map;
-
+import java.util.*;
 import org.apache.ivy.plugins.repository.AbstractRepository;
 import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
 import org.apache.ivy.plugins.repository.Resource;
@@ -75,8 +69,27 @@
     }
 
     public void put(File source, String destination, boolean overwrite) throws IOException
{
-        throw new UnsupportedOperationException(
-                "URL repository is not able to put files for the moment");
+        if (!overwrite) {
+            throw new UnsupportedOperationException(
+                    "URL repository do not support append operations at the moment");
+        }
+
+        fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT);
+        try {
+            long totalLength = source.length();
+            if (totalLength > 0) {
+                progress.setTotalLength(new Long(totalLength));
+            }
+            FileUtil.copy(source, new URL(destination), progress);
+        } catch (IOException ex) {
+            fireTransferError(ex);
+            throw ex;
+        } catch (RuntimeException ex) {
+            fireTransferError(ex);
+            throw ex;
+        } finally {
+            progress.setTotalLength(null);
+        }
     }
 
     private ApacheURLLister lister = new ApacheURLLister();

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/FileUtil.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/FileUtil.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/FileUtil.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/FileUtil.java Thu Aug  7 14:08:34 2008
@@ -136,6 +136,10 @@
         URLHandlerRegistry.getDefault().download(src, dest, l);
     }
 
+    public static void copy(File src, URL dest, CopyProgressListener l) throws IOException
{
+        URLHandlerRegistry.getDefault().upload(src, dest, l);
+    }
+
     public static void copy(InputStream src, File dest, CopyProgressListener l) throws IOException
{
         if (dest.getParentFile() != null) {
             dest.getParentFile().mkdirs();
@@ -172,6 +176,12 @@
                 evt.update(EMPTY_BUFFER, 0, total);
             }
 
+            try {
+                dest.flush();
+            } catch (IOException ex) {
+                // ignore
+            }
+
             // close the streams
             src.close();
             dest.close();

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/AbstractURLHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/AbstractURLHandler.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/AbstractURLHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/AbstractURLHandler.java Thu Aug  7
14:08:34 2008
@@ -18,6 +18,8 @@
 package org.apache.ivy.util.url;
 
 import java.net.URL;
+import java.net.HttpURLConnection;
+import java.io.IOException;
 
 public abstract class AbstractURLHandler implements URLHandler {
     public boolean isReachable(URL url) {
@@ -43,4 +45,23 @@
     public long getLastModified(URL url, int timeout) {
         return getURLInfo(url, timeout).getLastModified();
     }
+
+    protected void validatePutStatusCode(URL dest, int statusCode, String statusMessage)
throws IOException {
+        switch (statusCode) {
+            case HttpURLConnection.HTTP_OK:
+                /* intentional fallthrough */
+            case HttpURLConnection.HTTP_CREATED:
+                /* intentional fallthrough */
+            case HttpURLConnection.HTTP_ACCEPTED:
+                /* intentional fallthrough */
+            case HttpURLConnection.HTTP_NO_CONTENT:
+                break;
+            case HttpURLConnection.HTTP_UNAUTHORIZED:
+                /* intentional fallthrough */
+            case HttpURLConnection.HTTP_FORBIDDEN:
+                throw new IOException("Access to URL " + dest + " was refused by the server"
+ (statusMessage == null ? "" : ": " + statusMessage));
+            default:
+                throw new IOException("PUT operation to URL " + dest + " failed with status
code " + statusCode + (statusMessage == null ? "" : ": " + statusMessage));
+        }
+    }
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java Thu Aug  7 14:08:34
2008
@@ -17,11 +17,7 @@
  */
 package org.apache.ivy.util.url;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -143,6 +139,39 @@
         }
     }
 
+    public void upload(File source, URL dest, CopyProgressListener l) throws IOException
{
+        if (!"http".equals(dest.getProtocol()) && !"https".equals(dest.getProtocol()))
{
+            throw new UnsupportedOperationException(
+                    "URL repository only support HTTP PUT at the moment");
+        }
+
+        HttpURLConnection conn = null;
+        try {
+            conn = (HttpURLConnection) dest.openConnection();
+            conn.setDoOutput(true);
+            conn.setRequestMethod("PUT");
+            conn.setRequestProperty("User-Agent", "Apache Ivy");
+            conn.setRequestProperty("Content-type", "application/octet-stream");
+            conn.setRequestProperty("Content-length", Long.toString(source.length()));
+            conn.setInstanceFollowRedirects(true);
+
+            InputStream in = new FileInputStream(source);
+            try {
+                OutputStream os = conn.getOutputStream();
+                FileUtil.copy(in, os, l);
+            } finally {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    /* ignored */
+                }
+            }
+            validatePutStatusCode(dest, conn.getResponseCode(), conn.getResponseMessage());
+        } finally {
+            disconnect(conn);
+        }
+    }
+
     private void disconnect(URLConnection con) {
         if (con instanceof HttpURLConnection) {
             ((HttpURLConnection) con).disconnect();

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/HttpClientHandler.java Thu Aug  7
14:08:34 2008
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.FileInputStream;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.text.ParseException;
@@ -38,6 +39,8 @@
 import org.apache.commons.httpclient.auth.AuthPolicy;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.HeadMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 import org.apache.ivy.util.CopyProgressListener;
 import org.apache.ivy.util.Credentials;
 import org.apache.ivy.util.FileUtil;
@@ -99,6 +102,28 @@
         get.releaseConnection();
     }
 
+    public void upload(File src, URL dest, CopyProgressListener l) throws IOException {
+        HttpClient client = getClient(dest);
+
+        PutMethod put = new PutMethod(dest.toExternalForm());
+        put.setDoAuthentication(useAuthentication(dest) || useProxyAuthentication());
+        FileInputStream fileStream = null;
+        try {
+            fileStream = new FileInputStream(src);
+            put.setRequestEntity(new InputStreamRequestEntity(fileStream));
+            int statusCode = client.executeMethod(put);
+            validatePutStatusCode(dest, statusCode, null);
+        } finally {
+            if (fileStream != null) {
+                try {
+                    fileStream.close();
+                } catch (IOException e) {
+                    /* ignored */
+                }
+            }
+        }
+    }
+
     public URLInfo getURLInfo(URL url) {
         return getURLInfo(url, 0);
     }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandler.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandler.java Thu Aug  7 14:08:34
2008
@@ -139,4 +139,6 @@
     public InputStream openStream(URL url) throws IOException;
 
     public void download(URL src, File dest, CopyProgressListener l) throws IOException;
+
+    public void upload(File src, URL dest, CopyProgressListener l) throws IOException;
 }

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandlerDispatcher.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandlerDispatcher.java?rev=683715&r1=683714&r2=683715&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandlerDispatcher.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/URLHandlerDispatcher.java Thu Aug
 7 14:08:34 2008
@@ -77,6 +77,10 @@
         getHandler(src.getProtocol()).download(src, dest, l);
     }
 
+    public void upload(File src, URL dest, CopyProgressListener l) throws IOException {
+        getHandler(dest.getProtocol()).upload(src, dest, l);
+    }
+
     public void setDownloader(String protocol, URLHandler downloader) {
         handlers.put(protocol, downloader);
     }



Mime
View raw message