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.io.IOException;
19  import java.io.Reader;
20  import java.net.URI;
21  
22  import javax.portlet.PortletPreferences;
23  import javax.portlet.RenderRequest;
24  import javax.portlet.RenderResponse;
25  import javax.xml.transform.Templates;
26  import javax.xml.transform.Transformer;
27  import javax.xml.transform.TransformerConfigurationException;
28  import javax.xml.transform.TransformerException;
29  import javax.xml.transform.sax.SAXSource;
30  import javax.xml.transform.stream.StreamResult;
31  
32  import org.portletbridge.ResourceException;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.XMLReader;
35  
36  /***
37   * Bridge Transformer that uses the standard XSLT transformers
38   * 
39   * @author JMcCrindle
40   * @author rickard
41   */
42  public class AltBridgeTransformer implements
43          BridgeTransformer {
44  
45      private TemplateFactory templateFactory = null;
46      private XMLReader parser;
47      private String servletName;
48      private final BridgeFunctionsFactory bridgeFunctionsFactory;
49  
50      /***
51       * 
52       */
53      public AltBridgeTransformer(BridgeFunctionsFactory bridgeFunctionsFactory, TemplateFactory templateFactory, XMLReader parser, String servletName) {
54          this.bridgeFunctionsFactory = bridgeFunctionsFactory;
55          this.templateFactory = templateFactory;
56          this.parser = parser;
57          this.servletName = servletName;
58      }
59  
60      /***
61       * Transforms the HTML from a downstream site using a configured XSL
62       * stylesheet.
63       * 
64       * @param request
65       *            the render request
66       * @param response
67       *            the render response
68       * @param in
69       *            the http result from calling the downstream site.
70       * @throws ResourceException
71       *             if there was a problem doing the transform (e.g. if the
72       *             stylesheet throws an error).
73       */
74      public void transform(PortletBridgeMemento memento, PerPortletMemento perPortletMemento, URI currentUrl,
75              RenderRequest request, RenderResponse response,
76              Reader in) throws ResourceException {
77          try {
78              PortletPreferences preferences = request.getPreferences();
79              String stylesheet = preferences.getValue("stylesheet", null);
80              String stylesheetUrl = preferences.getValue("stylesheetUrl", null);
81              Templates templates = null;
82              if(stylesheetUrl != null) {
83                  templates = templateFactory.getTemplatesFromUrl(stylesheetUrl);
84              } else {
85                  templates = templateFactory.getTemplatesFromString(stylesheet);
86              }
87              Transformer transformer = templates.newTransformer();
88              transformer.setParameter("bridge", bridgeFunctionsFactory.createBridgeFunctions(memento, perPortletMemento, servletName,
89                      currentUrl, request, response));
90              transformer.transform(new SAXSource(parser, new InputSource(in)), new StreamResult(response.getWriter()));
91          } catch (TransformerConfigurationException e) {
92              throw new ResourceException("error.transformer", e
93                      .getLocalizedMessage(), e);
94          } catch (IOException e) {
95              throw new ResourceException("error.filter.io", e
96                      .getLocalizedMessage(), e);
97          } catch (TransformerException e) {
98              throw new ResourceException("error.transformer", e
99                      .getLocalizedMessage(), e);
100         }
101 
102     }
103 
104     public void setTemplateFactory(TemplateFactory templateFactory) {
105         this.templateFactory = templateFactory;
106     }
107 }