portals-pluto-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msnickl...@apache.org
Subject [22/50] [abbrv] portals-pluto git commit: worked on portlet async support
Date Thu, 14 Apr 2016 12:29:02 GMT
worked on portlet async support


Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/49ca2dd0
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/49ca2dd0
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/49ca2dd0

Branch: refs/heads/master
Commit: 49ca2dd09c7b85eda9959ee2cf478145c3813dc8
Parents: 45fb2a6
Author: Scott Nicklous <msnicklous@apache.org>
Authored: Mon Mar 21 17:47:52 2016 +0100
Committer: Scott Nicklous <msnicklous@apache.org>
Committed: Mon Mar 21 17:47:52 2016 +0100

----------------------------------------------------------------------
 .../samples/AsyncHackIncludedServlet.java       |  49 ++++++
 .../portals/samples/AsyncHackPortlet.java       | 148 +++++++++++++++++++
 .../apache/portals/samples/PathInfoPortlet.java |  44 +-----
 .../src/main/resources/logging.properties       |   2 +-
 .../container/impl/PortletContainerImpl.java    |  27 ++--
 .../pluto/driver/PortalDriverServlet.java       |  48 ++++++
 .../container/DefaultPortletInvokerService.java |  21 ++-
 7 files changed, 280 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackIncludedServlet.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackIncludedServlet.java
