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.rewriter;
17  
18  import org.portletbridge.UrlRewriter;
19  import org.portletbridge.UrlUtils;
20  
21  /***
22   * @author JMcCrindle
23   */
24  public class DefaultUrlRewriter implements UrlRewriter {
25      
26      private String currentUrl;
27      private String currentProtocolHostPort;
28      private String newBaseUrl;
29  
30      /***
31       * @param baseUrl
32       */
33      public DefaultUrlRewriter(String currentUrl, String newBaseUrl) {
34          this.currentUrl = currentUrl;
35          this.newBaseUrl = newBaseUrl;
36          this.currentProtocolHostPort = UrlUtils.getProtocolHostPort(currentUrl);
37      }
38  
39      /* (non-Javadoc)
40       * @see org.portletbridge.xsl.LinkRewriter#rewrite(java.lang.String)
41       */
42      public String rewrite(String url) {
43          if(UrlUtils.isRelativeHttp(url)) {
44              return url;
45          } else if(UrlUtils.isAbsoluteHttp(url)) {
46              return UrlUtils.safeAppend(newBaseUrl, url);
47          } else if(UrlUtils.isFullHttp(url)) {
48              if(currentProtocolHostPort != null) {
49                  if(url.startsWith(currentProtocolHostPort)) {
50                      return UrlUtils.safeAppend(newBaseUrl, url.substring(currentProtocolHostPort.length()));
51                  } else {
52                      return url;
53                  }
54              } else {
55                  return url;
56              }
57          } else {
58              // it's not http
59              return url;
60          }
61      }
62      
63  }