You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by rw...@apache.org on 2006/04/01 01:16:23 UTC
svn commit: r390539 - in /portals/jetspeed-2/trunk: ./ applications/demo/
components/rewriter/
components/rewriter/src/java/org/apache/jetspeed/rewriter/
components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/
components/rewriter/src/test/...
Author: rwatler
Date: Fri Mar 31 15:16:16 2006
New Revision: 390539
URL: http://svn.apache.org/viewcvs?rev=390539&view=rev
Log:
Rewriter Component Upgrade:
- use Neko HTML parser
- upgrade commons-httpclient to 3.0
- support HTTP basic authentication via SSOWebContentPortlet, (sso.type=http)
- rewriting bug fixes
- added cookie based session management support
Contributed by David Young
Added:
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java (with props)
portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java (with props)
portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html (with props)
Modified:
portals/jetspeed-2/trunk/applications/demo/project.xml
portals/jetspeed-2/trunk/components/rewriter/pom.xml
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/BasicRewriter.java
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/RulesetRewriterImpl.java
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/WebContentRewriter.java
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/CallbackElementRemover.java
portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/XMLAttributesWrapper.java
portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestRewriterController.java
portals/jetspeed-2/trunk/components/search/src/java/org/apache/jetspeed/search/handlers/URLToDocHandler.java
portals/jetspeed-2/trunk/components/sso/project.xml
portals/jetspeed-2/trunk/components/sso/src/java/org/apache/jetspeed/sso/impl/PersistenceBrokerSSOProvider.java
portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/SSOWebContentPortlet.java
portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java
portals/jetspeed-2/trunk/pom.xml
Modified: portals/jetspeed-2/trunk/applications/demo/project.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/applications/demo/project.xml?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/applications/demo/project.xml (original)
+++ portals/jetspeed-2/trunk/applications/demo/project.xml Fri Mar 31 15:16:16 2006
@@ -43,7 +43,7 @@
</dependency>
<dependency>
<id>commons-httpclient</id>
- <version>2.0</version>
+ <version>3.0</version>
<type>jar</type>
<properties>
<war.bundle>true</war.bundle>
Modified: portals/jetspeed-2/trunk/components/rewriter/pom.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/pom.xml?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/pom.xml (original)
+++ portals/jetspeed-2/trunk/components/rewriter/pom.xml Fri Mar 31 15:16:16 2006
@@ -45,6 +45,14 @@
<!-- Build Dependencies -->
<dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
+ <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/BasicRewriter.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/BasicRewriter.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/BasicRewriter.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/BasicRewriter.java Fri Mar 31 15:16:16 2006
@@ -34,15 +34,12 @@
* @return the rewritten URL to the proxy server.
*
*/
- public String rewriteUrl(
- String url,
- String tag,
- String attribute)
+ public String rewriteUrl(String url, String tag, String attribute)
{
String fullPath = "";
try
{
- String baseUrl = super.getBaseUrl();
+ String baseUrl = getBaseUrl();
if (baseUrl != null)
{
URL full = new URL(new URL(baseUrl), url);
Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/RulesetRewriterImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/RulesetRewriterImpl.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/RulesetRewriterImpl.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/RulesetRewriterImpl.java Fri Mar 31 15:16:16 2006
@@ -18,6 +18,10 @@
import java.net.URL;
import java.util.Iterator;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor;
import org.apache.jetspeed.rewriter.rules.Attribute;
import org.apache.jetspeed.rewriter.rules.Rule;
import org.apache.jetspeed.rewriter.rules.Ruleset;
@@ -32,6 +36,8 @@
*/
public class RulesetRewriterImpl extends BasicRewriter implements RulesetRewriter
{
+ protected final static Log log = LogFactory.getLog(RulesetRewriterImpl.class);
+
private Ruleset ruleset = null;
private boolean removeComments = false;
@@ -102,17 +108,15 @@
*/
public void enterConvertTagEvent(String tagid, MutableAttributes attributes)
{
- // System.out.println("tagid = " + tagid);
-
if (null == ruleset)
{
return;
}
- Tag tag = ruleset.getTag(tagid.toUpperCase());
+ Tag tag = ruleset.getTag(tagid.toUpperCase());
if (null == tag)
{
- return;
+ return;
}
Iterator attribRules = tag.getAttributes().iterator();
@@ -121,11 +125,7 @@
Attribute attribute = (Attribute)attribRules.next();
String name = attribute.getId();
String value = (String)attributes.getValue(name);
- //String id = (String)attributes.getValue("name");
- //System.out.println("id = " + id);
- //System.out.println("value = " + value);
- //System.out.println("name = " + name);
-
+
if (value != null) // && name.equalsIgnoreCase(attribute.getId()))
{
Rule rule = attribute.getRule();
@@ -139,7 +139,7 @@
continue;
}
- String rewritten = rewriteUrl(value, tag.getId(), name);
+ String rewritten = rewriteUrl(value, tag.getId(), name, attributes);
if (null != rewritten) // return null indicates "don't rewrite"
{
if (rule.getSuffix() != null)
@@ -167,15 +167,12 @@
* @return the rewritten URL to the proxy server.
*
*/
- public String rewriteUrl(
- String url,
- String tag,
- String attribute)
+ public String rewriteUrl(String url, String tag, String attribute, MutableAttributes otherAttribues )
{
String fullPath = "";
try
{
- String baseUrl = super.getBaseUrl();
+ String baseUrl = getBaseUrl();
if (baseUrl != null)
{
URL full = new URL(new URL(baseUrl), url);
@@ -188,7 +185,7 @@
}
catch (Exception e)
{
- System.err.println(e);
+ log.error("failure to rewriteUrl: "+url,e) ;
}
return fullPath;
}
Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/WebContentRewriter.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/WebContentRewriter.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/WebContentRewriter.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/WebContentRewriter.java Fri Mar 31 15:16:16 2006
@@ -26,9 +26,9 @@
*/
public class WebContentRewriter extends RulesetRewriterImpl implements Rewriter
{
-
- /** WebContentURL */
- public static final String ACTION_PARAMETER_URL = "WCURL";
+ /** parameters that need to be propagated in the action URL (since HTTP request parameters will not be available) */
+ public static final String ACTION_PARAMETER_URL = "_AP_URL";
+ public static final String ACTION_PARAMETER_METHOD = "_AP_METHOD";
/*
* Portlet URL will be used to replace all URL's
@@ -54,11 +54,12 @@
* @param url
* @param tag
* @param attribute
+ * @param otherAttributes
* @return the modified url which is a portlet action
*
* Rewrites all urls HREFS with a portlet action
*/
- public String rewriteUrl(String url, String tag, String attribute)
+ public String rewriteUrl(String url, String tag, String attribute, MutableAttributes otherAttributes)
{
String modifiedURL = url;
@@ -69,7 +70,7 @@
{
if (this.getBaseUrl() != null)
{
- URL full = new URL(new URL(this.getBaseUrl()), url);
+ URL full = new URL(new URL(getBaseUrl()), url);
modifiedURL = full.toString();
}
else
@@ -83,48 +84,31 @@
}
}
- // Only add PortletActions to URL's which are anchors (tag=a) and HREF's (attribute= HREF) -- ignore all others links
- if ( tag.compareToIgnoreCase("A") == 0 && attribute.compareToIgnoreCase("HREF") == 0)
+ // translate "submit" URL's as actions
+ // <A href="..."/>
+ // <FORM submit="..."/>
+ if (( tag.equalsIgnoreCase("A") && attribute.equalsIgnoreCase("href")) ||
+ ( tag.equalsIgnoreCase("FORM") && attribute.equalsIgnoreCase("action")))
+
{
// Regular URL just add a portlet action
if (this.actionURL != null)
{
// create Action URL
actionURL.setParameter(ACTION_PARAMETER_URL, modifiedURL);
+ if (tag.equalsIgnoreCase("FORM"))
+ {
+ String httpMethod = otherAttributes.getValue("method");
+ if (httpMethod != null)
+ actionURL.setParameter(ACTION_PARAMETER_METHOD, httpMethod);
+ }
modifiedURL = actionURL.toString();
}
}
+ // if ( !url.equalsIgnoreCase( modifiedURL ))
+ // System.out.println("In tag: "+tag+", for attribute: "+attribute+", converted url: "+url+", to: "+modifiedURL+", base URL was: "+getBaseUrl());
+
return modifiedURL;
}
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.jetspeed.rewriter.Rewriter#shouldRemoveTag(java.lang.String)
- */
- /*
- * public boolean shouldRemoveTag(String tag) { if
- * (tag.equalsIgnoreCase("html")) { return true; } return false; }
- */
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.jetspeed.rewriter.Rewriter#shouldStripTag(java.lang.String)
- */
- /*
- * public boolean shouldStripTag(String tag) { if
- * (tag.equalsIgnoreCase("head")) { return true; } return false; }
- */
-
- /*
- * (non-Javadoc)
- *
- * @see org.apache.jetspeed.rewriter.Rewriter#shouldRemoveComments()
- */
- /*
- * public boolean shouldRemoveComments() { return true; }
- */
-
-}
\ No newline at end of file
+}
Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/CallbackElementRemover.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/CallbackElementRemover.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/CallbackElementRemover.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/CallbackElementRemover.java Fri Mar 31 15:16:16 2006
@@ -15,10 +15,14 @@
*/
package org.apache.jetspeed.rewriter.html.neko;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.jetspeed.rewriter.MutableAttributes;
import org.apache.jetspeed.rewriter.Rewriter;
+import org.apache.jetspeed.rewriter.html.SwingAttributes;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
+import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.cyberneko.html.filters.ElementRemover;
@@ -41,52 +45,52 @@
private Rewriter rewriter;
/**
- *
+ * Construct with reference to the rewriter context to consult for rewriting advice
*/
public CallbackElementRemover( Rewriter rewriter )
{
super();
+
+ this.rewriter = rewriter;
}
-
+
+
+ // Base Class Protocol
+
/**
* <p>
- * emptyElement
+ * comment
* </p>
*
- * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName,
- * org.apache.xerces.xni.XMLAttributes,
- * org.apache.xerces.xni.Augmentations)
- * @param element
- * @param arg1
- * @param arg2
+ * @see org.apache.xerces.xni.XMLDocumentHandler#comment(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)
+ * @param text
+ * @param augs
* @throws org.apache.xerces.xni.XNIException
*/
- public void emptyElement( QName element, XMLAttributes arg1, Augmentations arg2 ) throws XNIException
+ public void comment(XMLString text,Augmentations augs) throws XNIException
{
- processTag(element.rawname);
- super.emptyElement(element, arg1, arg2);
+ if (rewriter.shouldRemoveComments())
+ return;
+ super.comment(text,augs);
}
/**
* <p>
- * processTag
+ * emptyElement
* </p>
*
- * @param tag
+ * @see org.apache.xerces.xni.XMLDocumentHandler#emptyElement(org.apache.xerces.xni.QName,
+ * org.apache.xerces.xni.XMLAttributes,
+ * org.apache.xerces.xni.Augmentations)
+ * @param element
+ * @param arg1
+ * @param arg2
+ * @throws org.apache.xerces.xni.XNIException
*/
- protected void processTag( String tag )
+ public void emptyElement( QName element, XMLAttributes attrs, Augmentations arg2 ) throws XNIException
{
- if (!fAcceptedElements.contains(tag.toLowerCase()) && !fRemovedElements.contains(tag.toLowerCase()))
- {
- if (!rewriter.shouldRemoveTag(tag) && !rewriter.shouldStripTag(tag))
- {
- acceptElement(tag, null);
- }
- else if (rewriter.shouldStripTag(tag))
- {
- removeElement(tag);
- }
- }
+ processTag(element,attrs) ;
+ super.emptyElement(element, attrs, arg2);
}
/**
@@ -102,9 +106,61 @@
* @param arg2
* @throws org.apache.xerces.xni.XNIException
*/
- public void startElement( QName element, XMLAttributes arg1, Augmentations arg2 ) throws XNIException
+ public void startElement( QName element, XMLAttributes attrs, Augmentations arg2 ) throws XNIException
{
- processTag(element.rawname);
- super.startElement(element, arg1, arg2);
+ processTag(element,attrs);
+ super.startElement(element, attrs, arg2);
+ }
+
+
+ // Support Methods
+
+ /**
+ * <p>
+ * processTag
+ * </p>
+ *
+ * @param tag
+ */
+ protected void processTag(QName element, XMLAttributes attrs)
+ {
+ String tag = element.rawname.toLowerCase();
+ if (fRemovedElements.contains(tag))
+ {
+ // alread removed
+ return ;
+ }
+ else if (rewriter.shouldStripTag(tag))
+ {
+ // first time for this tag...
+ // strip - remove tag and any text associated with it
+ removeElement(tag);
+ return ;
+ }
+ else if (rewriter.shouldRemoveTag(tag))
+ {
+ // BOZO - block intentially left EMPTY
+
+ // first time for this tag...
+ // remove - no directive necessary, the default behavior of ElementRemover is to drop tags that it does not know about (but the assocated text will remain)
+ return ;
+ }
+
+ // OTHERWISE - explicitly accept (keep tag and associated text)
+ // NOTE: even if fAcceptedElements contains the tag already, we need to reset the attribute names for this invocation context
+ rewriter.enterConvertTagEvent(tag,new XMLAttributesWrapper(attrs));
+ String[] attrNames = getAttributeNames(attrs);
+ acceptElement(tag,getAttributeNames(attrs));
+ }
+ protected String[] getAttributeNames(XMLAttributes attrs)
+ {
+ int length = attrs != null ? attrs.getLength() : 0 ;
+ String[] names = length > 0 ? new String[ length ] : null ;
+
+ for( int i = 0, limit = length;i<limit;i++)
+ {
+ names[i] = attrs.getQName(i) ;
+ }
+ return names ;
}
-}
\ No newline at end of file
+}
Added: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java?rev=390539&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java (added)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java Fri Mar 31 15:16:16 2006
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2000-2001,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.rewriter.html.neko;
+
+import java.io.Reader;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.xerces.xni.parser.XMLDocumentFilter;
+import org.apache.xerces.xni.parser.XMLInputSource;
+
+import org.apache.jetspeed.rewriter.ParserAdaptor;
+import org.apache.jetspeed.rewriter.Rewriter;
+import org.apache.jetspeed.rewriter.RewriterException;
+
+import org.xml.sax.SAXException ;
+
+import org.cyberneko.html.parsers.SAXParser;
+import org.cyberneko.html.filters.DefaultFilter;
+import org.cyberneko.html.filters.Purifier;
+import org.cyberneko.html.filters.Writer;
+
+
+/**
+ * <p>
+ * NekoParserAdapter
+ * </p>
+ * <p>
+ *
+ * </p>
+ * @author <a href="mailto:dyoung@phase2systems.com">David L Young</a>
+ * @version $Id: $
+ *
+ */
+public class NekoParserAdaptor implements ParserAdaptor
+{
+ protected final static Log log = LogFactory.getLog(NekoParserAdaptor.class);
+
+ /*
+ * Construct a cyberneko HTML parser adaptor
+ */
+ public NekoParserAdaptor()
+ {
+ super();
+ }
+
+ /**
+ * <p>
+ * parse
+ * </p>
+ *
+ * @see org.apache.jetspeed.rewriter.ParserAdaptor#parse(org.apache.jetspeed.rewriter.Rewriter, java.io.Reader)
+ * @param rewriter
+ * @param reader
+ * @throws RewriterException
+ */
+ public void parse(Rewriter rewriter, Reader reader)
+ throws RewriterException
+ {
+ // not sure what this means to parse without rewriting
+ rewrite(rewriter,reader,null);
+ }
+
+ /**
+ * <p>
+ * rewrite
+ * </p>
+ *
+ * @see org.apache.jetspeed.rewriter.ParserAdaptor#rewrite(org.apache.jetspeed.rewriter.Rewriter, java.io.Reader, java.io.Writer)
+ * @param rewriter
+ * @param reader
+ * @param writer
+ * @throws RewriterException
+ */
+ public void rewrite(Rewriter rewriter, java.io.Reader reader, java.io.Writer writer)
+ throws RewriterException
+ {
+ // use a cyberneko SAXParser
+ SAXParser parser = new SAXParser() ;
+
+ // setup filter chain
+ XMLDocumentFilter[] filters = {
+ new Purifier(), // [1] standard neko purifications (tag balancing, etc)
+ new CallbackElementRemover( rewriter ), // [2] accept / reject tags based on advice from rewriter
+ writer != null ? new org.cyberneko.html.filters.Writer( writer, null ) : new DefaultFilter() // [3] propagate results to specified writer (or do nothing -- Default -- when writer is null)
+ };
+
+ String filtersPropName = "http://cyberneko.org/html/properties/filters";
+
+ try
+ {
+ parser.setProperty(filtersPropName, filters);
+ }
+ catch (SAXException e)
+ {
+ // either no longer supported (SAXNotSupportedException), or no logner recognized (SAXNotRecognizedException)
+ log.error(filtersPropName + " is, unexpectedly, no longer defined for the cyberneko HTML parser",e);
+ throw new RewriterException("cyberneko parser version not supported",e);
+ }
+
+ try
+ {
+ // parse from reader
+ parser.parse(new XMLInputSource( null, null, null, reader, null )) ;
+ }
+ catch (IOException e)
+ {
+ String msg = "cyberneko HTML parsing failure";
+ log.error(msg,e);
+ throw new RewriterException(msg,e);
+ }
+
+ }
+
+}
Propchange: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/NekoParserAdaptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/XMLAttributesWrapper.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/XMLAttributesWrapper.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/XMLAttributesWrapper.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/html/neko/XMLAttributesWrapper.java Fri Mar 31 15:16:16 2006
@@ -56,7 +56,11 @@
*/
public int addAttribute( QName arg0, String arg1, String arg2 )
{
- return attrs.addAttribute(arg0, arg1, arg2);
+ int i = getIndex( arg0.rawname );
+ if ( i >= 0 )
+ attrs.removeAttributeAt( i );
+
+ return attrs.addAttribute( arg0, arg1, arg2 );
}
/**
* <p>
@@ -91,9 +95,9 @@
* @param arg0
* @return
*/
- public Augmentations getAugmentations( String arg0 )
+ public Augmentations getAugmentations( String qName )
{
- return attrs.getAugmentations(arg0);
+ return attrs.getAugmentations(asNekoAttributeName(qName)) ;
}
/**
* <p>
@@ -104,9 +108,9 @@
* @param arg1
* @return
*/
- public Augmentations getAugmentations( String arg0, String arg1 )
+ public Augmentations getAugmentations( String uri, String localPart )
{
- return attrs.getAugmentations(arg0, arg1);
+ return attrs.getAugmentations(uri,asNekoAttributeName(localPart));
}
/**
* <p>
@@ -116,9 +120,9 @@
* @param arg0
* @return
*/
- public int getIndex( String arg0 )
+ public int getIndex( String qName )
{
- return attrs.getIndex(arg0);
+ return attrs.getIndex(asNekoAttributeName(qName));
}
/**
* <p>
@@ -129,9 +133,9 @@
* @param arg1
* @return
*/
- public int getIndex( String arg0, String arg1 )
+ public int getIndex( String uri, String localName )
{
- return attrs.getIndex(arg0, arg1);
+ return attrs.getIndex(uri,asNekoAttributeName(localName));
}
/**
* <p>
@@ -224,9 +228,9 @@
* @param arg0
* @return
*/
- public String getType( String arg0 )
+ public String getType( String qName )
{
- return attrs.getType(arg0);
+ return attrs.getType(asNekoAttributeName(qName));
}
/**
* <p>
@@ -237,9 +241,9 @@
* @param arg1
* @return
*/
- public String getType( String arg0, String arg1 )
+ public String getType( String uri, String localName )
{
- return attrs.getType(arg0, arg1);
+ return attrs.getType(uri, asNekoAttributeName(localName));
}
/**
* <p>
@@ -273,9 +277,9 @@
* @param arg0
* @return
*/
- public String getValue( String arg0 )
+ public String getValue( String qName )
{
- return attrs.getValue(arg0);
+ return attrs.getValue(asNekoAttributeName(qName));
}
/**
* <p>
@@ -286,9 +290,9 @@
* @param arg1
* @return
*/
- public String getValue( String arg0, String arg1 )
+ public String getValue( String uri, String localName )
{
- return attrs.getValue(arg0, arg1);
+ return attrs.getValue(uri, asNekoAttributeName(localName));
}
/**
* <p>
@@ -431,7 +435,29 @@
*/
public void addAttribute( String name, Object value )
{
- addAttribute(new QName(null, name.toUpperCase(), name.toUpperCase(), null),"CDATA", value.toString());
-
+ QName qName = null ;
+ int i = name.indexOf(':');
+ if (i < 0)
+ {
+ name = name.toLowerCase();
+ qName = new QName(null,name,name,null);
+ }
+ else
+ {
+ String prefix = name.substring(0,i);
+ String localPart = name.substring(i+1).toLowerCase();
+ name = name.toLowerCase();
+ qName = new QName(prefix,localPart,name,null);
+ }
+ addAttribute(qName,"CDATA",value.toString());
+ }
+
+
+ // Support Methods
+
+ protected String asNekoAttributeName(String n)
+ {
+ // neko, by default, converts attribute names to lower case
+ return n != null ? n.toLowerCase() : null ;
}
}
Added: portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java?rev=390539&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java (added)
+++ portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java Fri Mar 31 15:16:16 2006
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jetspeed.rewriter;
+
+import java.io.BufferedInputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.util.Arrays;
+
+import javax.portlet.PortletException;
+
+import org.w3c.dom.Node;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.xerces.xni.parser.XMLDocumentFilter;
+import org.apache.xerces.xni.parser.XMLInputSource;
+
+import org.cyberneko.html.HTMLConfiguration ;
+import org.cyberneko.html.parsers.DOMParser;
+import org.cyberneko.html.parsers.SAXParser;
+import org.cyberneko.html.filters.Purifier ;
+import org.cyberneko.html.filters.Writer ;
+
+import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
+import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor;
+import org.apache.jetspeed.rewriter.rules.Ruleset;
+import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+
+/**
+ * TestNekoRewriter
+ *
+ * @author <a href="mailto:dyoung@phase2systems.com">David L Young</a>
+ * @version $Id:$
+ */
+public class TestNekoRewriter extends TestCase
+{
+
+ /**
+ * Defines the testcase name for JUnit.
+ *
+ * @param name
+ * the testcase's name.
+ */
+ public TestNekoRewriter(String name)
+ {
+ super(name);
+ }
+
+ public String getBaseProject()
+ {
+ return "components/jetspeed";
+ }
+
+ /**
+ * Start the tests.
+ *
+ * @param args
+ * the arguments. Not used
+ */
+ public static void main(String args[])
+ {
+ junit.awtui.TestRunner.main(new String[]
+ { TestNekoRewriter.class.getName()});
+ }
+
+ public static Test suite()
+ {
+ // All methods starting with "test" will be executed in the test suite.
+ return new TestSuite(TestNekoRewriter.class);
+ }
+
+
+ // DOMParser example
+
+ /* BOZO
+
+ public void testDOM() throws Exception
+ {
+ System.out.println( "testing...DOM" ) ;
+
+ // parse something and echo the DOM tree
+ String target = "http://www.google.com" ;
+ System.out.println( "Parsing: " + target ) ;
+
+ DOMParser parser = new DOMParser() ;
+ parser.parse( target ) ;
+ System.out.println( "parse() result..." ) ;
+ print( parser.getDocument(), "" ) ;
+ }
+
+ void print( Node node, String indent )
+ {
+ System.out.println(indent+node.getClass().getName());
+ Node child = node.getFirstChild();
+ while (child != null) {
+ print(child, indent+" ");
+ child = child.getNextSibling();
+ }
+ }
+ */
+
+
+ // SAXParser example
+
+ /* BOZO
+
+ public void testSAX() throws Exception
+ {
+ System.out.println( "testing...SAX" ) ;
+
+ // parse something to stdout
+ String target = "http://www.google.com" ;
+ System.out.println( "Parsing: " + target ) ;
+
+ SAXParser parser = new SAXParser() ;
+
+ // create pipeline filters
+ org.cyberneko.html.filters.Writer writer = new org.cyberneko.html.filters.Writer();
+ Purifier purifier = new Purifier() ;
+
+ // setup filter chain
+ XMLDocumentFilter[] filters = {
+ purifier,
+ writer,
+ };
+
+ parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
+
+ // parse documents
+ XMLInputSource source = new XMLInputSource(null, target, null);
+ parser.parse(source);
+ }
+ */
+
+
+
+ // NekoParserAdapter test
+
+ public void testNekoParserAdaptor() throws Exception
+ {
+ RewriterController controller = getController();
+ FileReader rulesReader = getTestReader("test-remove-rules.xml");
+ Ruleset ruleset = controller.loadRuleset(rulesReader);
+ rulesReader.close();
+ assertNotNull("ruleset is null", ruleset);
+ RulesetRewriter rewriter = controller.createRewriter(ruleset);
+ assertNotNull("ruleset rewriter is null", rewriter);
+
+ FileReader htmlReader = getTestReader("test-001.html");
+ FileWriter htmlWriter = getTestWriter("test-002-output.html");
+
+ ParserAdaptor adaptor = controller.createParserAdaptor("text/html");
+ rewriter.setBaseUrl("http://www.rewriter.com");
+ rewriter.rewrite(adaptor, htmlReader, htmlWriter);
+ htmlReader.close();
+ }
+
+ private RewriterController getController() throws Exception
+ {
+ Class[] rewriterClasses = new Class[]{ RulesetRewriterImpl.class, RulesetRewriterImpl.class};
+
+ Class[] adaptorClasses = new Class[]{ NekoParserAdaptor.class, SaxParserAdaptor.class};
+ return new JetspeedRewriterController("test/WEB-INF/conf/rewriter-rules-mapping.xml", Arrays.asList(rewriterClasses), Arrays.asList(adaptorClasses));
+ }
+
+ private Reader getRemoteReader(String uri) throws IOException
+ {
+ HttpClient client = new HttpClient();
+ GetMethod get = new GetMethod(uri);
+ int status = client.executeMethod(get);
+ BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
+ String encoding = get.getResponseCharSet();
+ return new InputStreamReader(bis, encoding);
+ }
+
+ /**
+ * Gets a reader for a given filename in the test directory.
+ *
+ * @return A file reader to the test rules file
+ * @throws IOException
+ */
+ private FileReader getTestReader(String filename) throws IOException
+ {
+ return new FileReader("test/rewriter/" + filename);
+ }
+
+ /**
+ * Gets a writer for a given filename in the test directory.
+ *
+ * @return A file reader to the test rules file
+ * @throws IOException
+ */
+ private FileWriter getTestWriter(String filename) throws IOException
+ {
+ return new FileWriter("test/rewriter/" + filename);
+ }
+
+
+ /* BOZO
+ public void testNekoWebTarget() throws Exception
+ {
+ // parse something with the NekoParserAdaptor
+ String target = "http://www.google.com";
+
+ RewriterController controller = getController();
+ FileReader rulesReader = getTestReader("test-remove-rules.xml");
+ Ruleset ruleset = controller.loadRuleset(rulesReader);
+ rulesReader.close();
+ assertNotNull("ruleset is null", ruleset);
+ RulesetRewriter rewriter = controller.createRewriter(ruleset);
+ assertNotNull("ruleset rewriter is null", rewriter);
+
+ java.io.Reader htmlReader = getRemoteReader(target);
+ java.io.Writer htmlWriter = new OutputStreamWriter(System.out);
+
+ ParserAdaptor adaptor = controller.createParserAdaptor("text/html");
+ rewriter.setBaseUrl("http://www.rewriter.com");
+ rewriter.rewrite(adaptor, htmlReader, htmlWriter);
+ htmlReader.close();
+ }
+ */
+}
Propchange: portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestNekoRewriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestRewriterController.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestRewriterController.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestRewriterController.java (original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/test/org/apache/jetspeed/rewriter/TestRewriterController.java Fri Mar 31 15:16:16 2006
@@ -322,4 +322,4 @@
return new InputStreamReader(is);
}
-}
\ No newline at end of file
+}
Added: portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html?rev=390539&view=auto
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html (added)
+++ portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html Fri Mar 31 15:16:16 2006
@@ -0,0 +1,12 @@
+
+
+
+
+<P>This is a test</P>
+<A name="1" href="http://www.bluesunrise.com/suffix" target="_BLANK">keep this</A>
+<A name="2" href="http://www.rewriter.com/stuff/junk/stuffedjunk.html/suffix" target="_BLANK">junk</A>
+<A name="3" href="http://www.rewriter.com/stuff/junk/stuffedjunk.html/suffix" target="_BLANK">junk2</A>
+<A name="4" href="javascript:whatever()">script</A>
+<A name="5" href="mailto:david@bluesunrise.com">script</A>
+<A name="6" href="#INTERNAL">internal</A>
+
Propchange: portals/jetspeed-2/trunk/components/rewriter/test/rewriter/test-002-output.html
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/trunk/components/search/src/java/org/apache/jetspeed/search/handlers/URLToDocHandler.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/search/src/java/org/apache/jetspeed/search/handlers/URLToDocHandler.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/search/src/java/org/apache/jetspeed/search/handlers/URLToDocHandler.java (original)
+++ portals/jetspeed-2/trunk/components/search/src/java/org/apache/jetspeed/search/handlers/URLToDocHandler.java Fri Mar 31 15:16:16 2006
@@ -57,78 +57,75 @@
URL pageToAdd = (URL) o;
HttpClient client = new HttpClient();
- client.startSession(pageToAdd);
- GetMethod method = new GetMethod(pageToAdd.getPath());
+ GetMethod method = new GetMethod(pageToAdd.toString());
method.setFollowRedirects(true);
int statusCode = -1;
int attempt = 0;
- // We will retry up to 3 times.
- while (statusCode == -1 && attempt < 3)
+ try
{
- try
+ // We will retry up to 3 times.
+ while (statusCode == -1 && attempt < 3)
{
- // execute the method.
- client.executeMethod(method);
- statusCode = method.getStatusCode();
- //if (logger.isDebugEnabled())
+ try
{
- //logger.debug("URL = " + pageToAdd.toString() + "Status code = " + statusCode);
+ // execute the method.
+ client.executeMethod(method);
+ statusCode = method.getStatusCode();
+ //if (logger.isDebugEnabled())
+ {
+ //logger.debug("URL = " + pageToAdd.toString() + "Status code = " + statusCode);
+ }
+ }
+ catch (HttpException e)
+ {
+ // We will retry
+ attempt++;
+ }
+ catch (IOException e)
+ {
+ return null;
}
}
- catch (HttpException e)
- {
- // We will retry
- attempt++;
- }
- catch (IOException e)
- {
- return null;
- }
- }
- // Check that we didn't run out of retries.
- if (statusCode != -1)
- {
- String content = null;
- try
- {
- content = method.getResponseBodyAsString();
- }
- catch (Exception ioe)
- {
- //logger.error("Getting content for " + pageToAdd.toString(), ioe);
- }
-
- if (content != null)
+ // Check that we didn't run out of retries.
+ if (statusCode != -1)
{
+ String content = null;
try
{
- result.setKey(java.net.URLEncoder.encode(pageToAdd.toString()));
- result.setType(org.apache.jetspeed.search.ParsedObject.OBJECT_TYPE_URL);
- // TODO: We should extract the <title> tag here.
- result.setTitle(pageToAdd.toString());
- result.setContent(content);
- result.setDescription("");
- result.setLanguage("");
- result.setURL(pageToAdd);
- result.setClassName(o.getClass().getName());
- //logger.info("Parsed '" + pageToAdd.toString() + "'");
+ content = method.getResponseBodyAsString();
}
- catch (Exception e)
+ catch (Exception ioe)
{
- e.printStackTrace();
- //logger.error("Adding document to index", e);
+ //logger.error("Getting content for " + pageToAdd.toString(), ioe);
+ }
+
+ if (content != null)
+ {
+ try
+ {
+ result.setKey(java.net.URLEncoder.encode(pageToAdd.toString(),"UTF-8"));
+ result.setType(org.apache.jetspeed.search.ParsedObject.OBJECT_TYPE_URL);
+ // TODO: We should extract the <title> tag here.
+ result.setTitle(pageToAdd.toString());
+ result.setContent(content);
+ result.setDescription("");
+ result.setLanguage("");
+ result.setURL(pageToAdd);
+ result.setClassName(o.getClass().getName());
+ //logger.info("Parsed '" + pageToAdd.toString() + "'");
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ //logger.error("Adding document to index", e);
+ }
}
}
}
- try
- {
- client.endSession();
- }
- catch (IOException ioe)
+ finally
{
- ioe.printStackTrace();
- //logger.error("Ending session to " + pageToAdd.toString(), ioe);
+ method.releaseConnection();
}
return result;
Modified: portals/jetspeed-2/trunk/components/sso/project.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/sso/project.xml?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/sso/project.xml (original)
+++ portals/jetspeed-2/trunk/components/sso/project.xml Fri Mar 31 15:16:16 2006
@@ -91,7 +91,7 @@
<dependency>
<id>commons-httpclient</id>
<groupId>commons-httpclient</groupId>
- <version>2.0.2</version>
+ <version>3.0</version>
<properties>
<war.bundle>true</war.bundle>
</properties>
Modified: portals/jetspeed-2/trunk/components/sso/src/java/org/apache/jetspeed/sso/impl/PersistenceBrokerSSOProvider.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/sso/src/java/org/apache/jetspeed/sso/impl/PersistenceBrokerSSOProvider.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/sso/src/java/org/apache/jetspeed/sso/impl/PersistenceBrokerSSOProvider.java (original)
+++ portals/jetspeed-2/trunk/components/sso/src/java/org/apache/jetspeed/sso/impl/PersistenceBrokerSSOProvider.java Fri Mar 31 15:16:16 2006
@@ -1387,25 +1387,26 @@
}
- //try
- //{
+ try
+ {
//bis = new BufferedInputStream(get.getResponseBodyAsStream());
resultPage = get.getResponseBodyAsString();
- //}
- //catch(IOException ioe)
- //{
- // log.error(strErrorMessage, ioe);
- // throw new SSOException (strErrorMessage, ioe);
- //
- //}
- //catch (Exception e)
- //{
- // log.error(strErrorMessage, e);
- // throw new SSOException (strErrorMessage, e);
- //
- //}
-
- get.releaseConnection();
+ }
+ catch(IOException ioe)
+ {
+ log.error(strErrorMessage, ioe);
+ throw new SSOException (strErrorMessage, ioe);
+ }
+ catch (Exception e)
+ {
+ log.error(strErrorMessage, e);
+ throw new SSOException (strErrorMessage, e);
+
+ }
+ finally
+ {
+ get.releaseConnection();
+ }
//return bis;
return resultPage;
Modified: portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/SSOWebContentPortlet.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/SSOWebContentPortlet.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/SSOWebContentPortlet.java (original)
+++ portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/SSOWebContentPortlet.java Fri Mar 31 15:16:16 2006
@@ -15,7 +15,11 @@
*/
package org.apache.jetspeed.portlet;
+import java.util.Map;
+import java.io.BufferedInputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.security.AccessControlContext;
import java.security.AccessController;
@@ -30,6 +34,13 @@
import javax.portlet.RenderResponse;
import javax.security.auth.Subject;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.jetspeed.rewriter.WebContentRewriter;
import org.apache.jetspeed.sso.SSOContext;
import org.apache.jetspeed.sso.SSOException;
@@ -49,6 +60,7 @@
public static final String SSO_TYPE_URL_BASE64 = "url.base64";
public static final String SSO_TYPE_HTTP = "http";
public static final String SSO_TYPE_CERTIFICATE = "certificate";
+ public static final String SSO_TYPE_DEFAULT = SSO_TYPE_HTTP;
public static final String SSO_TYPE_URL_USERNAME = "sso.url.Principal";
public static final String SSO_TYPE_URL_PASSWORD = "sso.url.Credential";
@@ -124,8 +136,7 @@
String site = request.getPreferences().getValue("SRC", null);
if (site == null)
{
- // no credentials configured in SSO store
- // switch to SSO Configure View
+ // no SRC configured in prefs - switch to SSO Configure View
request.setAttribute(PARAM_VIEW_PAGE, this.getPortletConfig().getInitParameter(PARAM_EDIT_PAGE));
setupPreferencesEdit(request, response);
super.doView(request, response);
@@ -193,44 +204,52 @@
return Subject.getSubject(context);
}
- public String getURLSource(String src, RenderRequest request, RenderResponse response)
+ public String getURLSource(String src, Map params, RenderRequest request, RenderResponse response)
{
+ String baseSource = super.getURLSource(src, params, request, response);
+
PortletPreferences prefs = request.getPreferences();
- String baseSource = super.getURLSource(src, request, response);
- String type = prefs.getValue(SSO_TYPE, SSO_TYPE_URL);
- if (type.equals(SSO_TYPE_URL))
+ String type = prefs.getValue(SSO_TYPE, SSO_TYPE_DEFAULT);
+ if (type.equals(SSO_TYPE_URL) || type.equals(SSO_TYPE_URL_BASE64))
{
+ // set user name and password parameters
String userNameParam = prefs.getValue(SSO_TYPE_URL_USERNAME, "user");
String passwordParam = prefs.getValue(SSO_TYPE_URL_PASSWORD, "password");
- StringBuffer source = new StringBuffer(baseSource);
- if (baseSource.indexOf("?") == -1)
- {
- source.append("?");
- }
- else
- {
- source.append("&");
- }
- source.append(userNameParam);
- source.append("=");
-
String userName = (String)request.getAttribute(SSO_REQUEST_ATTRIBUTE_USERNAME);
if (userName == null) userName = "";
String password = (String)request.getAttribute(SSO_REQUEST_ATTRIBUTE_PASSWORD);
if (password == null) password = "";
+ if (type.equals(SSO_TYPE_URL_BASE64))
+ {
+ Base64 encoder = new Base64() ;
+ userName = new String( encoder.encode( userName.getBytes() ));
+ password = new String( encoder.encode( password.getBytes() ));
+ }
- source.append(userName);
- source.append("&");
- source.append(passwordParam);
- source.append("=");
- source.append(password);
-
- return response.encodeURL(source.toString());
+ params.put(userNameParam,new String[]{ userName }) ;
+ params.put(passwordParam,new String[]{ password }) ;
}
- else
+
+ return baseSource;
+ }
+
+ protected HttpClient getHttpClient(RenderRequest request) throws IOException
+ {
+ HttpClient client = super.getHttpClient(request) ;
+
+ PortletPreferences prefs = request.getPreferences();
+ String type = prefs.getValue(SSO_TYPE, SSO_TYPE_DEFAULT);
+ if (type.equals(SSO_TYPE_HTTP))
{
- return baseSource;
+ String userName = (String)request.getAttribute(SSO_REQUEST_ATTRIBUTE_USERNAME);
+ if (userName == null) userName = "";
+ String password = (String)request.getAttribute(SSO_REQUEST_ATTRIBUTE_PASSWORD);
+ if (password == null) password = "";
+
+ HttpState state = client.getState() ;
+ state.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(userName, password));
}
+
+ return client;
}
-
}
Modified: portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java (original)
+++ portals/jetspeed-2/trunk/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java Fri Mar 31 15:16:16 2006
@@ -16,6 +16,11 @@
import java.io.IOException;
import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
@@ -33,13 +38,20 @@
import org.apache.portals.messaging.PortletMessaging;
import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.Cookie;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.jetspeed.rewriter.JetspeedRewriterController;
import org.apache.jetspeed.rewriter.RewriterController;
import org.apache.jetspeed.rewriter.RewriterException;
import org.apache.jetspeed.rewriter.RulesetRewriter;
import org.apache.jetspeed.rewriter.WebContentRewriter;
-import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
+import org.apache.jetspeed.rewriter.html.neko.NekoParserAdaptor;
import org.apache.jetspeed.rewriter.rules.Ruleset;
import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
@@ -103,16 +115,17 @@
*
* */
public static final String CURRENT_URL = "webcontent.url.current";
+ public static final String CURRENT_URL_PARAMS = "webcontent.url.current.params";
+ public static final String CURRENT_URL_METHOD = "webcontent.url.current.method";
public static final String LAST_URL = "webcontent.url.last";
- public static final String LAST_STATE = "webcontent.last.state";
+ public static final String LAST_URL_PARAMS = "webcontent.url.last.params";
+ public static final String LAST_WINDOW_STATE = "webcontent.window.last.state";
public static final String CACHE = "webcontent.cache";
+ public static final String HTTP_STATE = "webcontent.http.state";
/** Default encoding */
public String defaultEncoding = "UTF-8";
- /* SSO settings */
- boolean isSSOEnabled = false;
-
/* WebContent rewriter */
RulesetRewriter rewriter = null;
@@ -147,23 +160,34 @@
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException,
IOException
{
+
// Check if an action parameter was defined
- String webContentParameter = actionRequest.getParameter(WebContentRewriter.ACTION_PARAMETER_URL);
+ String webContentURL = actionRequest.getParameter(WebContentRewriter.ACTION_PARAMETER_URL);
+ String webContentMethod = actionRequest.getParameter(WebContentRewriter.ACTION_PARAMETER_METHOD);
- if (webContentParameter == null || actionRequest.getPortletMode() == PortletMode.EDIT)
+ if (webContentURL == null || actionRequest.getPortletMode() == PortletMode.EDIT)
{
processPreferencesAction(actionRequest, actionResponse);
- webContentParameter = actionRequest.getPreferences().getValue("SRC", "http://portals.apache.org");
+ webContentURL = actionRequest.getPreferences().getValue("SRC", "http://portals.apache.org");
}
-
/*
* If the webContentParameter is not empty attach the URL to the session
*/
- if (webContentParameter != null && webContentParameter.length() > 0)
+ if (webContentURL != null && webContentURL.length() > 0)
{
- String sessionObj = new String(webContentParameter);
- PortletMessaging.publish(actionRequest, CURRENT_URL, sessionObj);
+ // Map BOZO String sessionObj = new String(webContentParameter);
+ // getParameterMap() includes the URL (as ACTION_PARAMETER_URL), but all actual params as well
+ Map params = actionRequest.getParameterMap() ;
+ Map webContentParams = params != null ? new HashMap(params) : new HashMap() ;
+ if (webContentMethod == null) webContentMethod = "" ; // default to GET
+
+ webContentParams.remove(WebContentRewriter.ACTION_PARAMETER_URL);
+ webContentParams.remove(WebContentRewriter.ACTION_PARAMETER_METHOD);
+
+ PortletMessaging.publish(actionRequest, CURRENT_URL, webContentURL);
+ PortletMessaging.publish(actionRequest, CURRENT_URL_PARAMS, webContentParams);
+ PortletMessaging.publish(actionRequest, CURRENT_URL_METHOD, webContentMethod);
}
}
@@ -182,15 +206,20 @@
// Find the source URL to execute
String sourceURL = null;
+ Map sourceParams = null;
+ String sourceMethod = null;
String lastURL = null;
- boolean useCache = false;
+ Map lastParams = null;
// Check if the source was defined in the session
try
{
sourceURL = (String)PortletMessaging.receive(request, CURRENT_URL);
+ sourceParams = (Map)PortletMessaging.receive(request, CURRENT_URL_PARAMS);
+ sourceMethod = (String)PortletMessaging.receive(request, CURRENT_URL_METHOD);
- lastURL = (String)PortletMessaging.receive(request, LAST_URL);
+ lastURL = (String)PortletMessaging.receive(request, LAST_URL);
+ lastParams = (Map)PortletMessaging.receive(request, LAST_URL_PARAMS);
// TODO: This is just a kludge. Filtering of bad uRL's should be
// more sophisticated
if (sourceURL.startsWith("/") || sourceURL.startsWith("..")) sourceURL = null;
@@ -205,7 +234,6 @@
{
// Use the cache
sourceURL = lastURL;
- useCache = true;
}
if (sourceURL == null)
@@ -214,15 +242,10 @@
sourceURL = request.getPreferences().getValue("SRC", "");
}
- if (lastURL != null && sourceURL.equals(lastURL))
- {
- useCache = true;
- }
-
// If all above fails throw an error asking the user to define an URL in
// edit mode
if (sourceURL == null)
- throw new PortletException("WebContent source not specified. Go to edit mode and specify an URL.");
+ throw new PortletException("WebContent source not specified. Go to edit mode and specify an URL.");
// Initialize the controller if it's not already done
if (rewriteController == null)
@@ -243,25 +266,19 @@
}
}
- String lastState = (String)PortletMessaging.receive(request, LAST_STATE);
- String newState = request.getWindowState().toString();
- if (lastState == null || newState == null || !lastState.equals(newState))
- {
- useCache = false;
- }
+ String newWindowState = request.getWindowState().toString();
+ String lastWindowState = (String)PortletMessaging.receive(request, LAST_WINDOW_STATE);
+
+ // if *everything* is the same, we can use the cache
+ boolean useCache = (lastURL != null && sourceURL.equals(lastURL) && lastParams != null && sourceParams != null && sourceParams.equals(lastParams) && lastWindowState != null && newWindowState != null && newWindowState.equals(lastWindowState));
// Set the content type
response.setContentType("text/html");
- byte[] content;
- byte[] cache = (byte[])request.getPortletSession().getAttribute(CACHE, PortletSession.PORTLET_SCOPE);
- if (useCache && cache != null)
- {
- content = cache;
- }
- else
+ byte[] content = useCache ? (byte[])request.getPortletSession().getAttribute(CACHE, PortletSession.PORTLET_SCOPE) : null;
+ if (content == null)
{
// Draw the content
- content = doWebContent(request, sourceURL, response);
+ content = doWebContent(sourceURL, sourceParams, sourceMethod, request, response);
request.getPortletSession().setAttribute(CACHE, content, PortletSession.PORTLET_SCOPE);
}
@@ -270,10 +287,10 @@
drain(new InputStreamReader(bais, this.defaultEncoding), response.getWriter());
bais.close();
- // Done just save the last URL
- lastURL = sourceURL;
- PortletMessaging.publish(request, LAST_URL, lastURL);
- PortletMessaging.publish(request, LAST_STATE, newState);
+ // Done just save the last set of parameters
+ PortletMessaging.publish(request, LAST_URL, sourceURL);
+ PortletMessaging.publish(request, LAST_URL_PARAMS, sourceParams != null?sourceParams:new HashMap());
+ PortletMessaging.publish(request, LAST_WINDOW_STATE, newWindowState);
}
public void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException
@@ -285,31 +302,67 @@
/*
* Privaye helpers for generating WebContent
*/
- protected byte[] doWebContent(RenderRequest request, String sourceAttr, RenderResponse response)
+ protected byte[] doWebContent(String sourceAttr, Map sourceParams, String sourceMethod, RenderRequest request, RenderResponse response)
throws PortletException
{
- // Initialization
- Writer htmlWriter = null;
-
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
+ HttpMethodBase httpMethod = null ;
try
{
- htmlWriter = new OutputStreamWriter(byteOutputStream, this.defaultEncoding);
-
- // Set the action URL in the rewriter
- PortletURL action = response.createActionURL();
- ((WebContentRewriter) rewriter).setActionURL(action);
-
+ // Set the action and base URLs in the rewriter
+ PortletURL action = response.createActionURL();
+ ((WebContentRewriter) rewriter).setActionURL(action);
+
URL baseURL = new URL(sourceAttr);
- String baseurl = baseURL.getProtocol() + "://" + baseURL.getHost();
+ rewriter.setBaseUrl(baseURL.toString());
+
+ // Get the input stream...
+
+ // ...set up URL and HttpClient stuff
+ sourceAttr = getURLSource(sourceAttr, sourceParams, request, response);
+ HttpClient httpClient = getHttpClient(request) ;
+ httpMethod = getHttpMethod(sourceAttr, sourceParams, sourceMethod, request);
+ httpClient.executeMethod(httpMethod);
+
+ // ...save updated state
+ Cookie[] cookies = httpClient.getState().getCookies();
+ PortletMessaging.publish(request, HTTP_STATE, cookies);
+
+ // ...check for manual redirects
+ int responseCode = httpMethod.getStatusCode();
+ if (responseCode >= 300 && responseCode <= 399)
+ {
+ // redirection that could not be handled automatically!!! (probably from a POST)
+ Header locationHeader = httpMethod.getResponseHeader("location");
+ String redirectLocation = locationHeader != null ? locationHeader.getValue() : null ;
+ if (redirectLocation != null)
+ {
+ // one more time (assume most params are already encoded & new URL is using GET protocol!)
+ return doWebContent( redirectLocation, new HashMap(), "get", request, response ) ;
+ }
+ else
+ {
+ // The response is a redirect, but did not provide the new location for the resource.
+ throw new PortletException("Redirection code: "+responseCode+", but with no redirectionLocation set.");
+ }
+ }
+
+ // ...ok - *now* create the input stream and reader
+ BufferedInputStream bis = new BufferedInputStream(httpMethod.getResponseBodyAsStream());
+ String encoding = httpMethod.getResponseCharSet();
+ if (encoding == null)
+ encoding = getContentCharSet(bis);
+ Reader htmlReader = new InputStreamReader(bis, encoding);
+
+ // get the output buffer
+ if (encoding == null)
+ encoding = this.defaultEncoding ;
+ Writer htmlWriter = new OutputStreamWriter(byteOutputStream, encoding);
- rewriter.setBaseUrl(baseurl);
- String source = getURLSource(sourceAttr, request, response);
- // System.out.println("Rewriting SOURCE: " + source);
- rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), getRemoteReader(source), htmlWriter);
+ // rewrite and flush output
+ rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), htmlReader, htmlWriter);
htmlWriter.flush();
-
}
catch (UnsupportedEncodingException ueex)
{
@@ -323,13 +376,19 @@
{
throw new PortletException("Exception while rewritting HTML page. Error: " + e.getMessage());
}
+ finally
+ {
+ // release the http connection
+ if (httpMethod != null)
+ httpMethod.releaseConnection();
+ }
// Page has been rewritten
// TODO: Write it to cache
return byteOutputStream.toByteArray();
}
- public String getURLSource(String source, RenderRequest request, RenderResponse response)
+ public String getURLSource(String source, Map params, RenderRequest request, RenderResponse response)
{
return source;
}
@@ -352,7 +411,7 @@
{ WebContentRewriter.class, WebContentRewriter.class};
Class[] adaptorClasses = new Class[]
- { SwingParserAdaptor.class, SaxParserAdaptor.class};
+ { NekoParserAdaptor.class, SaxParserAdaptor.class};
RewriterController rwc = new JetspeedRewriterController(contextPath + "conf/rewriter-rules-mapping.xml", Arrays
.asList(rewriterClasses), Arrays.asList(adaptorClasses));
@@ -364,91 +423,84 @@
return rwc;
}
- /*
- * getReaderForURL() Streams the page from the uRL into the reader
- */
- protected Reader getReader(String url) throws PortletException
+ protected HttpClient getHttpClient(RenderRequest request) throws IOException
{
- URL pageUrl = null;
- URLConnection pageConn = null;
-
- // Open the connection to the page
- try
- {
- pageUrl = new URL(url);
- pageConn = pageUrl.openConnection();
-
- if (this.isSSOEnabled == true)
+ // derived class hook (e.g. to set up Basic Authentication)
+ HttpClient client = new HttpClient();
+
+ // reuse existing state, if we have been here before
+ Cookie[] cookies = (Cookie[])PortletMessaging.receive(request, HTTP_STATE);
+ if (cookies != null)
+ client.getState().addCookies(cookies);
+
+ return client ;
+ }
+
+ protected HttpMethodBase getHttpMethod(String uri, Map params, String method, RenderRequest request) throws IOException
+ {
+ HttpMethodBase httpMethod = null;
+ String useragentProperty = request.getProperty("User-Agent");
+ if (method == null || !method.equalsIgnoreCase("post"))
+ {
+ // System.out.println(">>>>>>>>>>>> HTTP GET from URL: "+uri);
+ // http GET
+ httpMethod = new GetMethod(uri);
+ if (params != null && !params.isEmpty())
{
- /*
- * TODO: SSO should provide username & password
- *
- * String username, password; // set HTTP Basic Authetication
- * header if username and password are set if (username != null &&
- * password !=null) {
- * pageConn.setRequestProperty("Authorization", "Basic " +
- * Base64.encodeAsString(username + ":" + password)); }
- */
+ ArrayList pairs = new ArrayList();
+ Iterator iter = params.entrySet().iterator();
+ while (iter.hasNext())
+ {
+ Map.Entry entry = (Map.Entry)iter.next() ;
+ String name = (String)entry.getKey() ;
+ String[] values = (String [])entry.getValue() ;
+ if (values != null)
+ for (int i = 0,limit = values.length; i < limit; i++)
+ {
+ // System.out.println("...adding GET parameter: "+name+", with value: "+values[i]);
+ pairs.add(new NameValuePair(name, values[i]));
+ }
+ }
+ httpMethod.setQueryString((NameValuePair[])pairs.toArray(new NameValuePair[pairs.size()]));
}
+
+ // automatically follow redirects (NOTE: not supported in POST - will throw exeception if you ask for it, then sees a redirect!!)
+ httpMethod.setFollowRedirects(true);
}
- catch (MalformedURLException urle)
- {
- throw new PortletException("Malformed URL. Error: " + urle.getMessage());
- }
- catch (IOException ioe)
- {
- throw new PortletException("Failed connecting to URL. Error: " + ioe.getMessage());
- }
- catch (Exception e)
- {
- throw new PortletException("Failed connecting to URL. Error: " + e.getMessage());
- }
-
- long pageExpiration = pageConn.getExpiration();
- String encoding = defaultEncoding;
- String contentType = pageConn.getContentType();
- String tempString = null;
- String noCache = "no-cache";
-
- if (contentType != null)
+ else
{
- StringTokenizer st = new StringTokenizer(contentType, "; =");
- while (st.hasMoreTokens())
+ // System.out.println(">>>>>>>>>>>> HTTP POST to URL: "+uri);
+ // http POST
+ PostMethod postMethod = (PostMethod)( httpMethod = new PostMethod(uri)) ;
+ if (params != null && !params.isEmpty())
{
- if (st.nextToken().equalsIgnoreCase("charset"))
+ Iterator iter = params.entrySet().iterator();
+ while (iter.hasNext())
{
- try
- {
- encoding = st.nextToken();
- break;
- }
- catch (Exception e)
- {
- break;
- }
- }
+ Map.Entry entry = (Map.Entry)iter.next();
+ String name = (String)entry.getKey();
+ String[] values = (String[])entry.getValue();
+ if (values != null)
+ for (int i=0,limit=values.length; i<limit; i++)
+ {
+ // System.out.println("...adding POST parameter: "+name+", with value: "+values[i]);
+ postMethod.addParameter(name, values[i]);
+ }
+ }
}
}
-
- Reader rdr = null;
-
- try
- {
- // Assign a reader
- rdr = new InputStreamReader(pageConn.getInputStream(), encoding);
- }
- catch (UnsupportedEncodingException ueex)
- {
- throw new PortletException("Encoding " + encoding + " not supported. Error: " + ueex.getMessage());
- }
- catch (IOException ioex)
- {
- throw new PortletException("Failed open stream to site " + url + " Error: " + ioex.getMessage());
- }
-
- return rdr;
+
+ // propagate User-Agent, so target site does not think we are a D.O.S. attack
+ httpMethod.addRequestHeader( "User-Agent", useragentProperty );
+
+ // NO - DON'T do this. default policy seems to be more flexible!!!
+ //httpMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+
+ // ...ready to use!
+ return httpMethod ;
}
+
static final int BLOCK_SIZE = 4096;
private void drain(InputStream reader, OutputStream writer) throws IOException
@@ -501,28 +553,6 @@
w.flush();
}
- private Reader getRemoteReader(String uri) throws PortletException
- {
- try
- {
- HttpClient client = new HttpClient();
- GetMethod get = new GetMethod(uri);
- int status = client.executeMethod(get);
- BufferedInputStream bis = new BufferedInputStream(get.getResponseBodyAsStream());
- bis.mark(BLOCK_SIZE);
- String encoding = getContentCharSet(bis);
- if (encoding == null)
- {
- encoding = get.getResponseCharSet();
- }
- return new InputStreamReader(bis, encoding);
- }
- catch (IOException e)
- {
- throw new PortletException(e);
- }
- }
-
private String getContentCharSet(InputStream is) throws IOException
{
if (!is.markSupported())
@@ -533,6 +563,7 @@
byte[] buf = new byte[BLOCK_SIZE];
try
{
+ is.mark(BLOCK_SIZE);
is.read(buf, 0, BLOCK_SIZE);
String content = new String(buf, "ISO-8859-1");
String lowerCaseContent = content.toLowerCase();
@@ -562,7 +593,6 @@
{
if (est.hasMoreTokens())
{
- is.reset();
return est.nextToken();
}
}
@@ -573,8 +603,10 @@
catch (IOException e)
{
}
-
- is.reset();
+ finally
+ {
+ is.reset();
+ }
return null;
}
Modified: portals/jetspeed-2/trunk/pom.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/pom.xml?rev=390539&r1=390538&r2=390539&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/pom.xml (original)
+++ portals/jetspeed-2/trunk/pom.xml Fri Mar 31 15:16:16 2006
@@ -141,7 +141,7 @@
<commons-dbcp.version>1.2.1</commons-dbcp.version>
<commons-digester.version>1.5</commons-digester.version>
<commons-fileupload.version>1.0</commons-fileupload.version>
- <commons-httpclient.version>2.0.2</commons-httpclient.version>
+ <commons-httpclient.version>3.0</commons-httpclient.version>
<commons-io.version>0.1</commons-io.version>
<commons-lang.version>2.0</commons-lang.version>
<commons-logging.version>1.0.3</commons-logging.version>
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org