portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwat...@apache.org
Subject svn commit: r390958 - in /portals/jetspeed-2/trunk: ./ components/rewriter/src/java/org/apache/jetspeed/rewriter/ components/web-content/src/java/org/apache/jetspeed/portlet/
Date Mon, 03 Apr 2006 05:48:08 GMT
Author: rwatler
Date: Sun Apr  2 22:48:07 2006
New Revision: 390958

URL: http://svn.apache.org/viewcvs?rev=390958&view=rev
Log:
Neko/rewritere update:
- upgrade neko to 0.9.5
- correct relative url rewriting bug
Contributed by David Young


Modified:
    portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/AbstractRewriter.java
    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/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/core-build.xml
    portals/jetspeed-2/trunk/pom.xml

Modified: portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/AbstractRewriter.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/AbstractRewriter.java?rev=390958&r1=390957&r2=390958&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/AbstractRewriter.java
(original)
+++ portals/jetspeed-2/trunk/components/rewriter/src/java/org/apache/jetspeed/rewriter/AbstractRewriter.java
Sun Apr  2 22:48:07 2006
@@ -17,7 +17,10 @@
 
 import java.io.Reader;
 import java.io.Writer;
+import java.net.URL;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 /**
  * AbstractRewriter
  *
@@ -26,6 +29,8 @@
  */
 public abstract class AbstractRewriter implements Rewriter
 {
+    protected final static Log log = LogFactory.getLog(AbstractRewriter.class);
+    
     private String baseUrl = null;
     private boolean useProxy = false; 
         
@@ -48,10 +53,7 @@
     /* (non-Javadoc)
      * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#rewriteUrl(java.lang.String,
java.lang.String, java.lang.String)
      */
-    public abstract String rewriteUrl(
-        String url,
-        String tag,
-        String attribute);
+    public abstract String rewriteUrl(String url, String tag, String attribute);
     
     /* (non-Javadoc)
      * @see org.apache.jetspeed.syndication.services.crawler.rewriter.Rewriter#setBaseUrl(java.lang.String)
@@ -67,6 +69,23 @@
     public String getBaseUrl()
     {
         return baseUrl;
+    }
+    public String getBaseRelativeUrl(String relativeUrl)
+    {        
+        try
+        {
+            String baseUrl = getBaseUrl();
+            if (baseUrl != null)
+            {
+                URL xlate = new URL(new URL(baseUrl), relativeUrl);
+                return xlate.toString();
+            }
+        }
+        catch (Exception e)
+        {
+            log.error("Unable to translate URL relative to base URL", e);
+        }
+        return relativeUrl;
     }
     
     /* (non-Javadoc)

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=390958&r1=390957&r2=390958&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
Sun Apr  2 22:48:07 2006
@@ -15,7 +15,8 @@
  */
 package org.apache.jetspeed.rewriter;
 
-import java.net.URL;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 
 /**
@@ -26,6 +27,8 @@
  */
 public class BasicRewriter extends AbstractRewriter implements Rewriter
 {
+    protected final static Log log = LogFactory.getLog(BasicRewriter.class);
+
     /*    
      * This callback is called by the ParserAdaptor implementation to write
      * back all rewritten URLs to point to the proxy server.
@@ -36,25 +39,7 @@
      */
     public String rewriteUrl(String url, String tag, String attribute)
     {
-        String fullPath = "";
-        try
-        {
-            String baseUrl = getBaseUrl();
-            if (baseUrl != null)
-            {
-                URL full = new URL(new URL(baseUrl), url);
-                fullPath = full.toString();
-            }
-            else
-            {
-                return url; // leave as is
-            }
-        }
-        catch (Exception e)
-        {
-            System.err.println(e);
-        }
-        return fullPath;
+        return getBaseRelativeUrl(url);
     }
     
     /* (non-Javadoc)

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=390958&r1=390957&r2=390958&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
Sun Apr  2 22:48:07 2006
@@ -156,38 +156,21 @@
                 }
             }            
         }
-        
     }
-
-    /*    
-     * This callback is called by the ParserAdaptor 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.
-     *
-     * @return the rewritten URL to the proxy server.
-     *
+    
+    /**
+     * rewriteURL
+     * 
+     * @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, MutableAttributes
otherAttribues )
+    public String rewriteUrl(String url, String tag, String attribute, MutableAttributes
otherAttributes)
     {
-        String fullPath = "";
-        try
-        {
-            String baseUrl = getBaseUrl();
-            if (baseUrl != null)
-            {
-                URL full = new URL(new URL(baseUrl), url);
-                fullPath = full.toString();
-            }
-            else
-            {
-                return url; // leave as is
-            }
-        }
-        catch (Exception e)
-        {
-            log.error("failure to rewriteUrl: "+url,e) ;
-        }
-        return fullPath;
+        return getBaseRelativeUrl(url);
     }
-    
 }

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=390958&r1=390957&r2=390958&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
Sun Apr  2 22:48:07 2006
@@ -38,9 +38,11 @@
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpState;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.auth.AuthState;
 import org.apache.jetspeed.rewriter.WebContentRewriter;
 import org.apache.jetspeed.sso.SSOContext;
 import org.apache.jetspeed.sso.SSOException;
@@ -103,13 +105,7 @@
             // ssoUserName 
             String ssoPrincipal = request.getParameter(SSO_FORM_PRINCIPAL);
             String ssoCredential = request.getParameter(SSO_FORM_CREDENTIAL);        
-            /*
-            if (ssoPrincipal == null || ssoCredential == null)
-            {
-                
-                actionResponse.setPortletMode(PortletMode.EDIT); // stay on edit
-            }
-            */
+
             String site = request.getPreferences().getValue("SRC", "");
             try
             {
@@ -232,11 +228,15 @@
         
         return baseSource;
     }
-
-    protected HttpClient getHttpClient(RenderRequest request) throws IOException
+    
+    protected boolean doAuthorize(HttpClient client,HttpMethod method, RenderRequest request)
     {
-        HttpClient client = super.getHttpClient(request) ;
-
+        if ( super.doAuthorize(client, method, request ))
+        {
+            // already handled
+            return true ;
+        }
+        
         PortletPreferences prefs = request.getPreferences();
         String type = prefs.getValue(SSO_TYPE, SSO_TYPE_DEFAULT);
         if (type.equals(SSO_TYPE_HTTP))
@@ -245,11 +245,17 @@
             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));
+            
+            method.setDoAuthentication(true);
+            AuthState state = method.getHostAuthState();
+            AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, state.getRealm(),
state.getAuthScheme().getSchemeName()) ;
+            client.getState().setCredentials(scope, new UsernamePasswordCredentials(userName,
password));
+            
+            // handled!
+            return true ;
         }
 
