001package org.apache.maven.wagon.shared.http; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.net.MalformedURLException; 023import java.net.URI; 024import java.net.URISyntaxException; 025import java.net.URL; 026 027/** 028 * Encoding utility. 029 * 030 * @since 2.7 031 */ 032public class EncodingUtil 033{ 034 /** 035 * Parses and returns an encoded version of the given URL string. 036 * 037 * @param url Raw/decoded string form of a URL to parse/encode. 038 * @return Parsed/encoded {@link URI} that represents the string form URL passed in. 039 * @throws MalformedURLException 040 * @throws URISyntaxException 041 */ 042 public static URI encodeURL( String url ) 043 throws MalformedURLException, URISyntaxException 044 { 045 URL urlObject = new URL( url ); 046 047 URI uriEncoded = 048 new URI( urlObject.getProtocol(), // 049 urlObject.getAuthority(), // 050 urlObject.getPath(), // 051 urlObject.getQuery(), // 052 urlObject.getRef() ); 053 054 return uriEncoded; 055 } 056 057 /** 058 * Parses and returns an encoded version of the given URL string. 059 * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid. 060 * 061 * @param url Raw/decoded string form of a URL to parse/encode. 062 * @return Parsed/encoded URI (as string) that represents the 063 * @throws IllegalArgumentException in case the URL string is invalid. 064 */ 065 public static String encodeURLToString( String url ) 066 { 067 try 068 { 069 return encodeURL( url ).toString(); 070 } 071 catch ( Exception e ) 072 { 073 throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e ); 074 } 075 } 076 077 /** 078 * Parses and returns an encoded version of the given URL string alongside the given paths. 079 * 080 * @param baseUrl Base URL to use when constructing the final URL, ie: scheme://authority/initial.path. 081 * @param paths Additional path(s) to append at the end of the base path. 082 * @return Composed URL (base + paths) already encoded, separating the individual path components by "/". 083 * @since TODO 084 */ 085 public static String encodeURLToString( String baseUrl, String... paths ) 086 { 087 StringBuilder url = new StringBuilder( baseUrl ); 088 089 String[] parts = paths == null ? // 090 new String[0] : // 091 paths.length == 1 ? paths[0].split( "/" ) : paths; 092 093 for ( String part : parts ) 094 { 095 if ( !url.toString().endsWith( "/" ) ) 096 { 097 url.append( '/' ); 098 } 099 100 url.append( part ); 101 } 102 103 return encodeURLToString( url.toString() ); 104 } 105}