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
027import org.apache.http.client.utils.URLEncodedUtils;
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     * @deprecated to be removed with 4.0.0
044     */
045    @Deprecated
046    public static URI encodeURL( String url )
047        throws MalformedURLException, URISyntaxException
048    {
049        URL urlObject = new URL( url );
050
051        URI uriEncoded =
052            new URI( urlObject.getProtocol(), //
053                     urlObject.getAuthority(), //
054                     urlObject.getPath(), //
055                     urlObject.getQuery(), //
056                     urlObject.getRef() );
057
058        return uriEncoded;
059    }
060
061    /**
062     * Parses and returns an encoded version of the given URL string.
063     * Wraps the {@link MalformedURLException} and {@link URISyntaxException} in case the passed URL is invalid.
064     *
065     * @param url Raw/decoded string form of a URL to parse/encode.
066     * @return Parsed/encoded URI (as string) that represents the
067     * @throws IllegalArgumentException in case the URL string is invalid.
068     * @deprecated To be remvoed with 4.0.0
069     */
070    @Deprecated
071    public static String encodeURLToString( String url )
072    {
073        try
074        {
075            return encodeURL( url ).toString();
076        }
077        catch ( Exception e )
078        {
079            throw new IllegalArgumentException( String.format( "Error parsing url: %s", url ), e );
080        }
081    }
082
083    /**
084     * Parses and returns an encoded version of the given URL string alongside the given path.
085     *
086     * @param baseUrl Base URL to use when constructing the final URL. This has to be a valid URL already.
087     * @param path Additional unencoded path to append at the end of the base path.
088     * @return Composed URL (base + path) already encoded, separating the individual path segments by "/".
089     * @since TODO
090     */
091    public static String encodeURLToString( String baseUrl, String path )
092    {
093        String[] pathSegments = path == null ? new String[0] : path.split( "/" );
094
095        String encodedUrl = encodeURLToString( baseUrl, pathSegments );
096        if ( path != null && path.endsWith( "/" ) )
097        {
098            return encodedUrl + "/";
099        }
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}