1 package org.apache.maven.wagon.shared.http; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.net.MalformedURLException; 23 import java.net.URI; 24 import java.net.URISyntaxException; 25 import java.net.URL; 26 27 import org.apache.http.client.utils.URLEncodedUtils; 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 * @deprecated to be removed with 4.0.0 44 */ 45 @Deprecated 46 public static URI encodeURL( String url ) 47 throws MalformedURLException, URISyntaxException 48 { 49 URL urlObject = new URL( url ); 50 51 URI uriEncoded = 52 new URI( urlObject.getProtocol(), // 53 urlObject.getAuthority(), // 54 urlObject.getPath(), // 55 urlObject.getQuery(), // 56 urlObject.getRef() ); 57 58 return uriEncoded; 59 } 60 61 /** 62 * Parses and returns an encoded version of the given URL string. 63 * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid. 64 * 65 * @param url Raw/decoded string form of a URL to parse/encode. 66 * @return Parsed/encoded URI (as string) that represents the 67 * @throws IllegalArgumentException in case the URL string is invalid. 68 * @deprecated To be remvoed with 4.0.0 69 */ 70 @Deprecated 71 public static String encodeURLToString( String url ) 72 { 73 try 74 { 75 return encodeURL( url ).toString(); 76 } 77 catch ( Exception e ) 78 { 79 throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e ); 80 } 81 } 82 83 /** 84 * Parses and returns an encoded version of the given URL string alongside the given path. 85 * 86 * @param baseUrl Base URL to use when constructing the final URL. This has to be a valid URL already. 87 * @param path Additional unencoded path to append at the end of the base path. 88 * @return Composed URL (base + path) already encoded, separating the individual path segments by "/". 89 * @since TODO 90 */ 91 public static String encodeURLToString( String baseUrl, String path ) 92 { 93 String[] pathSegments = path == null ? new String[0] : path.split( "/" ); 94 95 String encodedUrl = encodeURLToString( baseUrl, pathSegments ); 96 if ( path != null && path.endsWith( "/" ) ) 97 { 98 return encodedUrl + "/"; 99 } 100 101 return encodedUrl; 102 } 103 104 /** 105 * Parses and returns an encoded version of the given URL string alongside the given path segments. 106 * 107 * @param baseUrl Base URL to use when constructing the final URL. This has to be a valid URL already. 108 * @param pathSegments Additional unencoded path segments to append at the end of the base path. 109 * @return Composed URL (base + path) already encoded, separating the individual path segments by "/". 110 * @since TODO 111 */ 112 public static String encodeURLToString( String baseUrl, String... pathSegments ) 113 { 114 StringBuilder url = new StringBuilder( baseUrl ); 115 116 String[] segments = pathSegments == null ? new String[0] : pathSegments; 117 118 String path = URLEncodedUtils.formatSegments( segments ); 119 120 if ( url.toString().endsWith( "/" ) && !path.isEmpty() ) 121 { 122 url.deleteCharAt( url.length() - 1 ); 123 } 124 125 url.append( path ); 126 127 return url.toString(); 128 } 129 }