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.portlet;
17  
18  import java.net.URI;
19  import java.net.URISyntaxException;
20  import java.util.regex.Pattern;
21  
22  import javax.portlet.RenderRequest;
23  import javax.portlet.RenderResponse;
24  
25  /***
26   * Functions class for XSLT used for rewriting urls etc.
27   * 
28   * @author JMcCrindle
29   * @author rickard
30   */
31  public class BridgeFunctions implements LinkRewriter {
32      
33      private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
34      .getLog(BridgeFunctions.class);
35  
36      private final URI currentUrl;
37  
38      private final RenderRequest request;
39  
40      private final RenderResponse response;
41  
42      private final String servletName;
43  
44      private final PortletBridgeMemento memento;
45  
46      private final Pattern scope;
47  
48      private final PerPortletMemento perPortletMemento;
49  
50      private final IdGenerator idGenerator;
51  
52      private final ContentRewriter javascriptRewriter;
53  
54      private final ContentRewriter cssRewriter;
55  
56      public BridgeFunctions(ContentRewriter javascriptRewriter, ContentRewriter cssRewriter, IdGenerator idGenerator, PortletBridgeMemento memento,
57              PerPortletMemento perPortletMemento, String servletName,
58              URI currentUrl, RenderRequest request, RenderResponse response) {
59          this.javascriptRewriter = javascriptRewriter;
60          this.cssRewriter = cssRewriter;
61          this.idGenerator = idGenerator;
62          this.memento = memento;
63          this.perPortletMemento = perPortletMemento;
64          this.servletName = servletName;
65          this.currentUrl = currentUrl;
66          this.request = request;
67          this.response = response;
68          if(perPortletMemento != null) {
69              this.scope = perPortletMemento.getScope();
70          } else {
71              this.scope = Pattern.compile(".*");
72          }
73      }
74  
75      public String link(String baseUrl, String link) {
76          if (link.startsWith("javascript:")) {
77              return script(baseUrl, link);
78          } else if (link.equals("#")) {
79              return link;
80          } else {
81              return rewrite(baseUrl, link, true);
82          }
83      }
84  
85      private String rewrite(String baseUrl, String link, boolean checkScope) {
86          // replacing spaces in the url with +'s because... well, there shouldn't be spaces.
87          String trim = link.trim().replace(' ', '+');
88          URI url = null;
89          if(baseUrl != null && baseUrl.trim().length() > 0) {
90              // consider caching this
91              URI baseUri = currentUrl.resolve(baseUrl);
92              url = baseUri.resolve(trim);
93          } else {
94              url = currentUrl.resolve(trim);
95          }
96          if (url.getScheme().equals("http") || url.getScheme().equals("https")) {
97              if (!checkScope || shouldRewrite(url)) {
98                  BridgeRequest bridgeRequest = memento.createBridgeRequest(
99                          response, idGenerator.nextId(), url);
100                 String name = url.getPath();
101                 int lastIndex = name.lastIndexOf('/');
102                 if (lastIndex != -1) {
103                     name = name.substring(lastIndex + 1);
104                     if (name.equals("") && lastIndex > 0)
105                         name = url.getPath().substring(
106                                 url.getPath().lastIndexOf('/', lastIndex - 1));
107 
108                 }
109                 if (name.startsWith("/"))
110                     name = name.substring(1);
111                 name = request.getContextPath() + '/' + servletName + '/'
112                         + bridgeRequest.getId() + "/" + name;
113                 return name;
114             } else {
115                 return url.toString();
116             }
117         } else {
118             return link;
119         }
120     }
121 
122     /*
123      * (non-Javadoc)
124      * 
125      * @see org.portletbridge.StyleSheetRewriter#rewrite(java.lang.String)
126      */
127     public String style(String baseUrl, String css) {
128         return cssRewriter.rewrite(baseUrl, css, this);
129     }
130 
131     private boolean shouldRewrite(URI uri) {
132         return scope.matcher(uri.toString()).matches();
133     }
134 
135     public String script(String baseUrl, String script) {
136         return javascriptRewriter.rewrite(baseUrl, script, this);
137     }
138 
139     public URI getCurrentUrl() {
140         return currentUrl;
141     }
142     
143     public void setTitle(String title) {
144         response.setTitle(title);
145     }
146 
147     public PortletBridgeMemento getMemento() {
148         return memento;
149     }
150 
151     public PerPortletMemento getPerPortletMemento() {
152         return perPortletMemento;
153     }
154 
155     public RenderRequest getRequest() {
156         return request;
157     }
158 
159     public RenderResponse getResponse() {
160         return response;
161     }
162 
163     public String getServletName() {
164         return servletName;
165     }
166     
167     public boolean equalsIgnoreCase(String s1, String s2) {
168         if(s1 == s2) return true;
169         if(s1 == null) return false;
170         return s1.equalsIgnoreCase(s2);
171     }
172 
173 }