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 ta...@apache.org on 2006/10/05 23:38:13 UTC

svn commit: r453394 - in /portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed: layout/ajax-xml/ layout/impl/ profiler/impl/ request/

Author: taylor
Date: Thu Oct  5 14:38:13 2006
New Revision: 453394

URL: http://svn.apache.org/viewvc?view=rev&rev=453394
Log:
Two new features:

1. Create User Pages from Roles on first login feature
   The CreatePageValve creates a new user's home page from the user's role pages

2. Ajax Multi-action - contribution from David Gurney
   Execute n ajax actions in one request

Added:
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiaction.vm
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiactionerror.vm
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/MultipleAction.java
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/profiler/impl/CreatePageValveImpl.java
Modified:
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/PortletActionSecurityPathBehavior.java
    portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/request/JetspeedRequestContext.java

Added: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiaction.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiaction.vm?view=auto&rev=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiaction.vm (added)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiaction.vm Thu Oct  5 14:38:13 2006
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<js>
+    <status>$status</status>
+    <action>$action</action>
+    
+    <results>
+#foreach ($result in $results)    
+        $result
+#end        
+    </results>
+    
+</js>

Added: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiactionerror.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiactionerror.vm?view=auto&rev=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiactionerror.vm (added)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/ajax-xml/multiactionerror.vm Thu Oct  5 14:38:13 2006
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<js>
+    <status>$status</status>
+    <action>$action</action>
+    <reason>$reason</reason>
+</js>
\ No newline at end of file

