Refactoring ``wagon-http`` and ``wagon-http-lightweight`` to reuse shared 
``EncodingUtil``


Project: http://git-wip-us.apache.org/repos/asf/maven-wagon/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-wagon/commit/9fc6238c
Tree: http://git-wip-us.apache.org/repos/asf/maven-wagon/tree/9fc6238c
Diff: http://git-wip-us.apache.org/repos/asf/maven-wagon/diff/9fc6238c

Branch: refs/heads/master
Commit: 9fc6238c6cffbd9c203c4f578c1ca0a7c96a5671
Parents: 34e6372
Author: Roberto Andrade <[email protected]>
Authored: Tue Jul 22 19:52:19 2014 -0400
Committer: Roberto Andrade <[email protected]>
Committed: Tue Jul 22 19:52:19 2014 -0400

----------------------------------------------------------------------
 .../providers/http/LightweightHttpWagon.java    | 67 +++++++++-----------
 .../providers/http/AbstractHttpClientWagon.java | 65 ++++++++++---------
 2 files changed, 62 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/9fc6238c/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
----------------------------------------------------------------------
diff --git 
a/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
 
b/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
index f6c20f9..76c46a4 100644
--- 
a/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
+++ 
b/wagon-providers/wagon-http-lightweight/src/main/java/org/apache/maven/wagon/providers/http/LightweightHttpWagon.java
@@ -19,21 +19,6 @@ package org.apache.maven.wagon.providers.http;
  * under the License.
  */
 
-import org.apache.commons.io.IOUtils;
-import org.apache.maven.wagon.ConnectionException;
-import org.apache.maven.wagon.InputData;
-import org.apache.maven.wagon.OutputData;
-import org.apache.maven.wagon.ResourceDoesNotExistException;
-import org.apache.maven.wagon.StreamWagon;
-import org.apache.maven.wagon.TransferFailedException;
-import org.apache.maven.wagon.authentication.AuthenticationException;
-import org.apache.maven.wagon.authorization.AuthorizationException;
-import org.apache.maven.wagon.events.TransferEvent;
-import org.apache.maven.wagon.proxy.ProxyInfo;
-import org.apache.maven.wagon.resource.Resource;
-import org.apache.maven.wagon.shared.http.HtmlFileListParser;
-import org.codehaus.plexus.util.Base64;
-
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
@@ -51,6 +36,22 @@ import java.util.List;
 import java.util.Properties;
 import java.util.zip.GZIPInputStream;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.wagon.ConnectionException;
+import org.apache.maven.wagon.InputData;
+import org.apache.maven.wagon.OutputData;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.StreamWagon;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.authentication.AuthenticationException;
+import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.apache.maven.wagon.events.TransferEvent;
+import org.apache.maven.wagon.proxy.ProxyInfo;
+import org.apache.maven.wagon.resource.Resource;
+import org.apache.maven.wagon.shared.http.EncodingUtil;
+import org.apache.maven.wagon.shared.http.HtmlFileListParser;
+import org.codehaus.plexus.util.Base64;
+
 /**
  * LightweightHttpWagon, using JDK's HttpURLConnection.
  *
@@ -88,23 +89,14 @@ public class LightweightHttpWagon
     private volatile LightweightHttpWagonAuthenticator authenticator;
 
     /**
-     * Builds a complete URL string from the repository URL and the relative 
path passed.
+     * Builds a complete URL string from the repository URL and the relative 
path of the resource passed.
      *
-     * @param path the relative path
+     * @param resource the resource to extract the relative path from.
      * @return the complete URL
      */
-    private String buildUrl( String path )
+    private String buildUrl( Resource resource )
     {
-        final String repoUrl = getRepository().getUrl();
-
-        path = path.replace( ' ', '+' );
-
-        if ( repoUrl.charAt( repoUrl.length() - 1 ) != '/' )
-        {
-            return repoUrl + '/' + path;
-        }
-
-        return repoUrl + path;
+       return EncodingUtil.encodeURLToString( getRepository().getUrl(), 
resource.getName() );
     }
 
     public void fillInputData( InputData inputData )
@@ -112,7 +104,7 @@ public class LightweightHttpWagon
     {
         Resource resource = inputData.getResource();
 
-        String visitingUrl = buildUrl( resource.getName() );
+        String visitingUrl = buildUrl( resource );
         try
         {
             List<String> visitedUrls = new ArrayList<String>();
@@ -141,7 +133,7 @@ public class LightweightHttpWagon
                 if ( responseCode == HttpURLConnection.HTTP_FORBIDDEN
                     || responseCode == HttpURLConnection.HTTP_UNAUTHORIZED )
                 {
-                    throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource.getName() ) );
+                    throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource ) );
                 }
                 if ( responseCode == HttpURLConnection.HTTP_MOVED_PERM
                     || responseCode == HttpURLConnection.HTTP_MOVED_TEMP )
