View Javadoc

1   /*
2    * Copyright 2000-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  
17  package org.apache.jetspeed.util.rewriter;
18  
19  
20  // java.io
21  import java.io.Reader;
22  
23  // java.net
24  import java.net.MalformedURLException;
25  
26  // this makes it dependent on Swing...need an abstraction WTP
27  import javax.swing.text.html.HTML;
28  import javax.swing.text.MutableAttributeSet;
29  
30  /***
31   *
32   * Sample of extending HTML Rewriter for your specific needs
33   *
34   *
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36   * @version $Id: SampleRewriter.java,v 1.5 2004/02/23 03:18:59 jford Exp $
37   */
38  
39  public class SampleRewriter extends HTMLRewriter
40  {
41      private boolean debug = false;
42      private String basePortalURL;
43      private String fullPortalURL;
44      private String sampleURL;
45  
46      private String sessionID = "NONE";
47      private String formID = "NONE";
48      private boolean sampleEndFlag = false;
49  
50      public String getSessionID()
51      {
52          return sessionID;
53      }
54  
55      public String getFormID()
56      {
57          return formID;
58      }
59  
60      public boolean getSampleEndFlag()
61      {
62          return sampleEndFlag;
63      }
64  
65      /*
66       * Basic constructor for creating a Sample Rewriter.
67       *
68       * @param basePortalURL Base Portal URL
69       * @param fullPortalURL  Full Portal URL with path parameters and query strings (sessionid)
70       * @param sampleURL  The sample URL.
71       *
72       */
73      public SampleRewriter(String basePortalURL, String fullPortalURL, String sampleURL )
74      {
75          this.basePortalURL = basePortalURL;
76          this.fullPortalURL = fullPortalURL;
77          this.sampleURL = sampleURL;
78      }
79  
80      /*
81       * Entry point into rewriting HTML content.
82       *
83       * Reads stream from proxied host, runs configured HTML parser against that stream,
84       * rewriting relevant links, and writes the parsed stream back to the client.
85       *
86       * @param input the HTML input stream.
87       * @param baseURL the base URL of the target host.
88       * @return the rewritten HTML output stream.
89       *
90       * @exception MalformedURLException a servlet exception.
91       */
92  
93      public String rewrite(Reader input, String baseURL)
94                                 throws MalformedURLException
95      {
96          String rewrittenHTML = "";
97          this.basePortalURL = baseURL;
98  
99          HTMLParserAdaptor parser = new SwingParserAdaptor(this);
100         rewrittenHTML = parser.run(input);
101 
102         return rewrittenHTML;
103     }
104 
105     /*
106      * <p>
107      * This callback is called by the HTMLParserAdaptor implementation to write
108      * back all rewritten URLs to point to the proxy server. The MMS implementation
109      * writes specifically for network element ids and relative paths to MMS
110      * resources.
111      * </p>
112      * <p>
113      * Given the targetURL, rewrites the link as a link back to the proxy server.
114      * </p>
115      *
116      * Example format:
117      *
118      *   http://proxyserver/proxy?neid=id?nepath=path
119      *
120      * @param targetURL the URL to be rewritten back to the proxy server.
121      * @param baseURL the base URL of the target host.
122      * @param proxyURL the base URL of the proxy server.
123      * @return the rewritten URL to the proxy server.
124      *
125      * @exception MalformedURLException a servlet exception.
126      */
127     public String generateNewUrl(String targetURL, HTML.Tag tag, HTML.Attribute attribute)
128     {
129         if (debug)
130             System.out.println("[rewriter] Tag: " + tag.toString() + "  Attribute: " + attribute.toString() + "  targetURL: " + targetURL + "  target = " + fullPortalURL + "]");
131 
132         // The only URL we want to re-write is ACTION attribute of the <FORM> tag.
133         // Ignore all others
134         if (tag == HTML.Tag.FORM && attribute == HTML.Attribute.ACTION) {
135 
136             // Strip the session Id value out of the ACTION attribute value
137             int sessionLocation = targetURL.indexOf( "?sessionId" );
138             if (sessionLocation > -1) {
139                 int equalsLocation = targetURL.indexOf( "=", sessionLocation );
140                 if (equalsLocation > -1) {
141                     int ampLocation = targetURL.indexOf( "&", equalsLocation );
142                     if (ampLocation > -1) {
143                         sessionID = targetURL.substring( equalsLocation + 1, ampLocation );
144                     } else {
145                         sessionID = targetURL.substring( equalsLocation + 1 );
146                     }
147                 }
148             }
149 
150             if (sampleEndFlag) {
151                 // The sample session is being terminated, make the form action return to the portal home page
152                 return basePortalURL;
153             } else {
154                 // Make the form action run the same portal page
155                 return fullPortalURL;
156             }
157         }
158 
159         // This is a tag that we do not wish to re-write, return it's own value unmodified
160         return targetURL;
161     }
162 
163     /*
164      * Returns true if all rewritten URLs should be sent back to the proxy server.
165      *
166      * @return true if all URLs are rewritten back to proxy server.
167      */
168     public boolean proxyAllTags()
169     {
170         return true;
171     }
172 
173     /*
174      * Start Tag Events
175      */
176     public String exitStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
177     {
178         if (tag == HTML.Tag.FORM)
179         {
180             String inputTag = "<input type='hidden' name='sessionId' value='" + sessionID + "'/>";
181             return inputTag;
182         }
183         return null;
184     }
185 
186     /*
187      * Simple Tag Events
188      */
189 
190     public boolean enterSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
191     {
192         if (tag == HTML.Tag.META)
193         {
194             Object o = attrs.getAttribute(HTML.Attribute.NAME);
195             if (o != null)
196             {
197                 String s = o.toString();
198                 if (s.equalsIgnoreCase("SampleEnd"))
199                 {
200                     sampleEndFlag = true;
201                 }
202             }
203         }
204         return true;
205     }
206 
207 
208     /*
209      * Convert Tag Events
210      */
211 
212     public void convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs)
213     {
214         if (tag == HTML.Tag.FORM) {
215            // All forms from sample will have the same form NAME.
216            // Jetspeed will add its own FORM depending on the type of portlet
217            // being used.  So if you have multiple forms, any Javascript will
218            // have to know which form to reference.
219            attrs.addAttribute("NAME","SampleForm");
220         }
221 
222         // INPUT Tag
223         if (tag == HTML.Tag.INPUT)
224         {
225             Object o = attrs.getAttribute(HTML.Attribute.NAME);
226             if (o != null)
227             {
228                 String s = o.toString();
229                 if (s.equalsIgnoreCase("FormID"))
230                 {
231                     o = attrs.getAttribute(HTML.Attribute.VALUE);
232                     if (o != null)
233                     {
234                         formID = o.toString();
235                     }
236                 }
237             }
238         }
239 
240     }
241 
242 }
243