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