Added: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/MultipleAction.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/MultipleAction.java?view=auto&rev=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/MultipleAction.java (added)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/MultipleAction.java Thu Oct  5 14:38:13 2006
@@ -0,0 +1,302 @@
+package org.apache.jetspeed.layout.impl;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.jetspeed.ajax.AJAXException;
+import org.apache.jetspeed.ajax.AjaxAction;
+import org.apache.jetspeed.ajax.AjaxBuilder;
+import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
+import org.apache.jetspeed.layout.impl.BasePortletAction;
+import org.apache.jetspeed.layout.impl.Constants;
+import org.apache.jetspeed.page.PageManager;
+import org.apache.jetspeed.request.JetspeedRequestContext;
+import org.apache.jetspeed.request.RequestContext;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
+
+/**
+ * 
+ * @author David Gurney
+ * 
+ * The purpose of this object is to run several AJAX actions and aggregate the
+ * results into a single response. This is useful when the client needs to make
+ * more than one call as the result of a single user action.
+ * 
+ * The sample request URL is shown below:
+ * 
+ * http://host:port/ajaxapi?action=multiple&commands=(action;name,value;name,value)(action;name,value)
+ * 
+ * The constructor accepts a map of the actions that are available to be run.
+ * The name,value pairs are parameter values needed by the action. The actions
+ * are run in the order that they are found on the URL string
+ * 
+ */
+public class MultipleAction extends BasePortletAction implements AjaxAction,
+        AjaxBuilder
+{
+
+    protected static final String ALL_RESULTS = "results";
+
+    protected static final String BUILD_RESULTS = "buildresults";
+
+    protected static final String MULTIPLE_ACTION_PROCESSOR = "Multiple Action Processor";
+
+    protected static final String COMMANDS = "commands";
+
+    protected static final String COMMAND_TOKEN = ")";
+
+    protected static final String PARAM_TOKEN = ";";
+
+    protected static final String VALUE_TOKEN = ",";
+
+    protected Map m_sActionMap = null;
+
+    protected VelocityEngine m_oVelocityEngine = null;
+
+    public MultipleAction(Map p_sActionMap, String p_sTemplate,
+            String p_sErrorTemplate, PageManager p_oPageManager,
+            PortletActionSecurityBehavior p_oSecurityBehavior,
+            VelocityEngine p_oVelocityEngine)
+    {
+        super(p_sTemplate, p_sErrorTemplate, p_oPageManager,
+                p_oSecurityBehavior);
+        m_sActionMap = p_sActionMap;
+        m_oVelocityEngine = p_oVelocityEngine;
+    }
+
+    public boolean run(RequestContext p_oRequestContext, Map p_oResultMap)
+            throws AJAXException
+    {
+        boolean a_bReturnSuccess = true;
+        List a_oResultArray = new ArrayList();
+
+        p_oResultMap.put(ACTION, "multiple");
+        p_oResultMap.put(STATUS, "success");
+
+        // Get the command string
+        String a_sCommands = p_oRequestContext.getRequestParameter(COMMANDS);
+        if (a_sCommands == null || a_sCommands.length() <= 0)
+        {
+            buildErrorContext(p_oRequestContext, p_oResultMap);
+            p_oResultMap.put(STATUS, "failure");
+            p_oResultMap.put(REASON, "command parameters not found");
+
+            throw new AJAXException("command parameters not found");
+        }
+
+        // Tokenize the commands into single commands
+        StringTokenizer a_oCommandTok = new StringTokenizer(a_sCommands,
+                COMMAND_TOKEN);
+
+        // Process each command
+        while (a_oCommandTok.hasMoreTokens())
+        {
+            // Get the token
+            String a_sCommand = a_oCommandTok.nextToken();
+
+            // Strip off the opening (
+            a_sCommand = a_sCommand.substring(1);
+
+            // Tokenize the single commands into parameters
+            StringTokenizer a_oParamTok = new StringTokenizer(a_sCommand,
+                    PARAM_TOKEN);
+            if (a_oParamTok == null || a_oParamTok.hasMoreTokens() == false)
+            {
+                buildErrorContext(p_oRequestContext, p_oResultMap);
+                p_oResultMap.put(STATUS, "failure");
+                p_oResultMap.put(REASON, "incorrect url request");
+
+                throw new AJAXException("incorrect url request");
+            }
+
+            // Get the action - which is the first item in the list
+            String a_sAction = a_oParamTok.nextToken();
+
+            // Lookup the action from the action map
+            Object a_oActionObject = m_sActionMap.get(a_sAction);
+            if (a_oActionObject == null
+                    && !(a_oActionObject instanceof AjaxAction))
+            {
+                buildErrorContext(p_oRequestContext, p_oResultMap);
+                p_oResultMap.put(REASON, "unknown action requested==>"
+                        + a_sAction);
+
+                throw new AJAXException("unknown action requested==>"
+                        + a_sAction);
+            }
+
+            AjaxAction a_oAction = (AjaxAction) a_oActionObject;
+
+            JetspeedRequestContext a_oJetspeedRequestContext = (JetspeedRequestContext) p_oRequestContext;
+
+            // Process each parameter for this action
+            while (a_oParamTok.hasMoreTokens())
+            {
+                String a_sName = a_oParamTok.nextToken(VALUE_TOKEN);
+                // Strip of the leading ; if present
+                if (a_sName.indexOf(';') >= 0)
+                {
+                    a_sName = a_sName.substring(1);
+                }
+
+                String a_sValue = a_oParamTok.nextToken();
+
+                // Put the parameters on the request context
+                a_oJetspeedRequestContext
+                        .setRequestParameter(a_sName, a_sValue);
+            }
+
+            // Invoke the action
+            Map a_oResultMap = new HashMap();
+            boolean a_bSuccess;
+
+            try
+            {
+                a_bSuccess = a_oAction.run(a_oJetspeedRequestContext,
+                        a_oResultMap);
+            } catch (Exception e)
+            {
+                // Move the reason into the return map
+                p_oResultMap.put(REASON, a_oResultMap.get(REASON));
+
+                throw new AJAXException(e);
+            }
+
+            // Check for success
+            if (a_bSuccess)
+            {
+                // Invoke the builder for this action if possible
+                if (a_oAction instanceof AjaxBuilder)
+                {
+                    processBuilder((AjaxBuilder) a_oAction, a_oResultMap,
+                            p_oRequestContext, a_bSuccess);
+                }
+
+                // Get the build results
+                String a_sBuildResults = (String) a_oResultMap
+                        .get(BUILD_RESULTS);
+
+                // Look for an xml tag and strip it off
+                int a_iStartIndex = a_sBuildResults.indexOf("<?xml");
+                if (a_iStartIndex >= 0)
+                {
+                    // Look for the end of the tag
+                    int a_iEndIndex = a_sBuildResults.indexOf(">",
+                            a_iStartIndex);
+                    if (a_iEndIndex >= 0)
+                    {
+                        String a_sStart = a_sBuildResults.substring(0,
+                                a_iStartIndex);
+                        String a_sEnd = a_sBuildResults.substring(
+                                a_iEndIndex + 1, a_sBuildResults.length());
+                        a_sBuildResults = a_sStart + a_sEnd;
+                    }
+                }
+
+                if (a_sBuildResults != null)
+                {
+                    // Save the results
+                    a_oResultArray.add(a_sBuildResults);
+                }
+            } else
+            {
+                // Move the reason into the return map
+                p_oResultMap.put(REASON, a_oResultMap.get(REASON));
+
+                // Exit the loop
+                a_bReturnSuccess = false;
+                break;
+            }
+        }
+
+        // Save the results for later building into the response
+        p_oResultMap.put(ALL_RESULTS, a_oResultArray);
+
+        return a_bReturnSuccess;
+    }
+
+    // Process the builder if provided
+    protected void processBuilder(AjaxBuilder p_oBuilder, Map p_oInputMap,
+            RequestContext p_oRequestContext, boolean p_oActionSuccessFlag)
+    {
+        try
+        {
+            // Ask the builder to construct the context
+            // Add the input map to the velocity context
+            boolean result = true;
+
+            if (p_oActionSuccessFlag == true)
+            {
+                result = p_oBuilder
+                        .buildContext(p_oRequestContext, p_oInputMap);
+            } else
+            {
+                result = p_oBuilder.buildErrorContext(p_oRequestContext,
+                        p_oInputMap);
+            }
+
+            Context a_oContext = new VelocityContext(p_oInputMap);
+
+            // Check to see if we have a valid context
+            if (result)
+            {
+                // Get the name of the template from the builder
+                String a_sTemplateName = null;
+
+                if (p_oActionSuccessFlag == true)
+                {
+                    a_sTemplateName = p_oBuilder.getTemplate();
+                } else
+                {
+                    a_sTemplateName = p_oBuilder.getErrorTemplate();
+                }
+
+                // Get a reader to the velocity template
+                final InputStream a_oTemplateStream = this.getClass()
+                        .getClassLoader().getResourceAsStream(a_sTemplateName);
+
+                Reader a_oTemplate = new InputStreamReader(a_oTemplateStream);
+
+                // The results of the velocity template will be stored here
+                StringWriter a_oStringWriter = new StringWriter();
+
+                // Run the velocity template
+                m_oVelocityEngine.evaluate(a_oContext, a_oStringWriter,
+                        MULTIPLE_ACTION_PROCESSOR, a_oTemplate);
+
+                // Get the results from the velocity processing
+                String a_sResults = a_oStringWriter.getBuffer().toString();
+
+                // Save the results on the input map
+                p_oInputMap.put(BUILD_RESULTS, a_sResults);
+            } else
+            {
+                log.error("could not create builder context");
+            }
+        } catch (Exception e)
+        {
+            log.error("builder failed", e);
+            p_oInputMap.put(Constants.REASON, e.toString());
+        }
+    }
+
+    public boolean buildContext(RequestContext p_oRequestContext,
+            Map p_oInputMap)
+    {
+        boolean a_bResults = true;
+
+        a_bResults = super.buildContext(p_oRequestContext, p_oInputMap);
+
+        return a_bResults;
+    }
+
+}

