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 ro...@apache.org on 2004/12/02 02:06:27 UTC

cvs commit: jakarta-jetspeed-2/components/web-content project.xml

rogerrut    2004/12/01 17:06:27

  Modified:    components/web-content project.xml
  Added:       components/web-content/src/java/org/apache/jetspeed/portlet
                        WebContentPortlet.java
  Log:
  Initial version of WebContentPortlet & WebContentRewriter
  --> needs work on the rewriter/SSO/Preferences
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/components/web-content/src/java/org/apache/jetspeed/portlet/WebContentPortlet.java
  
  Index: WebContentPortlet.java
  ===================================================================
  /* Copyright 2004 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.portlet;
  
  import java.io.IOException;
  import java.util.Arrays;
  
  import javax.portlet.ActionRequest;
  import javax.portlet.ActionResponse;
  import javax.portlet.PortletConfig;
  import javax.portlet.PortletException;
  import javax.portlet.PortletPreferences;
  import javax.portlet.PortletSession;
  import javax.portlet.RenderRequest;
  import javax.portlet.RenderResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletRequestWrapper;
  
  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
  
  import org.apache.jetspeed.rewriter.JetspeedRewriterController;
  import org.apache.jetspeed.rewriter.RewriterController;
  import org.apache.jetspeed.rewriter.RewriterException;
  import org.apache.jetspeed.rewriter.RulesetRewriterImpl;
  import org.apache.jetspeed.rewriter.WebContentRewriter;
  import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
  import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
  
  //standard java stuff
  import java.io.ByteArrayOutputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.UnsupportedEncodingException;
  import java.io.Writer;
  import java.net.MalformedURLException;
  
  import java.io.Reader;
  
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.StringTokenizer;
  
  
  
  /**
   * WebContentPortlet
   *
   * TODO:
   *  *
   * @author <a href="mailto:rogerrutr@apache.org">Roger Ruttimann</a>
   * @version $Id: WebContentPortlet.java,v 1.1 2004/12/02 01:06:27 rogerrut Exp $
   */
  
  public class WebContentPortlet extends GenericVelocityPortlet {
  
      /**
       * WebContentPortlet
       * Allows navigation inside the portlet and caches the latest URL
       */
      
      /**
       * Configuration constants.
       */
      public static final String VIEW_SOURCE_PARAM = "viewSource";
      public static final String EDIT_SOURCE_PARAM = "editSource";
  
      /**
      * Default WebContent  source attribute members.
      */
     private String defaultViewSource;
     private String defaultEditSource;
     
     /**
      * Action Parameter
      */
     
     /** WebContent Session Parameter */
     public static final String SESSION_PARAMETER = "WCSP";
     
     /** Default encoding*/
     public String         defaultEncoding = "iso-8859-1";
     
     /* Internal Cache */
     private String lastURL = null;
     
     /* SSO settings */
     boolean isSSOEnabled = false;
     
     /* WebContent rewriter */
     WebContentRewriter rewriter = new WebContentRewriter();
     RewriterController rewriteController = null;
  
      public WebContentPortlet() {
          super();
         
         }
      
      /**
       * Initialize portlet configuration.
       */
      public void init(PortletConfig config)
          throws PortletException
      {
          super.init(config);
   
          defaultViewSource = config.getInitParameter(VIEW_SOURCE_PARAM);
          if (defaultViewSource == null)
              defaultViewSource = "http://www.apache.org";
          
          defaultEditSource = config.getInitParameter(EDIT_SOURCE_PARAM);
       }
      
      /**
       * processAction()
       * Checks action initiated by the WebContent portlet which means that a user has clicked on an URL
       * @param actionRequest
       * @param actionResponse
       * @throws PortletException
       * @throws IOException
       */
      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);
      		
      	/*
      	 * If the webContentParameter is not empty attach the URL to the session
      	 */
      	if ( webContentParameter != null && webContentParameter.length() > 0)
      	{
       		String sessionObj = new String(webContentParameter);
      		actionRequest.getPortletSession().setAttribute(WebContentPortlet.SESSION_PARAMETER, sessionObj, PortletSession.APPLICATION_SCOPE);
      	}
  	}
      
      /**
       * doView
       * Renders the URL in the following order 1) SESSION_PARAMETER 2)cached version 3) defined for preference SRC
       */
      public void doView(RenderRequest request, RenderResponse response)
      throws PortletException, IOException
  	{
          // Find the source URL to execute
          String sourceURL = null;
          
          // Check if the source was defined in the session
          try
  		{
              sourceURL = (String)request.getPortletSession().getAttribute(WebContentPortlet.SESSION_PARAMETER,  PortletSession.APPLICATION_SCOPE);
  		}
      	catch (Exception e )
  		{
      	    sourceURL = null;
  		}
      	
      	// Check if the page was rendered at least once
      	if (sourceURL == null &&  lastURL != null )
      	{
      	    // Use the cache
      	    sourceURL = lastURL;
      	}
      	else
      	{
      	    // Use the URL defined in the preferences
      	    sourceURL = defaultViewSource;
      	}
      	
      	// 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.");
      	
      	// Initialize the controller if it's not already done
      	if ( rewriteController == null)
      	{
      	    // Extract context path
      	    String pathTranslated = ((HttpServletRequest)((HttpServletRequestWrapper) request).getRequest()).getPathTranslated();
          	String contextPath =   request.getContextPath();
          	
          	contextPath = pathTranslated.substring(0, pathTranslated.indexOf("webapps") + 7) + contextPath + "/WEB-INF/";
          	
          	try
              {
      	        // Create rewriter adaptor
      	        rewriteController = getController(contextPath);
              }
              catch(Exception e)
              {
                  // Failed to create rewriter controller
                  throw new PortletException("WebContentProtlet failed to create rewriter controller. Error:" + e.getMessage() );
              }
      	}
      	
      	// Set the content type
      	response.setContentType("text/html");
      	// Draw the content
      	response.getWriter().println(doWebContent(request, sourceURL, response));
      	
      	// Done just save the last URL
      	lastURL = sourceURL;
     
  	}
      
      /*
       * Privaye helpers for generating WebContent
       */
      protected String doWebContent(RenderRequest request, String sourceAttr, RenderResponse response)
          throws PortletException
      {
          // Initialization
          Writer htmlWriter = null;
          
          // Rewriter
          ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
          
          Reader htmlReader = getReader( sourceAttr );
          
          try
          {
              htmlWriter = new OutputStreamWriter(byteOutputStream, this.defaultEncoding);
          
              rewriter.rewrite(rewriteController.createParserAdaptor("text/html"), getReader(sourceAttr), htmlWriter);
          }
          catch(UnsupportedEncodingException ueex)
          {
              throw new PortletException("Encoding " + defaultEncoding + " not supported. Error: " + ueex.getMessage());
          }
          catch(RewriterException rwe)
          {
              throw new PortletException("Failed to rewrite HTML page. Error: " + rwe.getMessage());
          }
          catch(Exception e)
          {
              throw new PortletException("Exception while rewritting HTML page. Error: " + e.getMessage());
          }
          
          // Page has been rewritten
          // TODO: Write it to cache
          return byteOutputStream.toString();
      }
      
      /*
      * Get WebContent source preference value
      */
     private String getSourcePreference(RenderRequest request, String name, String defaultValue)
     {
         PortletPreferences prefs = request.getPreferences();
         return ((prefs != null) ? prefs.getValue(name, defaultValue) : defaultValue);
     }
     
     /*
      * Generate a rewrite controller using the basic rules file
      */
     private RewriterController getController(String contextPath) throws Exception
     {        
         Class[] rewrtierClasses = new Class[]{WebContentRewriter.class, RulesetRewriterImpl.class};
         Class[] adaptorClasses = new Class[]{SwingParserAdaptor.class, SaxParserAdaptor.class};
         return new JetspeedRewriterController(contextPath + "conf/rewriter-rules-mapping.xml", Arrays.asList(rewrtierClasses), Arrays.asList(adaptorClasses));
     }
     
     /*
      * getReaderForURL()
      * Streams the page from the uRL into the reader
      */
     protected Reader getReader(String url) throws PortletException
     {
         URL						pageUrl = null;
         URLConnection	pageConn = null;
         
         // Open the connection to the page
         try
         {
             pageUrl = new URL(url);
             pageConn = pageUrl.openConnection();
             
             if (this.isSSOEnabled == true)
             {
  	           /* 
  	            * 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));
  	           }
  	           */
             }
         }
         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)
         {
             StringTokenizer st = new StringTokenizer(contentType, "; =");
             while (st.hasMoreTokens())
             {
                 if (st.nextToken().equalsIgnoreCase("charset"))
                 {
                     try
                     {
                         encoding = st.nextToken();
                         break;
                     }
                     catch (Exception e)
                     {
                         break;
                     }
                 }
             }
         }
  
         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;
     }
  
  }
  
  
  
  1.7       +4 -0      jakarta-jetspeed-2/components/web-content/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed-2/components/web-content/project.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- project.xml	1 Dec 2004 17:06:30 -0000	1.6
  +++ project.xml	2 Dec 2004 01:06:27 -0000	1.7
  @@ -40,6 +40,10 @@
     	&spring-deps;
     	&commons-config-dep;
     	
  +  	<dependency>
  +      <id>jetspeed2:jetspeed-components</id>
  +      <version>2.0-a1-dev</version>
  +    </dependency>
      	<dependency>
         <id>jetspeed2:jetspeed-commons</id>
         <version>2.0-a1-dev</version>
  
  
  

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