-        return client;
+        // only know how to handle Basic authentication, in this context
+        return false;
     }
 }

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=390958&r1=390957&r2=390958&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
Sun Apr  2 22:48:07 2006
@@ -38,6 +38,7 @@
 import org.apache.portals.messaging.PortletMessaging;
 
 import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpMethod;
 import org.apache.commons.httpclient.HttpMethodBase;
 import org.apache.commons.httpclient.HttpState;
 import org.apache.commons.httpclient.Cookie;
@@ -277,10 +278,13 @@
         byte[] content = useCache ? (byte[])request.getPortletSession().getAttribute(CACHE,
PortletSession.PORTLET_SCOPE) : null;
         if (content == null)
         {
+            // System.out.println("WebContentPortlet.doView() >>>fetching content
from: "+sourceURL+", using method: "+sourceMethod+"<<<");
+            
             // Draw the content            
             content = doWebContent(sourceURL, sourceParams, sourceMethod, request, response);
             request.getPortletSession().setAttribute(CACHE, content, PortletSession.PORTLET_SCOPE);
         }
+        // else System.out.println("WebContentPortlet.doView() - Using *cached* content");
 
         // drain the stream to the portlet window
         ByteArrayInputStream bais = new ByteArrayInputStream(content);
@@ -305,26 +309,51 @@
     protected byte[] doWebContent(String sourceAttr, Map sourceParams, String sourceMethod,
RenderRequest request, RenderResponse response)
             throws PortletException
     {
-        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
-        HttpMethodBase httpMethod = null ;
-
+        HttpMethod httpMethod = null ;
+        
         try
         {
             // Set the action and base URLs in the rewriter
             PortletURL action = response.createActionURL();
             ((WebContentRewriter) rewriter).setActionURL(action);
-  
             URL baseURL = new URL(sourceAttr);
             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);
+            httpMethod = getHttpMethod(httpClient, getURLSource(sourceAttr, sourceParams,
request, response), sourceParams, sourceMethod, request);
+            return doHttpWebContent(httpClient, httpMethod, 0, request, response);
+        }
+        catch (PortletException pex)
+        {
+            // already reported
+            throw pex;
+        }
+        catch (Exception ex)
+        {
+            throw new PortletException("Exception while rewritting HTML content. Error: "
+ ex.getMessage());
+        }
+        finally
+        {
+            // release the http connection
+            if (httpMethod != null)
+                httpMethod.releaseConnection();
+        }
+    }
+
+    protected byte[] doHttpWebContent(HttpClient httpClient, HttpMethod httpMethod, int retryCount,
RenderRequest request, RenderResponse response)
+            throws PortletException
+    {
+        try
+        {
+            // Get the input stream from the provided httpClient/httpMethod
+            
+            // ...set up URL and HttpClient stuff
             httpClient.executeMethod(httpMethod);
             
+            // ...reset base URL with fully resolved path (e.g. if a directory, path will
end with a /, which it may not have in the call to this method)
+            rewriter.setBaseUrl((new URL(new URL(rewriter.getBaseUrl()),httpMethod.getPath())).toString());
+            
             // ...save updated state
             Cookie[] cookies = httpClient.getState().getCookies();
             PortletMessaging.publish(request, HTTP_STATE, cookies); 
@@ -338,6 +367,8 @@
                 String redirectLocation = locationHeader != null ? locationHeader.getValue()
: null ;
                 if (redirectLocation != null)
                 {
+                    // System.out.println("WebContentPortlet.doHttpWebContent() >>>handling
redirect to: "+redirectLocation+"<<<");
+                    
                     // one more time (assume most params are already encoded & new URL
is using GET protocol!)
                     return doWebContent( redirectLocation, new HashMap(), "get", request,
response ) ;
                 }
