View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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  package org.portletbridge;
17  
18  /***
19   * @author JMcCrindle
20   */
21  public class UrlUtils {
22  
23      public static String getPath(String url) {
24          if(url == null) return null;
25          url = url.trim();
26          int i = url.lastIndexOf('/');
27          if(i >= 0) {
28              return url.substring(0, i);
29          } else {
30              return url;
31          }
32      }
33      
34      public static boolean isRelativeHttp(String url) {
35          if(url == null || url.length() == 0) return true;
36          return !isFullHttp(url) && url.charAt(0) != '/'; 
37      }
38      
39      public static boolean isAbsoluteHttp(String url) {
40          return !isAbsoluteWithHostHttp(url) && url.charAt(0) == '/'; 
41      }
42      
43      public static boolean isAbsoluteWithHostHttp(String url) {
44          return url.length() > 1 && url.charAt(0) == '/' && url.charAt(1) == '/'; 
45      }
46      
47      public static boolean isFull(String url) {
48          int indexOfFirstSlash = url.indexOf('/');
49          int indexOfColon = url.indexOf(':');
50          if(indexOfColon >= 0 && indexOfFirstSlash >= 0 && indexOfColon < indexOfFirstSlash) {
51              return true;
52          }
53          return false;
54      }
55  
56      public static boolean isFullHttp(String url) {
57          int indexOfFirstSlash = url.indexOf('/');
58          int indexOfColon = url.indexOf(':');
59          if(indexOfColon >= 0 && indexOfFirstSlash >= 0 && indexOfColon < indexOfFirstSlash) {
60              return url.startsWith("http");
61          }
62          return false;
63      }
64  
65      public static String safeAppend(String path, String resource) {
66          if(resource == null || resource.length() == 0) resource = "/";
67          if(path == null || path.length() == 0) path = "/";
68          if(isFull(resource)) {
69              return resource;
70          }
71          boolean pathEndsWithSlash = path.charAt(path.length() - 1) == '/';
72          boolean resourceStartsWithSlash = resource.charAt(0) == '/';
73          return (pathEndsWithSlash ? path.substring(0, path.length() - 1) : path)
74              + "/" + (resourceStartsWithSlash ? resource.substring(1) : resource);
75      }
76  
77      public static String rewriteRelative(String baseUrl, String resource) {
78          if(resource == null) resource = "";
79          resource = resource.trim();
80          if(resource.length() > 0 && resource.charAt(0) == '/') {
81              return resource;
82          } else {
83              return safeAppend(baseUrl, resource);
84          }
85      }
86  
87      /***
88       * @param currentUrl
89       * @return
90       */
91      public static String getProtocolHostPort(String currentUrl) {
92          if(isFullHttp(currentUrl)) {
93              int indexOfThirdSlash = currentUrl.indexOf('/');
94              if(indexOfThirdSlash != -1) {
95                  indexOfThirdSlash = currentUrl.indexOf('/', indexOfThirdSlash + 2);
96                  if(indexOfThirdSlash == -1) {
97                      return currentUrl;
98                  } else {
99                      return currentUrl.substring(0, indexOfThirdSlash);
100                 }
101             } else {
102                 return null;
103             }
104         } else {
105             return null;
106         }
107     }
108 
109     /***
110      * @param currentUrl
111      * @return
112      */
113     public static String getProtocol(String currentUrl) {
114         int indexOfColon = currentUrl.indexOf(':');
115         if(indexOfColon != -1) {
116             return currentUrl.substring(0, indexOfColon);
117         } else {
118             return null;
119         }
120     }
121 
122 }