@@ -212,7 +204,7 @@ public class LightweightHttpWagon
         Resource resource = outputData.getResource();
         try
         {
-            URL url = new URL( buildUrl( resource.getName() ) );
+            URL url = new URL( buildUrl( resource ) );
             putConnection = (HttpURLConnection) url.openConnection( this.proxy 
);
 
             addHeaders( putConnection );
@@ -244,16 +236,16 @@ public class LightweightHttpWagon
                     break;
 
                 case HttpURLConnection.HTTP_FORBIDDEN:
-                    throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource.getName() ) );
+                    throw new AuthorizationException( "Access denied to: " + 
buildUrl( resource ) );
 
                 case HttpURLConnection.HTTP_NOT_FOUND:
                     throw new ResourceDoesNotExistException(
-                        "File: " + buildUrl( resource.getName() ) + " does not 
exist" );
+                        "File: " + buildUrl( resource ) + " does not exist" );
 
                     // add more entries here
                 default:
                     throw new TransferFailedException(
-                        "Failed to transfer file: " + buildUrl( 
resource.getName() ) + ". Return code is: "
+                        "Failed to transfer file: " + buildUrl( resource ) + 
". Return code is: "
                             + statusCode );
             }
         }
@@ -355,7 +347,7 @@ public class LightweightHttpWagon
             destinationDirectory += "/";
         }
 
-        String url = buildUrl( destinationDirectory );
+        String url = buildUrl( new Resource( destinationDirectory ) );
 
         Resource resource = new Resource( destinationDirectory );
 
@@ -389,7 +381,8 @@ public class LightweightHttpWagon
 
         try
         {
-            URL url = new URL( buildUrl( new Resource( resourceName 
).getName() ) );
+            Resource resource = new Resource( resourceName );
+                       URL url = new URL( buildUrl( resource ) );
             headConnection = (HttpURLConnection) url.openConnection( 
this.proxy );
 
             addHeaders( headConnection );
@@ -415,7 +408,7 @@ public class LightweightHttpWagon
 
                 default:
                     throw new TransferFailedException(
-                        "Failed to look for file: " + buildUrl( resourceName ) 
+ ". Return code is: " + statusCode );
+                        "Failed to look for file: " + buildUrl( resource ) + 
". Return code is: " + statusCode );
             }
         }
         catch ( IOException e )

http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/9fc6238c/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
----------------------------------------------------------------------
diff --git 
a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
 
b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
index 7c6dcd2..611c5e7 100755
--- 
a/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
+++ 
b/wagon-providers/wagon-http/src/main/java/org/apache/maven/wagon/providers/http/AbstractHttpClientWagon.java
@@ -19,6 +19,25 @@ package org.apache.maven.wagon.providers.http;
  * under the License.
  */
 
+import java.io.ByteArrayInputStream;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TimeZone;
+import java.util.concurrent.TimeUnit;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
@@ -71,28 +90,10 @@ import org.apache.maven.wagon.events.TransferEvent;
 import org.apache.maven.wagon.proxy.ProxyInfo;
 import org.apache.maven.wagon.repository.Repository;
 import org.apache.maven.wagon.resource.Resource;
+import org.apache.maven.wagon.shared.http.EncodingUtil;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 
-import javax.net.ssl.HttpsURLConnection;
-import javax.net.ssl.SSLContext;
-import java.io.ByteArrayInputStream;
-import java.io.Closeable;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URLEncoder;
-import java.nio.ByteBuffer;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Properties;
-import java.util.TimeZone;
-import java.util.concurrent.TimeUnit;
-
 /**
  * @author <a href="[email protected]">Michal Maczka</a>
  * @author <a href="mailto:[email protected]";>James William Dumay</a>
@@ -477,20 +478,18 @@ public abstract class AbstractHttpClientWagon
     private void put( Resource resource, File source, HttpEntity httpEntity )
         throws TransferFailedException, AuthorizationException, 
ResourceDoesNotExistException
     {
-
-        StringBuilder url = new StringBuilder( getURL( getRepository() ) );
-        String[] parts = StringUtils.split( resource.getName(), "/" );
-        for ( String part : parts )
-        {
-            // TODO: Fix encoding...
-            // url += "/" + URLEncoder.encode( parts[i], 
System.getProperty("file.encoding") );
-            if ( !url.toString().endsWith( "/" ) )
-            {
-                url.append( '/' );
-            }
-            url.append( URLEncoder.encode( part ) );
-        }
-        put( resource, source, httpEntity, url.toString() );
+        put( resource, source, httpEntity, buildUrl( resource ) );
+    }
+    
+    /**
+     * Builds a complete URL string from the repository URL and the relative 
path of the resource passed.
+     *
+     * @param resource the resource to extract the relative path from.
+     * @return the complete URL
+     */
+    private String buildUrl( Resource resource )
+    {
+       return EncodingUtil.encodeURLToString( getRepository().getUrl(), 
resource.getName() );
     }
 
     private void put( Resource resource, File source, HttpEntity httpEntity, 
String url )

Reply via email to