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 FullUrlRewriter implements UrlRewriter {
25      
26      private String currentUrl;
27      private String currentProtocolHostPort;
28      private String newBaseUrl;
29      private String newBasePath;
30      private String newBaseContext;
31  
32      /***
33       * @param baseUrl
34       */
35      public FullUrlRewriter(String currentUrl, String newBaseUrl) {
36          this.currentUrl = currentUrl;
37          this.newBaseUrl = newBaseUrl;
38          this.newBaseContext = getBaseContext(newBaseUrl);
39          this.newBasePath = newBaseContext + '/' + UrlUtils.getProtocolHostPort(newBaseUrl.substring(newBaseContext.length() + 1));
40          this.currentProtocolHostPort = UrlUtils.getProtocolHostPort(currentUrl);
41      }
42  
43      /***
44       * @param newBaseUrl2
45       * @return
46       */
47      protected String getBaseContext(String newBaseUrl) {
48          int indexOfColon = newBaseUrl.indexOf(':');
49          if(indexOfColon != -1) {
50              int lastIndexOfContext = newBaseUrl.lastIndexOf('/', indexOfColon);
51              if(lastIndexOfContext == -1) {
52                  throw new RuntimeException("BaseUrl must have full url in it e.g. " +
53                          "/new/http://asdfadsaf:80 but is currently " + newBaseUrl);
54              }
55              return newBaseUrl.substring(0, lastIndexOfContext);
56          } else {
57              throw new RuntimeException("BaseUrl must have full url in it e.g. " +
58                      "/new/http://asdfadsaf:80 but is currently " + newBaseUrl);
59          }
60      }
61  
62      /* (non-Javadoc)
63       * @see org.portletbridge.xsl.LinkRewriter#rewrite(java.lang.String)
64       */
65      public String rewrite(String url) {
66          if(UrlUtils.isRelativeHttp(url)) {
67              return url;
68          } else if(UrlUtils.isAbsoluteWithHostHttp(url)) {
69              String protocol = UrlUtils.getProtocol(currentUrl);
70              return newBaseContext + '/' + protocol + ':' + url;
71          } else if(UrlUtils.isAbsoluteHttp(url)) {
72              return UrlUtils.safeAppend(newBasePath, url);
73          } else if(UrlUtils.isFullHttp(url)) {
74              if(currentProtocolHostPort != null) {
75                  if(url.startsWith(currentProtocolHostPort)) {
76                      if(url.length() == currentProtocolHostPort.length()) {
77                          return newBasePath + '/';
78                      } else {
79                          return UrlUtils.safeAppend(newBasePath, url.substring(currentProtocolHostPort.length()));
80                      }
81                  } else {
82                      String protocolHostPort = UrlUtils.getProtocolHostPort(url);
83                      if(protocolHostPort.length() == url.length()) {
84                          return newBaseContext + '/' + url + '/';
85                      } else {
86                          return newBaseContext + '/' + url;
87                      }
88                  }
89              } else {
90                  return url;
91              }
92          } else {
93              // it's not http
94              return url;
95          }
96      }
97      
98  }