View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.renderers;
17  
18  import static org.apache.commons.lang3.StringUtils.stripEnd;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  import org.esigate.Renderer;
24  import org.esigate.impl.DriverRequest;
25  import org.esigate.impl.UrlRewriter;
26  
27  /**
28   * This renderer "fixes" links to resources, images and pages in pages retrieved by esigate :
29   * <ul>
30   * <li>Current-path-relative urls are converted to full path relative urls ( img/test.img -&gt;
31   * /myapp/curentpath/img/test.img)</li>
32   * <li>All relative urls can be converted to absolute urls (including server name)</li>
33   * </ul>
34   * 
35   * This enables use of esigate without any special modifications of the generated urls on the provider side.
36   * 
37   * All href and src attributes are processed, except javascript links.
38   * 
39   * @author Nicolas Richeton
40   * 
41   */
42  public class ResourceFixupRenderer implements Renderer {
43      private final String baseUrl;
44      private final String requestUrl;
45      private final UrlRewriter urlRewriter;
46      private final String visibleBaseUrl;
47      private final boolean absolute;
48  
49      /**
50       * Creates a renderer which fixes urls. The domain name and the url path are computed from the full url made of
51       * baseUrl + pageFullPath.
52       * 
53       * If mode is ABSOLUTE, all relative urls will be replaced by the full urls :
54       * <ul>
55       * <li>images/image.png is replaced by http://server/context/images/image.png</li>;
56       * <li>/context/images/image.png is replaced by http://server/context/images/image.png</li>;
57       * </ul>
58       * 
59       * If mode is RELATIVE, context will be added to relative urls :
60       * <ul>
61       * <li>images/image.png is replaced by /context/images/image.png</li>
62       * </ul>
63       * 
64       * @param baseUrl
65       *            Base url (same as configured in provider).
66       * @param requestUrl
67       *            Page as used in tag lib or using API
68       * @param urlRewriter
69       *            The url rewriter for this provider
70       * @param visibleBaseUrl
71       *            The base url as seen from the client
72       * @param absolute
73       *            Should the rewritten urls contain the scheme host and port
74       */
75      public ResourceFixupRenderer(String baseUrl, String requestUrl, UrlRewriter urlRewriter, String visibleBaseUrl,
76              boolean absolute) {
77          this.baseUrl = stripEnd(baseUrl, "/");
78          this.requestUrl = requestUrl;
79          this.urlRewriter = urlRewriter;
80          this.visibleBaseUrl = visibleBaseUrl;
81          this.absolute = absolute;
82      }
83  
84      @Override
85      public void render(DriverRequest httpRequest, String src, Writer out) throws IOException {
86          out.write(urlRewriter.rewriteHtml(src, requestUrl, baseUrl, visibleBaseUrl, absolute).toString());
87      }
88  
89  }