@@ -347,10 +378,40 @@
                     throw new PortletException("Redirection code: "+responseCode+", but with
no redirectionLocation set.");
                 }
             }
+            else if ( responseCode >= 400 )
+            {
+                if ( responseCode == 401 )
+                {
+                    // System.out.println("WebContentPortlet.doHttpWebContent() - handling
authorization request");
+                    
+                    if (retryCount++ < 1 && doAuthorize( httpClient, httpMethod,
request))
+                    {
+                        // try again, now that we are authorizied
+                        return doHttpWebContent(httpClient, httpMethod, retryCount, request,
response);
+                    }
+                    else
+                    {
+                        // could not authorize
+                        throw new PortletException("Site requested authorization, but we
are unable to provide credentials");
+                    }
+                }
+                else if (retryCount++ < 2)
+                {
+                    // System.out.println("WebContentPortlet.doHttpWebContent() - retrying:
"+httpMethod.getPath()+", response code: "+responseCode);
+                    
+                    // retry
+                    return doHttpWebContent(httpClient, httpMethod, retryCount, request,
response);
+                }
+                else
+                {
+                    // bad
+                    throw new PortletException("Failure reading: "+httpMethod.getPath()+",
response code: "+responseCode);
+                }
+            }
             
             // ...ok - *now* create the input stream and reader
             BufferedInputStream bis = new BufferedInputStream(httpMethod.getResponseBodyAsStream());
-            String encoding = httpMethod.getResponseCharSet();
+            String encoding = ((HttpMethodBase)httpMethod).getResponseCharSet();
             if (encoding == null)
                 encoding = getContentCharSet(bis);
             Reader htmlReader = new InputStreamReader(bis, encoding);
@@ -358,11 +419,16 @@
             // get the output buffer
             if (encoding == null)
                 encoding = this.defaultEncoding ;
+            ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
             Writer htmlWriter = new OutputStreamWriter(byteOutputStream, encoding);
 
             // rewrite and flush output
             rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), htmlReader,
htmlWriter);
             htmlWriter.flush();
