portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jf...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/viewprocessor BufferedVelocityViewProcessor.java BufferedJSPViewProcessor.java ViewProcessorFactory.java
Date Sat, 02 Apr 2005 17:24:04 GMT
jford       2005/04/02 09:24:04

  Modified:    src/java/org/apache/jetspeed/portal/portlets/viewprocessor
                        ViewProcessorFactory.java
  Added:       src/java/org/apache/jetspeed/portal/portlets/viewprocessor
                        BufferedVelocityViewProcessor.java
                        BufferedJSPViewProcessor.java
  Log:
  Added new buffered view processors
  
  PR: JS1-541
  
  Revision  Changes    Path
  1.4       +10 -0     jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/viewprocessor/ViewProcessorFactory.java
  
  Index: ViewProcessorFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/viewprocessor/ViewProcessorFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ViewProcessorFactory.java	23 Feb 2004 03:27:18 -0000	1.3
  +++ ViewProcessorFactory.java	2 Apr 2005 17:24:04 -0000	1.4
  @@ -61,11 +61,21 @@
               logger.info("ViewProcessorFactory - creating Velocity processor");
               viewProcessor = new VelocityViewProcessor();
             }
  +        else if (viewType.equals("BufferedVelocity"))
  +        {
  +          logger.info("ViewProcessorFactory - creating Velocity processor");
  +          viewProcessor = new BufferedVelocityViewProcessor();
  +        }
           else if (viewType.equals("JSP"))
             {
               logger.info("ViewProcessorFactory - creating JSP processor");
               viewProcessor = new JSPViewProcessor();
             }
  +        else if (viewType.equals("BufferedJSP"))
  +        {
  +          logger.info("ViewProcessorFactory - creating JSP processor");
  +          viewProcessor = new BufferedJSPViewProcessor();
  +        }
           else if (viewType.equals("XSL"))
             {
               logger.info("ViewProcessorFactory - creating XSL processor");
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/viewprocessor/BufferedVelocityViewProcessor.java
  
  Index: BufferedVelocityViewProcessor.java
  ===================================================================
  /*
   * Copyright 2005 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.portal.portlets.viewprocessor;
  
  import org.apache.jetspeed.portal.Portlet;
  import org.apache.jetspeed.portal.PortletException;
  import org.apache.jetspeed.portal.portlets.*;
  import org.apache.jetspeed.portal.portlets.GenericMVCContext;
  import org.apache.jetspeed.services.TemplateLocator;
  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
  import org.apache.jetspeed.services.logging.JetspeedLogger;
  import org.apache.jetspeed.util.JetspeedClearElement;
  
  import org.apache.turbine.services.velocity.TurbineVelocity;
  import org.apache.turbine.util.RunData;
  
  
  /**
   * Version of the VelocityViewProcess that buffers its content
   *
   * @author <a href="mailto:tkuebler@cisco.com">Tod Kuebler</a>
   * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
   */
  public class BufferedVelocityViewProcessor implements ViewProcessor
  {
  
      /**
       * Static initialization of the logger for this class
       */    
      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BufferedVelocityViewProcessor.class.getName());
      
      /** Creates a new instance of VelocityViewProcessor */
      public BufferedVelocityViewProcessor()
        {
        }
  
      public void init(Portlet portlet)
                throws PortletException
        {
        }
  
      /** Process the template passed in the context
       * (context.get("template")).  Invoked by the GenericMVCPortlet
       * after action handling to process the template type
       * in question.
       *
       */
      public Object processView(GenericMVCContext context)
        {
  
          // generate the content
          JetspeedClearElement element = null;
          String template = (String) context.get("template");
          logger.info("VelocityViewProcessor - processing " + template);
  
          try
            {
  
              if (-1 == template.indexOf(".vm"))
                {
                  template = template + ".vm";
                }
  
              logger.info("VelocityViewProcessor - locating template - " + 
                       ((RunData) context.get("data")).toString() + template);
  
              String templatePath = TemplateLocator.locatePortletTemplate(
                                            (RunData) context.get("data"), 
                                            template);
  
              // need to add cache support
              Portlet portlet = (Portlet) context.get("portlet");
              RunData rundata = (RunData) context.get("data");
              long cachePeriod = -1;
              AbstractPortlet abstractPortlet = null;
              // STW: Safety net ;)
              if(portlet instanceof AbstractPortlet)
              {
              	abstractPortlet =(AbstractPortlet) portlet;
              	if(abstractPortlet.getExpirationMillis() != null)
              	{
              		cachePeriod = abstractPortlet.getExpirationMillis().longValue();
              	}
              }
             
  			if (cachePeriod > 0 && abstractPortlet != null)
  			{
  				String s = TurbineVelocity.handleRequest(context, templatePath);
  				abstractPortlet.setExpirationMillis(
  					cachePeriod + System.currentTimeMillis());
  				element = new JetspeedClearElement(s);
  
  			}
  			else
  			{
  				String s = TurbineVelocity.handleRequest(
  					context, templatePath);
  				element = new JetspeedClearElement(s);
  			}
              
              
            }
          catch (Exception e)
            {
              element = new JetspeedClearElement(e.toString());
              logger.error("VelocityViewProcessor - had problems handling request - " + e);
              e.printStackTrace();
            }
  
          TurbineVelocity.requestFinished(context);
  
          if (element == null)
            {
              element = new JetspeedClearElement("");
            }
  
          return element;
        }
    }
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/portal/portlets/viewprocessor/BufferedJSPViewProcessor.java
  
  Index: BufferedJSPViewProcessor.java
  ===================================================================
  /*
   * Copyright 2005 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.portal.portlets.viewprocessor;
  
  // Ecs
  import org.apache.ecs.StringElement;
  
  // Jetspeed portal
  import org.apache.jetspeed.portal.Portlet;
  import org.apache.jetspeed.portal.portlets.GenericMVCContext;
  import org.apache.jetspeed.services.TemplateLocator;
  import org.apache.jetspeed.services.Registry;
  import org.apache.jetspeed.services.jsp.HttpBufferedResponse;
  import org.apache.jetspeed.services.jsp.JetspeedJspService;
  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
  import org.apache.jetspeed.services.logging.JetspeedLogger;
  import org.apache.jetspeed.om.registry.PortletEntry;
  import org.apache.jetspeed.util.JetspeedClearElement;
  import org.apache.jetspeed.util.ServiceUtil;
  
  // Turbine stuff
  import org.apache.turbine.services.jsp.JspService;
  
  // Turbine util
  import org.apache.turbine.util.RunData;
  
  //java stuff
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.util.Iterator;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.RequestDispatcher;
  
  /**
   * Version of the JSPViewProcess that buffers its content
   * 
   * @author <a href="mailto:tkuebler@cisco.com">Tod Kuebler</a>
   * @author <a href="mailto:weaver@apache.org">Scott Weaver</a>
   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
   * @author <a href="mailto:jford@apache.org">Jeremy Ford</a>
   * @version $Id: BufferedJSPViewProcessor.java,v 1.1 2005/04/02 17:24:04 jford Exp $
   */
  public class BufferedJSPViewProcessor
  implements ViewProcessor
  {
  
      /**
       * Static initialization of the logger for this class
       */    
      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BufferedJSPViewProcessor.class.getName());
      
      /** Creates a new instance of JSPViewProcessor */
      public BufferedJSPViewProcessor()
      {
      }
  
      public Object processView(GenericMVCContext context)
      {
      	String result = "";
      	
          Portlet portlet = (Portlet) context.get("portlet");
          RunData data = (RunData) context.get("data");
          HttpServletRequest request = data.getRequest();
          String template = (String) context.get("template");
          logger.info("JSPViewProcessor - processing template " + template);
  
          try
          {
  
              // Allow access to portlet from .jsp template
              request.setAttribute("portlet", portlet);
  
              // put context in attribute so you can get to it from .jsp template
              request.setAttribute("context", context);
  
              // Add js_peid out of convenience
              request.setAttribute("js_peid", portlet.getID());
  
              // Add rundata out of convenience (JspService.RUNDATA differs from GenericMVCPortlet.RUNDATA)
              request.setAttribute(JspService.RUNDATA, data);
  
              // Retrieve the URL. For backward compatibility, use the URL first 
              // and then fallback to "template" parameter
              PortletEntry pe = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
  
              // Files referenced from default templates folder will be processed
              // using JspService. Otherwise, they will be loaded using EcsServletElement
              // from where ever they came from.
              if (pe.getURL() == null || pe.getURL().trim().length() == 0)
              {
  
                  if (template != null && -1 == template.indexOf(".jsp"))
                  {
                      template = template + ".jsp";
                  }
  
                  logger.info("JSPViewProcessor - locating template - " + data.toString()

                           + " - " + template);
  
                  //we use the template locator to translate the template
                  String locatedTemplate = TemplateLocator.locatePortletTemplate(data, template);
                  logger.info("JSPViewProcessor - located template: " + locatedTemplate);
  
                  /*if (locatedTemplate == null)
                  {
                      locatedTemplate = TemplateLocator.locateScreenTemplate(data, template);
                      if (locatedTemplate != null)
                      {
                          locatedTemplate = "/screens" + locatedTemplate;
                      }
                      logger.debug("JSPViewProcessor - located screen template: " + locatedTemplate);
                  } */
  
                  JetspeedJspService service = (JetspeedJspService) ServiceUtil.getServiceByName(JspService.SERVICE_NAME);
  
                  // this is only necessary if we don't run in a JSP page environment
                  // but better be safe than sorry...
                  service.addDefaultObjects(data);
  
                  // handle request
                  result = service.handleBufferedRequest(data, locatedTemplate, false);
  
              }
              else
              {
                  // Build parameter list to be passed with the jsp
                  Iterator names = portlet.getPortletConfig().getInitParameterNames();
                  while (names.hasNext()) 
                  {
                      String name = (String) names.next();
                      String value = (String) portlet.getPortletConfig().getInitParameter(name);
                      data.getParameters().setString(name, value);
                  }
  
                  template = pe.getURL();
  
                  if (logger.isDebugEnabled())
                  {
                      logger.debug("JSPViewProcessor - serving jsp directly using: " + template);
                  }
  
                  // get the RequestDispatcher for the JSP
                  StringWriter writer = new StringWriter();
              	HttpBufferedResponse response = new HttpBufferedResponse(data.getResponse(),
new PrintWriter(writer));
              	
                  RequestDispatcher dispatcher = data.getServletContext().getRequestDispatcher(template);
                  data.getOut().flush();
                  dispatcher.include(data.getRequest(), response);
                  
                  result = writer.toString();
              }
  
          }
          catch (Exception e)
          {
  
              String message = "JSPViewProcessor: Could not include the following JSP Page:
 [" + template + "] :\n\t" 
                               + e.getMessage();
              logger.error(message, e);
  
              return new StringElement(message);
          }
  
          return new JetspeedClearElement(result);
      }
  
      /** Process the template passed in the context
       * (context.get("template")).  Invoked by the GenericMVCPortlet
       * after action handling to process the template type
       * in question.
       *
       */
      public void init(Portlet portlet)
      {
      }
  }
  
  
  

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


Mime
View raw message