View Javadoc

1   /*
2    * Copyright 2010 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.opensaml.util;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * This class performs simple canonicalization of a URL as follows:
24   * 
25   * <p>
26   * <ul>
27   *   <li>The scheme is lower-cased.</li>
28   *   <li>The hostname is lower-cased</li>
29   *   <li>The port is removed if it is the default port registered for the scheme</li>
30   * </ul>
31   * </p>
32   * 
33   * <p>
34   * </p>
35   */
36  public final class SimpleURLCanonicalizer {
37      
38      /** The scheme-to-port mapping data. */
39      private static Map<String, Integer> schemePortMap = new HashMap<String, Integer>();
40      
41      /** Constructor to prevent instantiation.  */
42      private SimpleURLCanonicalizer() {}
43  
44      /**
45       * Register a new scheme-to-port mapping.
46       * 
47       * @param scheme the scheme to register
48       * @param port the default port for that scheme
49       */
50      public static void registerSchemePortMapping(String scheme, Integer port) {
51          if (scheme == null || port == null) {
52              throw new IllegalArgumentException("Scheme and port may not be null");
53          }
54          schemePortMap.put(scheme.toLowerCase(), port);
55      }
56      
57      /**
58       * Deregister a scheme-to-port mapping.
59       * 
60       * @param scheme the scheme to deregister
61       */
62      public static void deregisterSchemePortMapping(String scheme) {
63          if (scheme == null) {
64              throw new IllegalArgumentException("Scheme may not be null");
65          }
66          schemePortMap.remove(scheme.toLowerCase());
67      }
68      
69      /**
70       * Obtain the default port registered for a scheme.
71       * 
72       * @param scheme the scheme to look up
73       * @return the default port registered for the scheme, or null if none registered
74       */
75      public static Integer getRegisteredPort(String scheme) {
76          return schemePortMap.get(scheme.toLowerCase());
77      }
78      
79      /**
80       * Canonicalize the supplied URL.
81       * 
82       * @param url the URL to canonicalize
83       * @return the canonicalized URL
84       */
85      public static String canonicalize(String url) {
86          URLBuilder urlBuilder = new URLBuilder(url);
87          canonicalize(urlBuilder);
88          return urlBuilder.buildURL();
89      }
90      
91      /**
92       * Canonicalize the supplied URLBuilder data.
93       * 
94       * @param url the URLBuilder to canonicalize
95       */
96      private static void canonicalize(URLBuilder url) {
97          if (url.getScheme() != null) {
98              url.setScheme(url.getScheme().toLowerCase());
99              
100             String scheme = url.getScheme();
101             Integer port = getRegisteredPort(scheme);
102             if (port != null && port == url.getPort()) {
103                 url.setPort(0);
104             }
105         }
106         
107         if (url.getHost() != null) {
108             url.setHost(url.getHost().toLowerCase());
109         }
110     }
111     
112     static {
113         registerSchemePortMapping("ftp", 23);
114         registerSchemePortMapping("http", 80);
115         registerSchemePortMapping("https", 443);
116     }
117 
118 }