+
+            // Page has been rewritten
+            // TODO: Write it to cache
+            return byteOutputStream.toByteArray();
         }
         catch (UnsupportedEncodingException ueex)
         {
@@ -376,23 +442,20 @@
         {
             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, Map params, RenderRequest request, RenderResponse
response)
+    
+    protected String getURLSource(String source, Map params, RenderRequest request, RenderResponse
response)
     {
         return source;    
     }
     
+    protected boolean doAuthorize(HttpClient clent,HttpMethod method, RenderRequest request)
+    {
+        // derived class responsibilty - return true, if credentials have been set
+        return false ;
+    }
+
+        
     /*
      * Get WebContent source preference value
      */
@@ -431,18 +494,25 @@
         // reuse existing state, if we have been here before
         Cookie[] cookies = (Cookie[])PortletMessaging.receive(request, HTTP_STATE);
         if (cookies != null)
+        {
+            // ...so far, just saving cookies - may need a more complex Serializable object
here
             client.getState().addCookies(cookies);
+
+            // System.out.println("WebContentPortlet.getHttpClient() - reusing: "+cookies.length+",
cookies...");
+            // for(int i=0,limit = cookies.length; i<limit; i++) System.out.println("...cookie["+i+"]
is: "+cookies[i]);
+        }
  
         return client ;
     }
     
-    protected HttpMethodBase getHttpMethod(String uri, Map params, String method, RenderRequest
request) throws IOException
+    protected HttpMethodBase getHttpMethod(HttpClient client, 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);
+            // System.out.println("WebContentPortlet.getHttpMethod() - HTTP GET from URL:
"+uri);
+            
             // http GET
             httpMethod = new GetMethod(uri);
             if (params != null && !params.isEmpty())
@@ -457,7 +527,7 @@
                     if (values != null)
                         for (int i = 0,limit = values.length; i < limit; i++)
                         {
-                            // System.out.println("...adding GET parameter: "+name+", with
value: "+values[i]);
+                            // System.out.println("...adding >>>GET parameter: "+name+",
with value: "+values[i]+"<<<");
                             pairs.add(new NameValuePair(name, values[i]));
                         }
                 }
@@ -469,7 +539,8 @@
         }
         else
         {
-            // System.out.println(">>>>>>>>>>>> HTTP
POST to URL: "+uri);
+            // System.out.println("WebContentPortlet.getHttpMethod() - HTTP POST to URL:
"+uri);
+            
             // http POST
             PostMethod postMethod = (PostMethod)( httpMethod = new PostMethod(uri)) ; 
             if (params != null && !params.isEmpty())
@@ -483,7 +554,8 @@
                     if (values != null)
                         for (int i=0,limit=values.length; i<limit; i++)
                         {
-                            // System.out.println("...adding POST parameter: "+name+", with
value: "+values[i]);
+                            // System.out.println("...adding >>>POST parameter:
"+name+", with value: "+values[i]+"<<<");
+                            
                             postMethod.addParameter(name, values[i]);
                         }
                 }   
@@ -493,7 +565,7 @@
         // 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!!!
+        // BOZO - DON'T do this.   default policy seems to be more flexible!!!
         //httpMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
         
         // ...ready to use!

Modified: portals/jetspeed-2/trunk/core-build.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/core-build.xml?rev=390958&r1=390957&r2=390958&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/core-build.xml (original)
+++ portals/jetspeed-2/trunk/core-build.xml Sun Apr  2 22:48:07 2006
@@ -288,7 +288,7 @@
 		</dependency>
 	    <dependency>
 	      <id>nekohtml</id>
-	      <version>0.9.3</version>
+	      <version>0.9.5</version>
 	      <properties>
 	        <war.bundle>true</war.bundle>
 	      </properties>

Modified: portals/jetspeed-2/trunk/pom.xml
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/pom.xml?rev=390958&r1=390957&r2=390958&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/pom.xml (original)
+++ portals/jetspeed-2/trunk/pom.xml Sun Apr  2 22:48:07 2006
@@ -162,7 +162,7 @@
         <lucene.version>1.4.1</lucene.version>
         <mockrunner.version>0.2.7</mockrunner.version>
         <myfaces.version>1.1.0</myfaces.version>
-        <nekohtml.version>0.9.3</nekohtml.version>
+        <nekohtml.version>0.9.5</nekohtml.version>
         <ojb.version>1.0.3</ojb.version>
         <org.apache.derby.version>10.1.1.0</org.apache.derby.version>
         <org.apache.maven.artifact.ant.version>2.0.1</org.apache.maven.artifact.ant.version>



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message