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 /** 28 * Encoding utility. 29 * 30 * @since 2.7 31 */ 32 public class EncodingUtil 33 { 34 /** 35 * Parses and returns an encoded version of the given URL string. 36 * 37 * @param url Raw/decoded string form of a URL to parse/encode. 38 * @return Parsed/encoded {@link URI} that represents the string form URL passed in. 39 * @throws MalformedURLException 40 * @throws URISyntaxException 41 */ 42 public static URI encodeURL( String url ) 43 throws MalformedURLException, URISyntaxException 44 { 45 URL urlObject = new URL( url ); 46 47 URI uriEncoded = 48 new URI( urlObject.getProtocol(), // 49 urlObject.getAuthority(), // 50 urlObject.getPath(), // 51 urlObject.getQuery(), // 52 urlObject.getRef() ); 53 54 return uriEncoded; 55 } 56 57 /** 58 * Parses and returns an encoded version of the given URL string. 59 * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid. 60 * 61 * @param url Raw/decoded string form of a URL to parse/encode. 62 * @return Parsed/encoded URI (as string) that represents the 63 * @throws IllegalArgumentException in case the URL string is invalid. 64 */ 65 public static String encodeURLToString( String url ) 66 { 67 try 68 { 69 return encodeURL( url ).toString(); 70 } 71 catch ( Exception e ) 72 { 73 throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e ); 74 } 75 } 76 77 /** 78 * Parses and returns an encoded version of the given URL string alongside the given paths. 79 * 80 * @param baseUrl Base URL to use when constructing the final URL, ie: scheme://authority/initial.path. 81 * @param paths Additional path(s) to append at the end of the base path. 82 * @return Composed URL (base + paths) already encoded, separating the individual path components by "/". 83 * @since TODO 84 */ 85 public static String encodeURLToString( String baseUrl, String... paths ) 86 { 87 StringBuilder url = new StringBuilder( baseUrl ); 88 89 String[] parts = paths == null ? // 90 new String[0] : // 91 paths.length == 1 ? paths[0].split( "/" ) : paths; 92 93 for ( String part : parts ) 94 { 95 if ( !url.toString().endsWith( "/" ) ) 96 { 97 url.append( '/' ); 98 } 99 100 url.append( part ); 101 } 102 103 return encodeURLToString( url.toString() ); 104 } 105 }