Modified: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/PortletActionSecurityPathBehavior.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/PortletActionSecurityPathBehavior.java?view=diff&rev=453394&r1=453393&r2=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/PortletActionSecurityPathBehavior.java (original)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/layout/impl/PortletActionSecurityPathBehavior.java Thu Oct  5 14:38:13 2006
@@ -23,6 +23,7 @@
 import org.apache.jetspeed.om.page.ContentPageImpl;
 import org.apache.jetspeed.om.page.Page;
 import org.apache.jetspeed.page.PageManager;
+import org.apache.jetspeed.profiler.impl.ProfilerValveImpl;
 import org.apache.jetspeed.request.RequestContext;
 
 /**
@@ -73,6 +74,7 @@
                                                 + Folder.PATH_SEPARATOR 
                                                 + Folder.FALLBACK_DEFAULT_PAGE);                 
                 context.setPage(new ContentPageImpl(page));
+                context.getRequest().getSession().removeAttribute(ProfilerValveImpl.PORTAL_SITE_SESSION_CONTEXT_ATTR_KEY);                
             }            
         }
         catch (Exception e)

Added: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/profiler/impl/CreatePageValveImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/profiler/impl/CreatePageValveImpl.java?view=auto&rev=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/profiler/impl/CreatePageValveImpl.java (added)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/profiler/impl/CreatePageValveImpl.java Thu Oct  5 14:38:13 2006
@@ -0,0 +1,62 @@
+/*
+ * 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.profiler.impl;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
+import org.apache.jetspeed.pipeline.PipelineException;
+import org.apache.jetspeed.pipeline.valve.AbstractValve;
+import org.apache.jetspeed.pipeline.valve.Valve;
+import org.apache.jetspeed.pipeline.valve.ValveContext;
+import org.apache.jetspeed.request.RequestContext;
+
+/**
+ * ProfilerValveImpl
+ * 
+ * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
+ * @version $Id: ProfilerValveImpl.java 388733 2006-03-25 09:03:58Z rwatler $
+ */
+public class CreatePageValveImpl extends AbstractValve implements Valve
+{
+    protected Log log = LogFactory.getLog(CreatePageValveImpl.class);
+    
+    private PortletActionSecurityBehavior securityBehavior;
+
+    /**
+     * CreatePageValveImpl - constructor
+     *
+     * @param securityBehavior the behavior to create new page for new user
+     */
+    public CreatePageValveImpl(PortletActionSecurityBehavior behavior)
+    {
+        this.securityBehavior = behavior;
+    }
+
+    
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext,
+     *      org.apache.jetspeed.pipeline.valve.ValveContext)
+     */
+    public void invoke( RequestContext request, ValveContext context ) throws PipelineException
+    {
+        securityBehavior.createNewPageOnEdit(request);
+        context.invokeNext(request);
+    }
+
+}

Modified: portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/request/JetspeedRequestContext.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/request/JetspeedRequestContext.java?view=diff&rev=453394&r1=453393&r2=453394
==============================================================================
--- portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/request/JetspeedRequestContext.java (original)
+++ portals/jetspeed-2/trunk/components/portal/src/java/org/apache/jetspeed/request/JetspeedRequestContext.java Thu Oct  5 14:38:13 2006
@@ -137,7 +137,7 @@
     {
         return request;
     }
-
+    
     public HttpServletResponse getResponse()
     {
         return response;
@@ -410,6 +410,11 @@
     public String getRequestParameter( String key )
     {
         return request.getParameter(key);
+    }
+    
+    public void setRequestParameter(String key, String value)
+    {
+        request.getParameterMap().put(key, value);
     }
 
     /**



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