maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [02/12] git commit: Refactoring encoding logic and shared encoded URL building from other wagons
Date Fri, 05 Sep 2014 01:29:36 GMT
Refactoring encoding logic and shared encoded URL building from other wagons


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

Branch: refs/heads/master
Commit: 4a12711428ec989c04b5dba82589637f2558da0b
Parents: 33aceda
Author: Roberto Andrade <roberto@cloud.com>
Authored: Tue Jul 22 19:49:33 2014 -0400
Committer: Olivier Lamy <olamy@apache.org>
Committed: Fri Sep 5 10:48:20 2014 +1000

----------------------------------------------------------------------
 .../maven/wagon/shared/http/EncodingUtil.java   | 108 +++++++++++++++++++
 .../wagon/shared/http/EncodingUtilTest.java     |  49 +++++++++
 2 files changed, 157 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/4a127114/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java
b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java
new file mode 100644
index 0000000..951df99
--- /dev/null
+++ b/wagon-providers/wagon-http-shared/src/main/java/org/apache/maven/wagon/shared/http/EncodingUtil.java
@@ -0,0 +1,108 @@
+package org.apache.maven.wagon.shared.http;
+
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.apache.commons.lang.StringUtils;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Encoding utility.
+ */
+public class EncodingUtil
+{	
+	/**
+	 * Parses and returns an encoded version of the given URL string.
+	 * 
+	 * @param url 	Raw/decoded string form of a URL to parse/encode.
+	 * @return		Parsed/encoded {@link URI} that represents the string form URL passed in.
+	 * @throws MalformedURLException
+	 * @throws URISyntaxException
+	 */
+	public static URI encodeURL( String url ) throws MalformedURLException, URISyntaxException
 
+	{
+	    URL urlObject = new URL(url);
+	    
+	    URI uriEncoded = new URI( 
+	    	urlObject.getProtocol(), 
+	    	urlObject.getAuthority(), 
+	    	urlObject.getPath(), 
+	    	urlObject.getQuery(), 
+	    	urlObject.getRef()
+	    );
+
+	    return uriEncoded;
+	}
+	
+	/**
+	 * Parses and returns an encoded version of the given URL string. 
+	 * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed
URL is invalid.
+	 * 
+	 * @param url 	Raw/decoded string form of a URL to parse/encode.
+	 * @return		Parsed/encoded URI (as string) that represents the 
+	 * @throws IllegalArgumentException	in case the URL string is invalid.
+	 */
+	public static String encodeURLToString( String url )
+	{
+		try
+		{
+			return encodeURL( url ).toString();
+		}
+		catch ( Exception e )
+		{
+			throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e );
+		}
+	}
+	
+	/**
+	 * Parses and returns an encoded version of the given URL string alongside the given paths.
+	 * 
+	 * @param baseUrl	Base URL to use when constructing the final URL, ie: scheme://authority/initial.path.
+	 * @param paths		Additional path(s) to append at the end of the base path.
+	 * @return			Composed URL (base + paths) already encoded, separating the individual path
components by "/".
+	 * @since TODO
+	 */
+	public static String encodeURLToString( String baseUrl, String... paths )
+	{
+        StringBuilder url = new StringBuilder( baseUrl );
+        
+        String[] parts = 
+        	paths == null ? 
+        		new String[0] : 
+        	paths.length == 1 ? 
+        		StringUtils.split( paths[0], "/" ) : 
+        		paths;
+        		
+        for ( String part : parts )
+        {
+            if ( !url.toString().endsWith( "/" ) )
+            {
+                url.append( '/' );
+            }
+            
+            url.append( part );
+        }
+        
+        return encodeURLToString( url.toString() );
+	}
+}

http://git-wip-us.apache.org/repos/asf/maven-wagon/blob/4a127114/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java
----------------------------------------------------------------------
diff --git a/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java
b/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java
new file mode 100644
index 0000000..e14133e
--- /dev/null
+++ b/wagon-providers/wagon-http-shared/src/test/java/org/apache/maven/wagon/shared/http/EncodingUtilTest.java
@@ -0,0 +1,49 @@
+package org.apache.maven.wagon.shared.http;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+
+import junit.framework.TestCase;
+
+public class EncodingUtilTest extends TestCase
+{	
+	public void testEncodeURLWithSpaces() throws URISyntaxException, MalformedURLException
+	{
+		String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/path with spaces" );
+		
+		assertEquals( "file://host:1/path%20with%20spaces", encodedURL );
+	}
+	
+	public void testEncodeURLWithSpacesInPath() throws URISyntaxException, MalformedURLException
+	{
+		String encodedURL = EncodingUtil.encodeURLToString( "file://host:1", "path with spaces"
);
+		
+		assertEquals( "file://host:1/path%20with%20spaces", encodedURL );
+	}
+	
+	public void testEncodeURLWithSpacesInBothBaseAndPath() throws URISyntaxException, MalformedURLException
+	{
+		String encodedURL = EncodingUtil.encodeURLToString( "file://host:1/with a", "path with
spaces" );
+		
+		assertEquals( "file://host:1/with%20a/path%20with%20spaces", encodedURL );
+	}
+}


Mime
View raw message