1 package org.apache.maven.wagon.shared.http; 2 3 import org.apache.commons.lang.StringUtils; 4 5 import java.net.MalformedURLException; 6 import java.net.URI; 7 import java.net.URISyntaxException; 8 import java.net.URL; 9 10 /* 11 * Licensed to the Apache Software Foundation (ASF) under one 12 * or more contributor license agreements. See the NOTICE file 13 * distributed with this work for additional information 14 * regarding copyright ownership. The ASF licenses this file 15 * to you under the Apache License, Version 2.0 (the 16 * "License"); you may not use this file except in compliance 17 * with the License. You may obtain a copy of the License at 18 * 19 * http://www.apache.org/licenses/LICENSE-2.0 20 * 21 * Unless required by applicable law or agreed to in writing, 22 * software distributed under the License is distributed on an 23 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 24 * KIND, either express or implied. See the License for the 25 * specific language governing permissions and limitations 26 * under the License. 27 */ 28 29 /** 30 * Encoding utility. 31 * 32 * @since 2.7 33 */ 34 public class EncodingUtil 35 { 36 /** 37 * Parses and returns an encoded version of the given URL string. 38 * 39 * @param url Raw/decoded string form of a URL to parse/encode. 40 * @return Parsed/encoded {@link URI} that represents the string form URL passed in. 41 * @throws MalformedURLException 42 * @throws URISyntaxException 43 */ 44 public static URI encodeURL( String url ) 45 throws MalformedURLException, URISyntaxException 46 { 47 URL urlObject = new URL( url ); 48 49 URI uriEncoded = 50 new URI( urlObject.getProtocol(), // 51 urlObject.getAuthority(), // 52 urlObject.getPath(), // 53 urlObject.getQuery(), // 54 urlObject.getRef() ); 55 56 return uriEncoded; 57 } 58 59 /** 60 * Parses and returns an encoded version of the given URL string. 61 * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid. 62 * 63 * @param url Raw/decoded string form of a URL to parse/encode. 64 * @return Parsed/encoded URI (as string) that represents the 65 * @throws IllegalArgumentException in case the URL string is invalid. 66 */ 67 public static String encodeURLToString( String url ) 68 { 69 try 70 { 71 return encodeURL( url ).toString(); 72 } 73 catch ( Exception e ) 74 { 75 throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e ); 76 } 77 } 78 79 /** 80 * Parses and returns an encoded version of the given URL string alongside the given paths. 81 * 82 * @param baseUrl Base URL to use when constructing the final URL, ie: scheme://authority/initial.path. 83 * @param paths Additional path(s) to append at the end of the base path. 84 * @return Composed URL (base + paths) already encoded, separating the individual path components by "/". 85 * @since TODO 86 */ 87 public static String encodeURLToString( String baseUrl, String... paths ) 88 { 89 StringBuilder url = new StringBuilder( baseUrl ); 90 91 String[] parts = paths == null ? // 92 new String[0] : // 93 paths.length == 1 ? StringUtils.split( paths[0], "/" ) : paths; 94 95 for ( String part : parts ) 96 { 97 if ( !url.toString().endsWith( "/" ) ) 98 { 99 url.append( '/' ); 100 } 101 102 url.append( part ); 103 } 104 105 return encodeURLToString( url.toString() ); 106 } 107 }