taylor 2002/10/29 10:38:06 Modified: src/java/org/apache/jetspeed/modules/actions/controllers PanedControllerAction.java src/java/org/apache/jetspeed/portal/portlets AggregatePortlet.java src/java/org/apache/jetspeed/util/rewriter FrameRewriterTest.java HTMLParserAdaptor.java HTMLRewriter.java Rewriter.java SampleRewriter.java SwingParserAdaptor.java Added: src/java/org/apache/jetspeed/portal/portlets WebPagePortlet2.java src/java/org/apache/jetspeed/util Streams.java src/java/org/apache/jetspeed/util/rewriter ProxyRewriter.java TestRewriter.java Log: - implementation of WebPagePortlet2 - added starts of a Proxy-based rewriter. This is incomplete and shouldn't yet be used until all code is committed Revision Changes Path 1.8 +9 -3 jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/controllers/PanedControllerAction.java Index: PanedControllerAction.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/modules/actions/controllers/PanedControllerAction.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- PanedControllerAction.java 22 Mar 2002 18:42:06 -0000 1.7 +++ PanedControllerAction.java 29 Oct 2002 18:38:05 -0000 1.8 @@ -65,6 +65,7 @@ import org.apache.jetspeed.om.profile.Entry; import org.apache.jetspeed.om.profile.MetaInfo; import org.apache.jetspeed.om.profile.Layout; +import org.apache.jetspeed.services.resources.JetspeedResources; // Turbine stuff import org.apache.turbine.util.Log; @@ -104,13 +105,18 @@ Portlet portlet = null; String paneID = null; - String paneName = rundata.getParameters().getString( "js_panename" ); + String paneName = rundata.getParameters().getString( JetspeedResources.PATH_PANENAME_KEY ); + if (null != paneName) { portlet = myPortlets.getPortletByName(paneName); if (portlet != null) + { paneID = portlet.getID(); + rundata.getParameters().setString(JetspeedResources.PATH_PANEID_KEY, paneID); + } } + if (null == portlet) { paneID = cont.retrievePaneID(rundata, true); @@ -120,8 +126,8 @@ paneID = cont.retrievePaneID(rundata, false); portlet = myPortlets.getPortletByID(paneID); } - } + if (portlet != null) context.put("pane", portlet); cont.savePaneID(rundata, paneID); 1.3 +5 -2 jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/AggregatePortlet.java Index: AggregatePortlet.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/AggregatePortlet.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AggregatePortlet.java 27 Sep 2002 19:54:26 -0000 1.2 +++ AggregatePortlet.java 29 Oct 2002 18:38:05 -0000 1.3 @@ -111,15 +111,18 @@ { return new JetspeedClearElement("Path parameter not set"); } - +System.err.println("path = " + path); ProfileLocator locator = new BaseProfileLocator(); locator.createFromPath(path); String id = locator.getId(); +System.err.println("id = " + id); try { Profile profile = Profiler.getProfile(locator); PSMLDocument doc = profile.getDocument(); +System.err.println("doc = " + doc); + if (doc == null) { return null; 1.1 jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/WebPagePortlet2.java Index: WebPagePortlet2.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Jetspeed" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" or * "Apache Jetspeed", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.jetspeed.portal.portlets; //Element Construction Set import org.apache.jetspeed.util.JetspeedClearElement; import org.apache.ecs.ConcreteElement; //Jetspeed stuff import org.apache.jetspeed.portal.PortletConfig; import org.apache.jetspeed.portal.PortletException; import org.apache.jetspeed.util.rewriter.Rewriter; import org.apache.jetspeed.util.rewriter.HTMLRewriter; import org.apache.jetspeed.util.Base64; //turbine import org.apache.turbine.util.Log; import org.apache.turbine.util.RunData; //standard java stuff import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; import java.net.URL; import java.net.URLConnection; /** * A class that loads a web page and filters it to have certain features * deleted. * * @author <a href="mailto:taylor@;apache.org">David Sean Taylor</a> */ public class WebPagePortlet2 extends AbstractInstancePortlet { protected Rewriter rewriter = null; protected boolean initDone = false; protected boolean contentStale = true; protected boolean cacheContent = false; protected String username = null; protected String password = null; /** * Initialize this portlet by defining a HTML rewriter. * @throws PortletException Initialization failed */ public void init() throws PortletException { if (initDone) // Why is init called more than once per portlet? return; PortletConfig config = this.getPortletConfig(); try { rewriter = new HTMLRewriter(); // fetch username and password for HTTP Basic Autentication username = config.getInitParameter("username"); password = config.getInitParameter("password"); contentStale = true; initDone = true; } catch (Exception e) { Log.info("Exception occurred:" + e.toString()); e.printStackTrace(); throw new PortletException( e.toString() ); } } /** * took this from FileServerPortlet as it was private * */ // FIXME: Currently only the expiration the HTTP Response header is honored. // Expiration information in <meta> tags are not honored protected Reader getReader(String url) throws IOException { URL pageUrl = new URL(url); URLConnection pageConn = pageUrl.openConnection(); try { // set HTTP Basic Authetication header if username and password are set if (username != null && password !=null) { pageConn.setRequestProperty("Authorization", "Basic " + Base64.encodeAsString(username + ":" + password)); } } catch (Exception e) { Log.info("Exception occurred:" + e.toString()); e.printStackTrace(); } long pageExpiration = pageConn.getExpiration(); String encoding = pageConn.getContentEncoding(); String tempString = null; String noCache = "no-cache"; if(encoding == null) { // Standard HTTP encoding encoding = "iso-8859-1"; } /* * Determing if content should be cached. */ cacheContent = true; // Assume content is cached if (pageExpiration == 0) { cacheContent = false; } // Check header field CacheControl tempString = pageConn.getHeaderField( "Cache-Control"); if (tempString != null) { if (tempString.toLowerCase().indexOf(noCache) >= 0) { cacheContent = false; } } // Check header field Pragma tempString = pageConn.getHeaderField( "Pragma"); if (tempString != null) { if (tempString.toLowerCase().indexOf(noCache) >= 0) { cacheContent = false; } } // Assign a reader Reader rdr = new InputStreamReader(pageConn.getInputStream(), encoding ); // Only set the page expiration it the page has not expired if (pageExpiration > System.currentTimeMillis() && (cacheContent == true)) { contentStale = false; Log.debug( "WebPagePortlet caching URL: " + url + " Expiration: " + pageExpiration + ", " + (pageExpiration - System.currentTimeMillis() ) + " milliseconds into the future" ); setExpirationMillis(pageExpiration); } else { contentStale = true; } return rdr; } /** This methods outputs the content of the portlet for a given request. @param data the RunData object for the request @return the content to be displayed to the user-agent */ public ConcreteElement getContent( RunData data ) { PortletConfig config = this.getPortletConfig(); if (contentStale == true) return getWebPageContent(data, config); if (null == getExpirationMillis()) return getContent( data, null, true); if (getExpirationMillis().longValue() <= System.currentTimeMillis()) return getWebPageContent(data, config); return getContent( data, null , true ); } private ConcreteElement getWebPageContent( RunData data, PortletConfig config ) { String convertedString = null; // parsed and re-written HTML JetspeedClearElement element = null; String url = selectUrl( data, config ); try { Reader htmlReader = getReader( url ); convertedString = rewriter.rewrite(htmlReader, url); element = new JetspeedClearElement(convertedString); //FIXME: We should do a clearContent() for the media type, not ALL media types this.clearContent(); // doing this because setContent() is not overwriting current content. this.setContent(element); htmlReader.close(); } catch (Exception e) { Log.info("Exception occurred:" + e.toString()); e.printStackTrace(); } return element; } /** * Usually called by caching system when portlet is marked as expired, but * has not be idle longer then TimeToLive. * * Any cached content that is expired need to be refreshed. */ public void refresh() { if (cacheContent == true) { getWebPageContent(null, this.getPortletConfig()); } } /** * Select the URL to use for this portlet. * @return The URL to use for this portlet */ protected String selectUrl( RunData data, PortletConfig config ) { String url = config.getURL(); return url; } // selectUrl } 1.1 jakarta-jetspeed/src/java/org/apache/jetspeed/util/Streams.java Index: Streams.java =================================================================== /* ==================================================================== * BBC Syndication Server 1.0 * * Copyright (c) 2002-2003 The BBC. All rights reserved. * */ package org.apache.jetspeed.util; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; /** * <P>Utility functions related to Streams</P> * * @author <a href="mailto:taylor@;apache.org">David Sean Taylor</a> * @version $Id: Streams.java,v 1.1 2002/10/29 18:38:06 taylor Exp $ */ public class Streams { static final int BLOCK_SIZE=4096; public static void drain(InputStream r,OutputStream w) throws IOException { byte[] bytes=new byte[BLOCK_SIZE]; try { int length=r.read(bytes); while(length!=-1) { if(length!=0) { w.write(bytes,0,length); } length=r.read(bytes); } } finally { bytes=null; } } public static void drain(Reader r,Writer w) throws IOException { char[] bytes=new char[BLOCK_SIZE]; try { int length=r.read(bytes); while(length!=-1) { if(length!=0) { w.write(bytes,0,length); } length=r.read(bytes); } } finally { bytes=null; } } public static void drain(Reader r,OutputStream os) throws IOException { Writer w=new OutputStreamWriter(os); drain(r,w); w.flush(); } public static byte[] drain(InputStream r) throws IOException { ByteArrayOutputStream bytes=new ByteArrayOutputStream(); drain(r,bytes); return bytes.toByteArray(); } public static String getAsString(InputStream is) { int c=0; char lineBuffer[]=new char[128], buf[]=lineBuffer; int room= buf.length, offset=0; try { loop: while (true) { // read chars into a buffer which grows as needed switch (c = is.read() ) { case -1: break loop; default: if (--room < 0) { buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); lineBuffer = buf; } buf[offset++] = (char) c; break; } } } catch(IOException ioe) { ioe.printStackTrace(); } if ((c == -1) && (offset == 0)) { return null; } return String.copyValueOf(buf, 0, offset); } public static String getAsString(Reader is) { int c=0; char lineBuffer[]=new char[128], buf[]=lineBuffer; int room= buf.length, offset=0; try { loop: while (true) { // read chars into a buffer which grows as needed switch (c = is.read() ) { case -1: break loop; default: if (--room < 0) { buf = new char[offset + 128]; room = buf.length - offset - 1; System.arraycopy(lineBuffer, 0, buf, 0, offset); lineBuffer = buf; } buf[offset++] = (char) c; break; } } } catch(IOException ioe) { ioe.printStackTrace(); } if ((c == -1) && (offset == 0)) { return null; } return String.copyValueOf(buf, 0, offset); } } 1.2 +1 -1 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/FrameRewriterTest.java Index: FrameRewriterTest.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/FrameRewriterTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- FrameRewriterTest.java 26 Apr 2002 17:05:08 -0000 1.1 +++ FrameRewriterTest.java 29 Oct 2002 18:38:06 -0000 1.2 @@ -121,7 +121,7 @@ reader = new StringReader(HTML); HTMLRewriter rewriter = new HTMLRewriter(); - String result = rewriter.rewrite(reader); + String result = rewriter.rewrite(reader, null); System.out.println(result); reader.close(); reader = null; 1.2 +3 -17 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/HTMLParserAdaptor.java Index: HTMLParserAdaptor.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/HTMLParserAdaptor.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HTMLParserAdaptor.java 27 Mar 2002 17:43:58 -0000 1.1 +++ HTMLParserAdaptor.java 29 Oct 2002 18:38:06 -0000 1.2 @@ -70,29 +70,15 @@ /* * Parses and rewrites a HTML document, rewriting all URLs as either fully proxied * URLs or as web-application full URLs, not relative. - * Given a relative URL, such a "/content/images/my.gif" it can be rewritten as either - * a proxied URL, for example: * - * "http://www.webserver.com?js_path=/content/images/my.gif" - * - * - * or a full path to the URL on the web server: - * - * "http://www.webserver.com/content/images/my.gif" - * - * - * @param html The html content to be converted. - * @param proxyRoot The root URL of the Proxy Server. - * @param baseURL The Base URL of the host being proxied. + * @param reader to the source to be read * @throws MalformedURLException If the baseUrl is not a valid URL or if an URL inside * the document could not be converted. * @return An HTML-String with rewritten URLs. */ - String run(Reader html, - String proxyRoot, - String baseUrl) + String run(Reader reader) throws MalformedURLException; } 1.3 +21 -171 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/HTMLRewriter.java Index: HTMLRewriter.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/HTMLRewriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- HTMLRewriter.java 26 Apr 2002 17:05:08 -0000 1.2 +++ HTMLRewriter.java 29 Oct 2002 18:38:06 -0000 1.3 @@ -87,80 +87,37 @@ { } - /* - * Entry point into rewriting HTML content. - * - * Reads stream from proxied host, runs configured HTML parser against that stream, - * rewriting relevant links, and writes the parsed stream back to the client. - * - * @param input the HTML input stream. - * @param proxyRoot the base URL of the proxy server. - * @param baseURL the base URL of the target host. - * @return the rewritten HTML output stream. - * - * @exception MalformedURLException a servlet exception. - */ - public String rewrite(Reader input, - String proxyRoot, - String baseURL) - throws MalformedURLException - { - String rewrittenHTML = ""; - - this.proxyRoot = proxyRoot; - this.baseURL = baseURL; - HTMLParserAdaptor parser = new SwingParserAdaptor(this); - rewrittenHTML = parser.run(input, proxyRoot, baseURL); - - return rewrittenHTML; - } - - private String proxyRoot; - private String baseURL; - - public final static String DEFAULT_PROXY_PARAM = "js_path"; + protected String baseURL = null; /* * This callback is called by the HTMLParserAdaptor implementation to write * back all rewritten URLs to point to the proxy server. * Given the targetURL, rewrites the link as a link back to the proxy server. * - * @param targetURL the URL to be rewritten back to the proxy server. - * @param baseURL the base URL of the target host. - * @param proxyURL the base URL of the proxy server. * @return the rewritten URL to the proxy server. * - * @exception MalformedURLException a servlet exception. */ public String generateNewUrl( String targetURL, HTML.Tag tag, HTML.Attribute attribute) - // String targetURL, - // String baseURL, - // String proxyURL, - // boolean proxied) { - try { - - URL full = new URL(new URL(proxyRoot), targetURL); - String fullPath = full.toString(); - - // some attributes shouldn't be proxied, like images, stylesheetrefs - - StringBuffer buffer = new StringBuffer(proxyRoot.toString()); - buffer.append("?"); - buffer.append(DEFAULT_PROXY_PARAM); - buffer.append("="); - buffer.append(URLEncoder.encode(fullPath)); - String proxiedPath = buffer.toString().replace('&', '@'); - return proxiedPath; + String fullPath = ""; + try + { - } - catch (Throwable t) + if (baseURL != null) + { + URL full = new URL(new URL(baseURL), targetURL); + fullPath = full.toString(); + } + else + { + return targetURL; // leave as is + } + } + catch (Exception e) { - //FIXME: transient print to debug... - System.err.println( "HTMLRewriter: BASE=" + proxyRoot); - System.err.println( "target=" + targetURL); - return URLEncoder.encode(targetURL); + System.err.println(e); } + return fullPath; } @@ -175,124 +132,17 @@ return true; //false; } - public String rewrite(Reader input) + public String rewrite(Reader input, String baseURL) throws MalformedURLException { String rewrittenHTML = ""; + this.baseURL = baseURL; HTMLParserAdaptor parser = new SwingParserAdaptor(this); - rewrittenHTML = parser.run(input, this.proxyRoot, this.baseURL); + rewrittenHTML = parser.run(input); return rewrittenHTML; } - - - public static byte[] rewriteScript(String script, - String url, - String proxyHost, - String base) - throws IOException - { - int baseLength = base.length(); - - int totalScriptLength = script.length(); - CharArrayWriter writer = new CharArrayWriter(totalScriptLength + 100); - char chars[] = script.toCharArray(); - boolean translating = false; - - // now rewrite the script stream - for (int ix=0; ix < chars.length; ix++) - { - if (chars[ix] == '"') - { - //int endpos= ix + len + 1; - if (translating) - translating = false; - else if (false == translating ) //&& - // endpos < totalScriptLength) - { - translating = true; - writer.write(chars[ix]); - - if (!findImage(chars, ix + 1)) - continue; - - String trans = translate(proxyHost, base); - writer.write(trans); - if (chars[ix+1] != PATH_SEPARATOR && base.charAt(baseLength - 1) != PATH_SEPARATOR) - writer.write(PATH_SEPARATOR); - if (chars[ix+1] == PATH_SEPARATOR && base.charAt(baseLength - 1) == PATH_SEPARATOR) - ix++; - - continue; - } - } - if (translating && chars[ix] == '&') - writer.write('@'); - else - writer.write(chars[ix]); - } - - return writer.toString().getBytes(); - } - - - public static String translate(String proxyURL, String targetURL) - { - StringBuffer buffer = new StringBuffer(proxyURL); - buffer.append("?"); - buffer.append(DEFAULT_PROXY_PARAM); - buffer.append("="); - buffer.append(targetURL.replace('&', '@')); - String proxiedPath = buffer.toString(); - - return proxiedPath; - } - - /* - * Finds an image hyperlink in a quoted string. - * The image hyperlink is found by searching through the script text, searching - * for references ending in typical image extensions (GIF, PNG, JPG). - * - * NOTE: this function is just the start of script-parsing. - * A much more robust implementation will be necessary. - * - * @param chars The character array to search. - * @param ix The starting index to search from in the character array. - * @return If the image string is found, returns true otherwise false. - * - */ - protected static boolean findImage(char[] chars, int ix) - { - for (int iy=ix; iy < chars.length ; iy++) - { - if (chars[iy] == '"') - return false; - if (chars[iy] == '.') - { - int iw = 0; - for (int iz = iy+1; iz < chars.length && iw < 3; iz++, iw++ ) - { - if (chars[iz] == GIF[iw] || chars[iz] == PNG[iw] || chars[iz] == JPG[iw]) - { - continue; - } - else - return false; - } - if (iw == 3) - return true; - - return false; - } - } - return false; - } - - private static final char[] GIF = {'g', 'i', 'f'}; - private static final char[] PNG = {'p', 'n', 'g'}; - private static final char[] JPG = {'j', 'p', 'g'}; - protected static final char PATH_SEPARATOR = '/'; /* * Simple Tag Events 1.3 +5 -5 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/Rewriter.java Index: Rewriter.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/Rewriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Rewriter.java 26 Apr 2002 17:05:08 -0000 1.2 +++ Rewriter.java 29 Oct 2002 18:38:06 -0000 1.3 @@ -84,14 +84,14 @@ * rewriting relevant links, and writes the parsed stream back to the client. * * @param input the HTML input stream. - * @param proxyRoot the base URL of the proxy server. - * @param baseURL the base URL of the target host. + * @param input the base URL host string used to create full links back to host * @return the rewritten HTML output stream. * * @exception MalformedURLException a servlet exception. */ - String rewrite(Reader input, String proxyRoot, String baseURL) - throws MalformedURLException; + String rewrite(Reader input, String baseURL) + throws MalformedURLException; + /* <p> * This callback is called by the HTMLParserAdaptor implementation to write 1.3 +4 -4 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/SampleRewriter.java Index: SampleRewriter.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/SampleRewriter.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SampleRewriter.java 26 Apr 2002 17:12:46 -0000 1.2 +++ SampleRewriter.java 29 Oct 2002 18:38:06 -0000 1.3 @@ -126,20 +126,20 @@ * rewriting relevant links, and writes the parsed stream back to the client. * * @param input the HTML input stream. - * @param proxyRoot the base URL of the proxy server. * @param baseURL the base URL of the target host. * @return the rewritten HTML output stream. * * @exception MalformedURLException a servlet exception. */ - public String rewrite(Reader input) + public String rewrite(Reader input, String baseURL) throws MalformedURLException { String rewrittenHTML = ""; + this.basePortalURL = baseURL; HTMLParserAdaptor parser = new SwingParserAdaptor(this); - rewrittenHTML = parser.run(input, fullPortalURL, sampleURL); + rewrittenHTML = parser.run(input); return rewrittenHTML; } 1.3 +18 -38 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/SwingParserAdaptor.java Index: SwingParserAdaptor.java =================================================================== RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/SwingParserAdaptor.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SwingParserAdaptor.java 26 Apr 2002 17:05:08 -0000 1.2 +++ SwingParserAdaptor.java 29 Oct 2002 18:38:06 -0000 1.3 @@ -96,30 +96,16 @@ } /* - * Parses and rewrites a HTML document, rewriting all URLs as either fully proxied - * URLs or as web-application full URLs, not relative. - * Given a relative URL, such a "/content/images/my.gif" it can be rewritten as either - * a proxied URL, for example: + * Parses and an HTML document, rewriting all URLs as determined by the Rewriter callback * - * "http://proxyserver/proxy?pxpath=/content/images/my.gif" - * - * or a full path to the URL on the web server: * - * "http://www.webserver.com/content/images/my.gif" + * @param reader The input stream reader * - * In all cases in MMS, the proxy is rewritten as in the first example, proxied. + * @throws MalformedURLException * - * @param html The html content to be converted. - * @param proxyRoot The root URL of the Proxy Server. - * @param baseURL The Base URL of the host being proxied. - - * @throws MalformedURLException If the baseUrl is not a valid URL or if an URL inside - * the document could not be converted. * @return An HTML-String with rewritten URLs. */ - public String run(Reader reader, - String proxyRoot, - String baseUrl) + public String run(Reader reader) throws MalformedURLException { HTMLEditorKit.Parser parser = new SwingParserAdaptor.ParserGetter().getParser(); @@ -127,10 +113,6 @@ String res =""; try { -// cb.baseURL = new URL(baseUrl); -// cb.proxyURL = new URL(proxyRoot); - cb.baseURL = baseUrl; - cb.proxyURL = proxyRoot; parser.parse(reader, cb, true); res = cb.getResult(); } catch (Exception e) @@ -163,11 +145,6 @@ class Callback extends HTMLEditorKit.ParserCallback { - // the base-url of which the given html comes from. -// private URL baseURL; -// private URL proxyURL; - private String baseURL; - private String proxyURL; // either handling of <FORM> is buggy, or I made some weird mistake ... // ... JDK 1.3 sends double "</form>"-tags on closing <form> @@ -193,10 +170,10 @@ */ public void handleText(char[] values,int param) { - if (false == emit) - return; - if (values[0] == '>') - return; + if (false == emit) + return; + if (values[0] == '>') + return; addToResult(values); } @@ -262,14 +239,14 @@ boolean isValidFragmentTag(HTML.Tag tag) - { + { + /* if (false == emit) return false; if (tag == HTML.Tag.HTML) // always strip out HTML tag for fragments return false; - if (tag == HTML.Tag.BODY) return false; @@ -281,7 +258,7 @@ if (tag == HTML.Tag.NOFRAMES) return false; - + */ return true; } @@ -313,8 +290,10 @@ String appended = rewriter.exitEndTagEvent(tag); if (null != appended) result.write(appended); + } + /* * Hot Java event callback for handling errors. * @@ -575,8 +554,9 @@ // always post attrs.addAttribute(HTML.Attribute.METHOD, "POST"); //self referencing <FORM> - attrs.addAttribute(HTML.Attribute.ACTION, - baseURL); + + // attrs.addAttribute(HTML.Attribute.ACTION, + // baseURL); } else { @@ -706,7 +686,7 @@ boolean proxied) { String oldURL = attrs.getAttribute( attr ).toString(); - + // System.out.println("Generating new url: " + oldURL); return rewriter.generateNewUrl(oldURL, tag, attr); } 1.1 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/ProxyRewriter.java Index: ProxyRewriter.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Jetspeed" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" or * "Apache Jetspeed", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.jetspeed.util.rewriter; // java.io import java.io.IOException; import java.io.CharArrayWriter; import java.io.Reader; // java.net import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; // this makes it dependent on Swing...need an abstraction WTP import javax.swing.text.html.HTML; import javax.swing.text.MutableAttributeSet; /** * * Proxy Rewriter for rewriting HTML content with links back to a proxy host. * URLs or as web-application full URLs, not relative. * Given a relative URL, such a "/content/images/my.gif" it can be rewritten as either * a proxied URL, for example: * * "http://proxyserver/proxy?pxpath=/content/images/my.gif" * * or a full path to the URL on the web server: * * "http://www.webserver.com/content/images/my.gif" * * @author <a href="mailto:taylor@;apache.org">David Sean Taylor</a> * @version $Id: ProxyRewriter.java,v 1.1 2002/10/29 18:38:06 taylor Exp $ * */ public class ProxyRewriter implements Rewriter { /* * Construct a basic HTML Rewriter * */ public ProxyRewriter() { } /* * Entry point into rewriting HTML content. * * Reads stream from proxied host, runs configured HTML parser against that stream, * rewriting relevant links, and writes the parsed stream back to the client. * * @param input the HTML input stream. * @param proxyRoot the base URL of the proxy server. * @param baseURL the base URL of the target host. * @return the rewritten HTML output stream. * * @exception MalformedURLException a servlet exception. */ public String rewrite(Reader input, String proxyRoot, String baseURL) throws MalformedURLException { String rewrittenHTML = ""; this.proxyRoot = proxyRoot; this.baseURL = baseURL; HTMLParserAdaptor parser = new SwingParserAdaptor(this); rewrittenHTML = parser.run(input); return rewrittenHTML; } private String proxyRoot; private String baseURL; public final static String DEFAULT_PROXY_PARAM = "js_path"; /* * This callback is called by the HTMLParserAdaptor implementation to write * back all rewritten URLs to point to the proxy server. * Given the targetURL, rewrites the link as a link back to the proxy server. * * @param targetURL the URL to be rewritten back to the proxy server. * @param baseURL the base URL of the target host. * @param proxyURL the base URL of the proxy server. * @return the rewritten URL to the proxy server. * * @exception MalformedURLException a servlet exception. */ public String generateNewUrl( String targetURL, HTML.Tag tag, HTML.Attribute attribute) // String targetURL, // String baseURL, // String proxyURL, // boolean proxied) { try { URL full = new URL(new URL(proxyRoot), targetURL); String fullPath = full.toString(); StringBuffer buffer = new StringBuffer(proxyRoot.toString()); buffer.append("?"); buffer.append(DEFAULT_PROXY_PARAM); buffer.append("="); buffer.append(URLEncoder.encode(fullPath)); String proxiedPath = buffer.toString().replace('&', '@'); return proxiedPath; } catch (Throwable t) { //FIXME: transient print to debug... System.err.println( "HTMLRewriter: BASE=" + proxyRoot); System.err.println( "target=" + targetURL); return URLEncoder.encode(targetURL); } } /* * Returns true if all rewritten URLs should be sent back to the proxy server. * * @return true if all URLs are rewritten back to proxy server. */ public boolean proxyAllTags() { return true; //false; } public String rewrite(Reader input, String baseURL) throws MalformedURLException { String rewrittenHTML = ""; this.baseURL = baseURL; HTMLParserAdaptor parser = new SwingParserAdaptor(this); rewrittenHTML = parser.run(input); return rewrittenHTML; } public static byte[] rewriteScript(String script, String url, String proxyHost, String base) throws IOException { int baseLength = base.length(); int totalScriptLength = script.length(); CharArrayWriter writer = new CharArrayWriter(totalScriptLength + 100); char chars[] = script.toCharArray(); boolean translating = false; // now rewrite the script stream for (int ix=0; ix < chars.length; ix++) { if (chars[ix] == '"') { //int endpos= ix + len + 1; if (translating) translating = false; else if (false == translating ) //&& // endpos < totalScriptLength) { translating = true; writer.write(chars[ix]); if (!findImage(chars, ix + 1)) continue; String trans = translate(proxyHost, base); writer.write(trans); if (chars[ix+1] != PATH_SEPARATOR && base.charAt(baseLength - 1) != PATH_SEPARATOR) writer.write(PATH_SEPARATOR); if (chars[ix+1] == PATH_SEPARATOR && base.charAt(baseLength - 1) == PATH_SEPARATOR) ix++; continue; } } if (translating && chars[ix] == '&') writer.write('@'); else writer.write(chars[ix]); } return writer.toString().getBytes(); } public static String translate(String proxyURL, String targetURL) { StringBuffer buffer = new StringBuffer(proxyURL); buffer.append("?"); buffer.append(DEFAULT_PROXY_PARAM); buffer.append("="); buffer.append(targetURL.replace('&', '@')); String proxiedPath = buffer.toString(); return proxiedPath; } /* * Finds an image hyperlink in a quoted string. * The image hyperlink is found by searching through the script text, searching * for references ending in typical image extensions (GIF, PNG, JPG). * * NOTE: this function is just the start of script-parsing. * A much more robust implementation will be necessary. * * @param chars The character array to search. * @param ix The starting index to search from in the character array. * @return If the image string is found, returns true otherwise false. * */ protected static boolean findImage(char[] chars, int ix) { for (int iy=ix; iy < chars.length ; iy++) { if (chars[iy] == '"') return false; if (chars[iy] == '.') { int iw = 0; for (int iz = iy+1; iz < chars.length && iw < 3; iz++, iw++ ) { if (chars[iz] == GIF[iw] || chars[iz] == PNG[iw] || chars[iz] == JPG[iw]) { continue; } else return false; } if (iw == 3) return true; return false; } } return false; } private static final char[] GIF = {'g', 'i', 'f'}; private static final char[] PNG = {'p', 'n', 'g'}; private static final char[] JPG = {'j', 'p', 'g'}; protected static final char PATH_SEPARATOR = '/'; /* * Simple Tag Events */ public boolean enterSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs) { return true; } public String exitSimpleTagEvent(HTML.Tag tag, MutableAttributeSet attrs) { return null; } /* * Start Tag Events */ public boolean enterStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs) { return true; } public String exitStartTagEvent(HTML.Tag tag, MutableAttributeSet attrs) { return null; } /* * Exit Tag Events */ public boolean enterEndTagEvent(HTML.Tag tag) { return true; } public String exitEndTagEvent(HTML.Tag tag) { return null; } /* * Convert Tag Events */ public void convertTagEvent(HTML.Tag tag, MutableAttributeSet attrs) { } } 1.1 jakarta-jetspeed/src/java/org/apache/jetspeed/util/rewriter/TestRewriter.java Index: TestRewriter.java =================================================================== /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Jetspeed" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" or * "Apache Jetspeed", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.jetspeed.util.rewriter; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Assert; import java.io.Reader; import java.io.StringReader; import java.io.FileReader; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import org.apache.jetspeed.util.Streams; public class TestRewriter // extends TestCase { /* static public Test suite() { TestSuite suite = new TestSuite(TestRewriter.class); return(suite); } */ public TestRewriter(String name) { //super(name); } /////////////////////////////////////////////////////////////////////////// protected void setUp() { } protected void tearDown() { } /////////////////////////////////////////////////////////////////////////// public void testPage() { Reader reader = null; try { String baseURL = "http://java.sun.com/"; String fullURL = "http://java.sun.com/"; //URL pageUrl = new URL("http://www.devx.com/Java/Article/9747/"); URL pageUrl = new URL(fullURL); URLConnection pageConn = pageUrl.openConnection(); String encoding = pageConn.getContentEncoding(); if(encoding == null) { // Standard HTTP encoding encoding = "iso-8859-1"; } reader = new InputStreamReader(pageConn.getInputStream(), encoding ); //String crap = Streams.getAsString(reader); //System.out.println("crap = " + crap); HTMLRewriter rewriter = new HTMLRewriter(); // String result = rewriter.rewrite(reader, "XXXX", "YYYY"); String result = rewriter.rewrite(reader, baseURL); System.out.println(result); reader.close(); reader = null; } catch (Exception e) { System.err.println("Exception occurred:" + e.toString()); e.printStackTrace(); } finally { try { if (null != reader) reader.close(); } catch (Exception e) {} } } /////////////////////////////////////////////////////////////////////////// static public void main(String[] argv) { // String[] testCaseName = {TestRewriter.class.getName()}; // junit.swingui.TestRunner.main(testCaseName); TestRewriter trw = new TestRewriter("test"); trw.testPage(); } }
-- To unsubscribe, e-mail: <mailto:jetspeed-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:jetspeed-dev-help@;jakarta.apache.org>