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 org.apache.commons.lang.StringUtils;
023
024import java.net.MalformedURLException;
025import java.net.URI;
026import java.net.URISyntaxException;
027import java.net.URL;
028
029/**
030 * Encoding utility.
031 *
032 * @since 2.7
033 */
034public class EncodingUtil
035{
036    /**
037     * Parses and returns an encoded version of the given URL string.
038     *
039     * @param url Raw/decoded string form of a URL to parse/encode.
040     * @return Parsed/encoded {@link URI} that represents the string form URL passed in.
041     * @throws MalformedURLException
042     * @throws URISyntaxException
043     */
044    public static URI encodeURL( String url )
045        throws MalformedURLException, URISyntaxException
046    {
047        URL urlObject = new URL( url );
048
049        URI uriEncoded =
050            new URI( urlObject.getProtocol(), //
051                     urlObject.getAuthority(), //
052                     urlObject.getPath(), //
053                     urlObject.getQuery(), //
054                     urlObject.getRef() );
055
056        return uriEncoded;
057    }
058
059    /**
060     * Parses and returns an encoded version of the given URL string.
061     * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid.
062     *
063     * @param url Raw/decoded string form of a URL to parse/encode.
064     * @return Parsed/encoded URI (as string) that represents the
065     * @throws IllegalArgumentException in case the URL string is invalid.
066     */
067    public static String encodeURLToString( String url )
068    {
069        try
070        {
071            return encodeURL( url ).toString();
072        }
073        catch ( Exception e )
074        {
075            throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e );
076        }
077    }
078
079    /**
080     * Parses and returns an encoded version of the given URL string alongside the given paths.
081     *
082     * @param baseUrl Base URL to use when constructing the final URL, ie: scheme://authority/initial.path.
083     * @param paths   Additional path(s) to append at the end of the base path.
084     * @return Composed URL (base + paths) already encoded, separating the individual path components by "/".
085     * @since TODO
086     */
087    public static String encodeURLToString( String baseUrl, String... paths )
088    {
089        StringBuilder url = new StringBuilder( baseUrl );
090
091        String[] parts = paths == null ? //
092            new String[0] : //
093            paths.length == 1 ? StringUtils.split( paths[0], "/" ) : paths;
094
095        for ( String part : parts )
096        {
097            if ( !url.toString().endsWith( "/" ) )
098            {
099                url.append( '/' );
100            }
101
102            url.append( part );
103        }
104
105        return encodeURLToString( url.toString() );
106    }
107}