b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackIncludedServlet.java
new file mode 100644
index 0000000..9c752c2
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackIncludedServlet.java
@@ -0,0 +1,49 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.portals.samples;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author Scott Nicklous
+ *
+ */
+@WebServlet(urlPatterns="/ais")
+public class AsyncHackIncludedServlet extends HttpServlet {
+   private static final long serialVersionUID = 1L;
+
+   public AsyncHackIncludedServlet() {
+   }
+
+   @Override
+   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
+      
+      PrintWriter writer = resp.getWriter();
+      writer.append("<p>Hello from dispatched servlet!</p>");
+
+   }
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackPortlet.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackPortlet.java
b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackPortlet.java
new file mode 100644
index 0000000..9e2c243
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncHackPortlet.java
@@ -0,0 +1,148 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.portals.samples;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.logging.Logger;
+
+import javax.inject.Inject;
+import javax.portlet.PortletException;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.portlet.ResourceURL;
+import javax.portlet.annotations.Namespace;
+import javax.portlet.annotations.RenderMethod;
+import javax.portlet.annotations.ServeResourceMethod;
+import javax.portlet.annotations.URLFactory;
+import javax.servlet.AsyncContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Resource portlet for viewing path information.
+ */
+public class AsyncHackPortlet {
+   private static final Logger LOGGER = Logger.getLogger(AsyncHackPortlet.class.getName());
+
+   public static final String RESPARAM_DISPLAY = "display";
+
+   // Injecting the namespace & URLFactory
+   @Inject
+   @Namespace
+   private String             pid;
+   @Inject
+   private URLFactory         uf;
+
+   @RenderMethod(portletNames = { "AsyncHackPortlet" }, ordinal = 100)
+   public String getImageInclude() {
+      LOGGER.fine("Rendering async hack portlet");
+
+      StringBuilder txt = new StringBuilder(128);
+      txt.append("<h3>Async Hack Portlet</h3>");
+
+      ResourceURL resurl = uf.createResourceURL();
+
+      txt.append("<div class='infobox' id='").append(pid).append("-putResourceHere'></div>\n");
+      txt.append("<script>\n");
+      txt.append("(function () {\n");
+      txt.append("   var xhr = new XMLHttpRequest();\n");
+      txt.append("   xhr.onreadystatechange=function() {\n");
+      txt.append("      if (xhr.readyState==4 && xhr.status==200) {\n");
+      txt.append("         document.getElementById('").append(pid)
+            .append("-putResourceHere').innerHTML=xhr.responseText;\n");
+      txt.append("      }\n");
+      txt.append("   };\n");
+      txt.append("   xhr.open(\"GET\",\"").append(resurl.toString()).append("\",true);\n");
+      txt.append("   xhr.send();\n");
+      txt.append("})();\n");
+      txt.append("</script>\n");
+
+      return txt.toString();
+   }
+
+   /**
+    * This resource method generates some output directly, then includes output from a JSP
as specified in the
+    * annotation.
+    * 
+    * @return The string for inclusion in the output.
+    * @throws IOException
+    * @throws PortletException 
+    */
+   @ServeResourceMethod(portletNames = { "AsyncHackPortlet" }, asyncSupported = true)
+   public void getPathInfo(ResourceRequest req, ResourceResponse resp) throws IOException,
PortletException {
+      LOGGER.fine("Doing async hack resource request");
+
+      @SuppressWarnings("unchecked")
+      ArrayList<String> pathInfo = (ArrayList<String>) req.getAttribute("pathInfo");
+      if (pathInfo == null) {
+         pathInfo = new ArrayList<String>();
+      }
+
+      HttpServletRequest hreq = (HttpServletRequest) req.getAttribute("javax.portlet.debug.ServletRequest");
+      HttpServletResponse hresp = (HttpServletResponse) req.getAttribute("javax.portlet.debug.ServletResponse");
+      PathDisplay pd;
+      if (hreq != null) {
+         pd = new PathDisplay(hreq, "Resource Method (Servlet)");
+         hreq.setAttribute("pathInfo", pathInfo);
+      } else {
+         pd = new PathDisplay(req, "Resource Method (Resource)");
+         req.setAttribute("pathInfo", pathInfo);
+      }
+      pathInfo.add(pd.toMarkup());
+
+      PrintWriter writer = resp.getWriter();
+      writer.append("<h5>Async Hack Resource Request</h5>");
+      
+      StringBuilder txt = new StringBuilder(128);
+      txt.append("Trying to start async. Servlet context: ").append(hreq.getServletContext().getContextPath());
+
+      String jsp = "/WEB-INF/jsp/pathinfo.jsp";
+//      String jsp = "/ais";
+//      RequestDispatcher rd = null;
+//      rd = hreq.getRequestDispatcher(jsp);
+//      txt.append("Request dispatcher: ").append(rd);
+      LOGGER.fine(txt.toString());
+      txt.setLength(0);
+      
+      if (hreq != null && hresp != null) {
+         try {
+            
+            AsyncContext context = hreq.startAsync(hreq, hresp);
+            context.setTimeout(1000);
+            
+            txt.append("Async context: ").append((context == null) ? "null." : "not null.");
+            txt.append(" Now dispatching ... ");
+
+//            context.dispatch(hreq.getServletContext(), jsp);
+            context.dispatch(jsp);
+            
+            txt.append(" done. ");
+         } catch (Exception e) {
+            txt.append(" ... didn't work. Exception: ").append(e.toString());
+         }
+         
+         LOGGER.fine(txt.toString());
+         txt.setLength(0);
+      }
+
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/PathInfoPortlet.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/PathInfoPortlet.java
b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/PathInfoPortlet.java
index aa87980..8eb2594 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/PathInfoPortlet.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/PathInfoPortlet.java
@@ -19,7 +19,6 @@
 package org.apache.portals.samples;
 
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.util.ArrayList;
 
 import javax.inject.Inject;
@@ -30,11 +29,7 @@ import javax.portlet.annotations.Namespace;
 import javax.portlet.annotations.RenderMethod;
 import javax.portlet.annotations.ServeResourceMethod;
 import javax.portlet.annotations.URLFactory;
-import javax.servlet.AsyncContext;
-import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
 
 /**
  * Resource portlet for viewing path information.
@@ -83,7 +78,7 @@ public class PathInfoPortlet {
     * @return The string for inclusion in the output.
     * @throws IOException
     */
-   @ServeResourceMethod(portletNames = { "PathInfoPortlet" }, asyncSupported = true)
+   @ServeResourceMethod(portletNames = { "PathInfoPortlet" }, include="/WEB-INF/jsp/pathinfo.jsp")
    public void getPathInfo(ResourceRequest req, ResourceResponse resp) throws IOException
{
 
       @SuppressWarnings("unchecked")
@@ -93,7 +88,6 @@ public class PathInfoPortlet {
       }
 
       HttpServletRequest hreq = (HttpServletRequest) req.getAttribute("javax.portlet.debug.ServletRequest");
-      HttpServletResponse hresp = (HttpServletResponse) req.getAttribute("javax.portlet.debug.ServletResponse");
       PathDisplay pd;
       if (hreq != null) {
          pd = new PathDisplay(hreq, "Resource Method (Servlet)");
@@ -104,42 +98,6 @@ public class PathInfoPortlet {
 
       req.setAttribute("pathInfo", pathInfo);
 
-      PrintWriter writer = resp.getWriter();
-      writer.append("<h5>Async Hack Resource Request</h5>");
-      writer.append("<p>");
-      writer.append("Trying to start async. Servlet context = ").append(hreq.getServletContext().getContextPath())
-            .append(". ");
-      resp.flushBuffer();
-
-      RequestDispatcher rd = null;
-      String jsp = "/WEB-INF/jsp/pathinfo.jsp";
-      rd = hreq.getRequestDispatcher(jsp);
-      if (hreq != null && hresp != null) {
-         try {
-            AsyncContext context = hreq.startAsync(hreq, hresp);
-            context.setTimeout(3000);
-            writer = context.getResponse().getWriter();
-            writer.append("worked. Async context: ").append((context == null) ? "null." :
"not null.");
-            writer.append(" Now dispatching ... ");
-            resp.flushBuffer();
-            context.dispatch(jsp);
-            writer.append(" done. ");
-         } catch (Exception e) {
-            writer.append(" ... didn't work. Exception: ").append(e.toString());
-            resp.flushBuffer();
-         }
-      }
-
-      writer.append("</p><hr>");
-      resp.flushBuffer();
-      try {
-         writer.append("<p>Now doing request dispatcher include.</p>");
-         rd.include(hreq, hresp);
-      } catch (ServletException e) {
-         writer.append("<p>Exception including jsp. ").append(e.toString());
-         writer.append("</p>");
-      }
-
    }
 
 }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/PortletV3AnnotatedDemo/src/main/resources/logging.properties
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/resources/logging.properties b/PortletV3AnnotatedDemo/src/main/resources/logging.properties
index 09bae36..9a479f9 100644
--- a/PortletV3AnnotatedDemo/src/main/resources/logging.properties
+++ b/PortletV3AnnotatedDemo/src/main/resources/logging.properties
@@ -34,4 +34,4 @@ org.apache.juli.FileHandler.directory = ${catalina.base}/logs
 org.apache.juli.FileHandler.prefix = PortletV3AnnotatedDemo.
 
 org.apache.pluto.level=FINE
-basic.portlet.level=FINEST
+org.apache.portals.samples.level=FINEST

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletContainerImpl.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletContainerImpl.java
b/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletContainerImpl.java
index c1be463..dc17d7f 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletContainerImpl.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/impl/PortletContainerImpl.java
@@ -267,16 +267,23 @@ public class PortletContainerImpl implements PortletContainer
         ResourceResponse portletResponse = envService.createResourceResponse(responseContext,
requestContext.getCacheability());
 
         FilterManager filterManager = filterInitialisation(portletWindow,PortletRequest.RESOURCE_PHASE);
-
-        try
-        {
-            invoker.serveResource(requestContext, portletRequest, portletResponse, filterManager);
-            // Mark portlet interaction is completed: backend implementation can flush response
state now
-            responseContext.close();
-        }
-        finally
-        {
-            responseContext.release();
+        
+        if (portletWindow.getPortletDefinition().isAsyncSupported()) {
+           LOG.debug("invoking for async ... no resource release");
+           invoker.serveResource(requestContext, portletRequest, portletResponse, filterManager);
+           
+        } else {
+           LOG.debug("invocation through request dispatcher.");
+           try
+           {
+              invoker.serveResource(requestContext, portletRequest, portletResponse, filterManager);
+              // Mark portlet interaction is completed: backend implementation can flush
response state now
+              responseContext.close();
+           }
+           finally
+           {
+              responseContext.release();
+           }
         }
 
         debugWithName("Portlet resource done for: " + portletWindow.getPortletDefinition().getPortletName());

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/pluto-portal-driver/src/main/java/org/apache/pluto/driver/PortalDriverServlet.java
----------------------------------------------------------------------
diff --git a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/PortalDriverServlet.java
b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/PortalDriverServlet.java
index 21326ae..722b037 100644
--- a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/PortalDriverServlet.java
+++ b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/PortalDriverServlet.java
@@ -17,6 +17,7 @@
 package org.apache.pluto.driver;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.List;
@@ -24,6 +25,7 @@ import java.util.Map;
 
 import javax.portlet.PortletException;
 import javax.portlet.PortletRequest;
+import javax.servlet.AsyncContext;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
@@ -35,6 +37,7 @@ import javax.servlet.http.HttpServletResponse;
 import org.apache.pluto.container.HeaderData;
 import org.apache.pluto.container.PortletContainer;
 import org.apache.pluto.container.PortletContainerException;
+import org.apache.pluto.container.PortletInvokerService;
 import org.apache.pluto.container.om.portlet.ContainerRuntimeOption;
 import org.apache.pluto.container.om.portlet.PortletDefinition;
 import org.apache.pluto.driver.config.DriverConfiguration;
@@ -142,6 +145,51 @@ public class PortalDriverServlet extends HttpServlet {
             LOG.debug("Processing " + reqType + " request for window: " + portletWindow.getId().getStringId());
          }
 
+         
+//          String portletName = portletWindow.getPortletName();
+//          if (portletName.equals("AsyncHackPortlet")
+//                && portalURL.getType() == URLType.Resource) {
+// 
+//             String uri =  PortletInvokerService.URIPREFIX + portletWindow.getPortletDefinition().getPortletName();
+//             String ctxpath =  "/PortletV3AnnotatedDemo";
+//             String jsp = "/WEB-INF/jsp/pathinfo.jsp";
+//             
+// //            LOG.debug("Directly doing async dispatch to JSP. uri: " + uri + ", jsp:
" + jsp);
+//             LOG.debug("Directly dispatching to portlet servlet. uri: " + uri);
+//             
+//             ServletContext ctx = request.getServletContext().getContext(ctxpath);
+//             RequestDispatcher rd = ctx.getRequestDispatcher(uri);
+//             rd.forward(request, response);
+//             LOG.debug("Completed the dispatch.");
+//             return;
+//             
+// //             ServletContext ctx = request.getServletContext().getContext(ctxpath);
+// //             if (ctx == null) {
+// //                LOG.debug("couldn't get context for uri.");
+// //             } else {
+// //                LOG.debug("Got context, path: >>>" + ctx.getContextPath() +
"<<<");
+// // 
+// //                AsyncContext context = request.startAsync();
+// //                context.dispatch(ctx, jsp);
+// //                
+// //                LOG.debug("Completed the dispatch.");
+// //                return;
+// // 
+// //                
+// //               RequestDispatcher rd = ctx.getRequestDispatcher(jsp);
+// //               if (rd == null) {
+// //                  LOG.debug("couldn't get request dispatcher for context.");
+// //               } else {
+// //                  LOG.debug("Got the request dispatcher.");
+// // //                  PrintWriter writer = response.getWriter();
+// // //                  writer.append("<h5>Greetings from portal driver servlet!</h5>");
+// //                  rd.include(request, response);
+// //                  LOG.debug("Dispatch seemed to go OK.");
+// //                  return;
+// //               }
+// //             }
+//          }
+
          try {
             PageState ps;
             String jsondata;

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/49ca2dd0/pluto-portal-driver/src/main/java/org/apache/pluto/driver/container/DefaultPortletInvokerService.java
----------------------------------------------------------------------
diff --git a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/container/DefaultPortletInvokerService.java
b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/container/DefaultPortletInvokerService.java
index a46a4e5..3d1e4ea 100644
--- a/pluto-portal-driver/src/main/java/org/apache/pluto/driver/container/DefaultPortletInvokerService.java
+++ b/pluto-portal-driver/src/main/java/org/apache/pluto/driver/container/DefaultPortletInvokerService.java
@@ -31,6 +31,7 @@ import javax.portlet.RenderRequest;
 import javax.portlet.RenderResponse;
 import javax.portlet.ResourceRequest;
 import javax.portlet.ResourceResponse;
+import javax.servlet.AsyncContext;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
@@ -239,7 +240,15 @@ public class DefaultPortletInvokerService implements PortletInvokerService
{
 
                 if (methodID.equals(PortletInvokerService.METHOD_RESOURCE))
                 {
-                    dispatcher.forward(containerRequest, containerResponse);
+                    if (portletWindow.getPortletDefinition().isAsyncSupported()) {
+                       LOG.debug("Async dispatching resource request to portlet servlet.");
+                       AsyncContext actx = containerRequest.startAsync(containerRequest,
containerResponse);
+                       actx.dispatch(servletContext, uri);
+                    } else {
+                       LOG.debug("Request dispatcher forward resource request to portlet
servlet.");
+                       dispatcher.forward(containerRequest, containerResponse);
+                    }
+                    LOG.debug("Dispatch complete.");
                 }
                 else
                 {
@@ -275,10 +284,12 @@ public class DefaultPortletInvokerService implements PortletInvokerService
{
                 }
 
             } finally {
-                containerRequest.removeAttribute(PortletInvokerService.METHOD_ID);
-                containerRequest.removeAttribute(PortletInvokerService.PORTLET_REQUEST);
-                containerRequest.removeAttribute(PortletInvokerService.PORTLET_RESPONSE);
-                containerRequest.removeAttribute(PortletInvokerService.FILTER_MANAGER);
+                if (!portletWindow.getPortletDefinition().isAsyncSupported()) {
+                   containerRequest.removeAttribute(PortletInvokerService.METHOD_ID);
+                   containerRequest.removeAttribute(PortletInvokerService.PORTLET_REQUEST);
+                   containerRequest.removeAttribute(PortletInvokerService.PORTLET_RESPONSE);
+                   containerRequest.removeAttribute(PortletInvokerService.FILTER_MANAGER);
+                }
             }
         } else {
             String msg = EXCEPTIONS.getString(


Mime
View raw message