001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels.webproxy;
007:
008: import org.jasig.portal.ChannelRuntimeData;
009: import org.jasig.portal.PortalException;
010: import org.jasig.portal.utils.SAX2FilterImpl;
011: import org.xml.sax.Attributes;
012: import org.xml.sax.ContentHandler;
013: import org.xml.sax.helpers.AttributesImpl;
014:
015: /**
016: * Rewrites URLs for CWebProxy.
017: * @author Sarah Arnott, sarnott@mun.ca
018: * @version $Revision: 42283 $
019: */
020: public abstract class CWebProxyURLFilter extends SAX2FilterImpl {
021:
022: protected ChannelRuntimeData runtimeData;
023: protected String baseUrl;
024:
025: /**
026: * A constructor which receives a ContentHandler to which
027: * filtered SAX events are passed.
028: * @param handler the ContentHandler to which filtered SAX events are passed
029: */
030: protected CWebProxyURLFilter(ContentHandler handler) {
031: super (handler);
032: }
033:
034: /**
035: * A factory method that uses mimeType to determine
036: * which type of CWebProxyURLFilter to return.
037: * There are currently two types of markup supported: XHTML and WML.
038: * @param handler the ContentHandler used to pass along filtered SAX events
039: * @param runtimeData the CWebProxy channel runtime data
040: */
041: public static final CWebProxyURLFilter newCWebProxyURLFilter(
042: String mimeType, ChannelRuntimeData runtimeData,
043: ContentHandler handler) throws PortalException {
044: // Create a CWebProxyURLFilter, depending on mime type
045: CWebProxyURLFilter filter = null;
046: if (mimeType != null) {
047: if (mimeType.equals("text/html"))
048: filter = new CWebProxyXHTMLURLFilter(handler);
049: else if (mimeType.equals("text/vnd.wap.wml"))
050: filter = new CWebProxyWMLURLFilter(handler);
051: else
052: throw new PortalException(
053: "CWebProxyURLFilter.newCWebProxyURLFilter(): Unable to locate CWebProxyURLFilter for mime type '"
054: + mimeType + "'");
055: } else {
056: throw new PortalException(
057: "CWebProxyURLFilter.newCWebProxyURLFilter(): Unable to create CWebProxyURLFilter. Mime type is null.");
058: }
059:
060: // Set CWebProxyURLFilter properties
061: filter.runtimeData = runtimeData;
062: filter.baseUrl = (String) runtimeData.get("cw_xml");
063:
064: return filter;
065: }
066:
067: /**
068: * A helper method which rewrites an attribute that has a URL value
069: * for CWebProxy. The URL rewriting is dependant upon the values
070: * of runtime data parameters (see CWebProxy documentation).
071: *
072: * @param elementName the element name containing an attribute of name attName
073: * @param attName the name of the attribute of elementName
074: * @param qName the name of the current element
075: * @param attsImpl the attributes implementation to contain the new attribute value
076: */
077: protected final void rewriteURL(String elementName, String attName,
078: String qName, Attributes atts, AttributesImpl attsImpl) {
079: if (qName.equalsIgnoreCase(elementName)) {
080: String passThrough = (String) runtimeData
081: .get("cw_passThrough");
082: if (passThrough != null
083: && (passThrough.equals("all")
084: || passThrough.equals("application") || passThrough
085: .equals("marked"))) {
086: String attValue = atts.getValue(attName);
087: if (attValue != null
088: && (attValue.startsWith("http://") || attValue
089: .startsWith("https://"))) {
090: String query = getQueryString(attValue);
091: String base = getBase(attValue);
092:
093: // determine the actionURL
094: String actionURL;
095: if (attValue.indexOf("cw_download=") != -1)
096: actionURL = (String) runtimeData
097: .get("downloadActionURL");
098: else
099: actionURL = (String) runtimeData
100: .get("baseActionURL");
101:
102: String xmlUri = (String) runtimeData.get("cw_xml");
103:
104: // rewrite URL when required
105: if (passThrough.equals("marked")) {
106: if (attValue.indexOf("cw_inChannelLink=") != -1) {
107: if ((attValue.trim().equals("") || xmlUri
108: .equals(base)))
109: attValue = actionURL + query;
110: else if (!query.equals(""))
111: attValue = actionURL + query
112: + "&cw_xml=" + base;
113: else
114: attValue = actionURL + "?cw_xml="
115: + base;
116: }
117: } else if (passThrough.equals("application")) {
118: if (attValue.trim().equals("")
119: || xmlUri.equals(base))
120: attValue = actionURL + query;
121: } else if (passThrough.equals("all")) {
122: if (attValue.trim().equals("")
123: || xmlUri.equals(base))
124: attValue = actionURL + query;
125: else if (!query.equals(""))
126: attValue = actionURL + query + "&cw_xml="
127: + base;
128: else
129: attValue = actionURL + "?cw_xml="
130: + attValue;
131: }
132:
133: int index = atts.getIndex(attName);
134: attsImpl.setAttribute(index, atts.getURI(index),
135: atts.getLocalName(index), attName, atts
136: .getType(index), attValue);
137: }
138: }
139: }
140: }
141:
142: /**
143: * Returns the portion of the URL without the query string.
144: * @param url A String representing the absolute URL.
145: */
146: protected String getBase(String url) {
147: if (url.indexOf("?") != -1)
148: return url.substring(0, url.indexOf("?"));
149: else
150: return url;
151: }
152:
153: /**
154: * Returns the query string portion of the URL.
155: * @param url A String representing the absolute URL.
156: */
157: protected String getQueryString(String url) {
158: if (url.indexOf("?") != -1)
159: return url.substring(url.indexOf("?"));
160: else
161: return "";
162: }
163:
164: }
|