001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.rewriter;
018:
019: import java.util.StringTokenizer;
020:
021: import javax.portlet.PortletURL;
022:
023: /**
024: * WebContentRewriter
025: *
026: * @author <a href="mailto:rogerrutr@apache.org">Roger Ruttimann </a>
027: * @version $Id: WebContentRewriter.java 516448 2007-03-09 16:25:47Z ate $
028: */
029: public class WebContentRewriter extends RulesetRewriterImpl implements
030: Rewriter {
031: /* (non-Javadoc)
032: * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#convertTagEvent(java.lang.String, org.xml.sax.Attributes)
033: */
034: public void enterConvertTagEvent(String tagid,
035: MutableAttributes attributes) {
036: super .enterConvertTagEvent(tagid, attributes);
037: }
038:
039: /** parameters that need to be propagated in the action URL (since HTTP request parameters will not be available) */
040: public static final String ACTION_PARAMETER_URL = "_AP_URL";
041: public static final String ACTION_PARAMETER_METHOD = "_AP_METHOD";
042:
043: /*
044: * Portlet URL will be used to replace all URL's
045: */
046: private PortletURL actionURL = null;
047:
048: /**
049: * Setters/getters for members
050: */
051: public void setActionURL(PortletURL action) {
052: this .actionURL = action;
053: }
054:
055: public PortletURL getActionURL() {
056: return this .actionURL;
057: }
058:
059: /**
060: * rewriteURL
061: *
062: * @param url
063: * @param tag
064: * @param attribute
065: * @param otherAttributes
066: * @return the modified url which is a portlet action
067: *
068: * Rewrites all urls HREFS with a portlet action
069: */
070: public String rewriteUrl(String url, String tag, String attribute,
071: MutableAttributes otherAttributes) {
072: String modifiedURL = url;
073: modifiedURL = getModifiedURL(url);
074:
075: // translate "submit" URL's as actions
076: // <A href="..."/>
077: // <FORM submit="..."/>
078: if ((tag.equalsIgnoreCase("A") && attribute
079: .equalsIgnoreCase("href"))
080: || (tag.equalsIgnoreCase("FORM") && attribute
081: .equalsIgnoreCase("action")))
082:
083: {
084: // Regular URL just add a portlet action
085: if (this .actionURL != null) {
086: // create Action URL
087: actionURL.setParameter(ACTION_PARAMETER_URL,
088: modifiedURL);
089: if (tag.equalsIgnoreCase("FORM")) {
090: String httpMethod = otherAttributes
091: .getValue("method");
092: if (httpMethod != null)
093: actionURL.setParameter(ACTION_PARAMETER_METHOD,
094: httpMethod);
095: }
096: modifiedURL = actionURL.toString();
097: }
098: }
099:
100: // Deal with links in an "onclick".
101: if (attribute.equalsIgnoreCase("onclick")) {
102: // Check for onclick with location change
103: for (int i = 0; i < otherAttributes.getLength(); i++) {
104:
105: String name = otherAttributes.getQName(i);
106:
107: if (name.equalsIgnoreCase("onclick")) {
108:
109: String value = otherAttributes.getValue(i);
110:
111: int index = value.indexOf(".location=");
112: if (index >= 0) {
113: String oldLocation = value.substring(index
114: + ".location=".length());
115: StringTokenizer tokenizer = new StringTokenizer(
116: oldLocation, "\'\"");
117: oldLocation = tokenizer.nextToken();
118:
119: modifiedURL = oldLocation;
120: url = oldLocation;
121: modifiedURL = getModifiedURL(url);
122:
123: // Regular URL just add a portlet action
124: if (this .actionURL != null) {
125: // create Action URL
126: actionURL.setParameter(
127: ACTION_PARAMETER_URL, modifiedURL);
128: modifiedURL = actionURL.toString();
129: }
130:
131: modifiedURL = value.replaceAll(oldLocation,
132: modifiedURL);
133: }
134: }
135: }
136:
137: }
138:
139: // if ( !url.equalsIgnoreCase( modifiedURL ))
140: // System.out.println("WebContentRewriter.rewriteUrl() - In tag: "+tag+", for attribute: "+attribute+", converted url: "+url+", to: "+modifiedURL+", base URL was: "+getBaseUrl());
141:
142: return modifiedURL;
143: }
144:
145: private String getModifiedURL(String url) {
146: String modifiedURL = url;
147: // Any relative URL needs to be converted to a full URL
148: if (url.startsWith("/")
149: || (!url.startsWith("http:") && !url
150: .startsWith("https:"))) {
151: if (this .getBaseUrl() != null) {
152: modifiedURL = getBaseRelativeUrl(url);
153: // System.out.println("WebContentRewriter.rewriteUrl() - translated URL relative to base URL - result is: "+modifiedURL);
154: } else {
155: modifiedURL = url; // leave as is
156: }
157: }
158: return modifiedURL;
159: }
160: }
|