View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.rewriter;
18  
19  import java.util.StringTokenizer;
20  
21  import javax.portlet.PortletURL;
22  
23  /***
24   * WebContentRewriter
25   * 
26   * @author <a href="mailto:rogerrutr@apache.org">Roger Ruttimann </a>
27   * @version $Id: WebContentRewriter.java 516448 2007-03-09 16:25:47Z ate $
28   */
29  public class WebContentRewriter extends RulesetRewriterImpl implements Rewriter
30  {
31      /* (non-Javadoc)
32      * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#convertTagEvent(java.lang.String, org.xml.sax.Attributes)
33      */
34      public void enterConvertTagEvent(String tagid, MutableAttributes attributes) {
35          super.enterConvertTagEvent(tagid, attributes);
36      }
37  
38      /*** parameters that need to be propagated in the action URL (since HTTP request parameters will not be available) */
39      public static final String ACTION_PARAMETER_URL    = "_AP_URL";
40      public static final String ACTION_PARAMETER_METHOD = "_AP_METHOD";
41  
42      /*
43       * Portlet URL will be used to replace all URL's
44       */
45      private PortletURL actionURL = null;
46  
47      /***
48       * Setters/getters for members
49       */
50      public void setActionURL(PortletURL action)
51      {
52          this.actionURL = action;
53      }
54  
55      public PortletURL getActionURL()
56      {
57          return this.actionURL;
58      }
59  
60      /***
61       * rewriteURL
62       * 
63       * @param url
64       * @param tag
65       * @param attribute
66       * @param otherAttributes
67       * @return the modified url which is a portlet action
68       * 
69       * Rewrites all urls HREFS with a portlet action
70       */
71      public String rewriteUrl(String url, String tag, String attribute, MutableAttributes otherAttributes)
72      {
73           String modifiedURL = url;
74          modifiedURL = getModifiedURL(url);
75  
76          // translate "submit" URL's as actions
77          //  <A href="..."/>
78          //  <FORM submit="..."/>
79          if (( tag.equalsIgnoreCase("A") && attribute.equalsIgnoreCase("href")) ||
80              ( tag.equalsIgnoreCase("FORM") && attribute.equalsIgnoreCase("action")))
81                  
82          {
83                  // Regular URL just add a portlet action
84                  if (this.actionURL != null)
85                  {
86                      // create Action URL
87                      actionURL.setParameter(ACTION_PARAMETER_URL, modifiedURL);
88                      if (tag.equalsIgnoreCase("FORM"))
89                      {
90                          String httpMethod = otherAttributes.getValue("method");
91                          if (httpMethod != null)
92                              actionURL.setParameter(ACTION_PARAMETER_METHOD, httpMethod);
93                      }
94                      modifiedURL = actionURL.toString();
95                  }
96          }
97  
98          // Deal with links in an "onclick".
99          if (attribute.equalsIgnoreCase("onclick"))
100         {
101             // Check for onclick with location change
102             for (int i=0; i < otherAttributes.getLength(); i++) {
103 
104                 String name = otherAttributes.getQName(i);
105 
106                 if (name.equalsIgnoreCase("onclick")) {
107 
108                     String value = otherAttributes.getValue(i);
109 
110                     int index = value.indexOf(".location=");
111                     if (index >= 0) {
112                         String oldLocation = value.substring(index + ".location=".length());
113                         StringTokenizer tokenizer = new StringTokenizer(oldLocation, "\'\"");
114                         oldLocation = tokenizer.nextToken();
115 
116                         modifiedURL = oldLocation;
117                         url = oldLocation;
118                         modifiedURL = getModifiedURL(url);
119 
120                         // Regular URL just add a portlet action
121                         if (this.actionURL != null)
122                         {
123                             // create Action URL
124                             actionURL.setParameter(ACTION_PARAMETER_URL, modifiedURL);
125                             modifiedURL = actionURL.toString();
126                         }
127 
128 
129                         modifiedURL = value.replaceAll(oldLocation, modifiedURL);
130                     }
131                 }
132             }
133 
134 
135         }
136         
137         // if ( !url.equalsIgnoreCase( modifiedURL ))
138         //     System.out.println("WebContentRewriter.rewriteUrl() - In tag: "+tag+", for attribute: "+attribute+", converted url: "+url+", to: "+modifiedURL+", base URL was: "+getBaseUrl());
139 
140         return modifiedURL;
141     }
142 
143     private String getModifiedURL(String url) {
144         String modifiedURL = url;
145         // Any relative URL needs to be converted to a full URL
146         if (url.startsWith("/") || (!url.startsWith("http:") && !url.startsWith("https:")))
147         {
148             if (this.getBaseUrl() != null)
149             {
150                 modifiedURL = getBaseRelativeUrl(url) ;
151                 // System.out.println("WebContentRewriter.rewriteUrl() - translated URL relative to base URL - result is: "+modifiedURL);
152   	        }
153             else
154             {
155                 modifiedURL = url; // leave as is
156 	        }
157         }
158         return modifiedURL;
159     }
160 }