You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by ms...@apache.org on 2016/04/08 15:40:27 UTC

[15/34] portals-pluto git commit: worked on async portlet

worked on async portlet


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

Branch: refs/heads/V3Prototype
Commit: f0e0b11c175fdf74555884a5baeb656987cf60d6
Parents: 4b054ad
Author: Scott Nicklous <ms...@apache.org>
Authored: Mon Mar 28 23:01:23 2016 +0200
Committer: Scott Nicklous <ms...@apache.org>
Committed: Mon Mar 28 23:01:23 2016 +0200

----------------------------------------------------------------------
 .../apache/portals/samples/AsyncDialogBean.java | 224 +++++++++++++++++++
 .../portals/samples/AsyncPortletResource.java   | 117 ++++++++++
 .../samples/CSSIncludingHeaderMethod.java       |   2 +-
 .../src/main/webapp/WEB-INF/jsp/asyncDialog.jsp |  72 ++++++
 .../src/main/webapp/WEB-INF/jsp/asyncOutput.jsp |   9 +
 .../src/main/webapp/resources/css/infobox.css   |   2 +-
 6 files changed, 424 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
new file mode 100644
index 0000000..5f824a2
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncDialogBean.java
@@ -0,0 +1,224 @@
+/*  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.util.Arrays;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.inject.Named;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletException;
+import javax.portlet.annotations.ActionMethod;
+import javax.portlet.annotations.PortletSerializable;
+import javax.portlet.annotations.RenderMethod;
+import javax.portlet.annotations.RenderStateScoped;
+
+/**
+ * Render state scoped bean. The bean is stored as a render parameter, so it
+ * needs to be portlet serializable.
+ */
+@RenderStateScoped @Named("adb")
+public class AsyncDialogBean implements PortletSerializable {
+   private static final Logger LOGGER = Logger.getLogger(AsyncDialogBean.class.getName());
+   
+   public enum OutputType {TEXT, INC, FWD, DISPATCH, AUTO}
+   
+   public static final String PARAM_MSG = "msg";
+   public static final String PARAM_DELAY = "delay";
+   public static final String PARAM_REPS = "reps";
+   public static final String PARAM_AUTO = "auto";
+   public static final String PARAM_TYPE = "type";
+   public static final String PARAM_TYPE_TXT = OutputType.TEXT.toString();
+   public static final String PARAM_TYPE_INC = OutputType.INC.toString();
+   public static final String PARAM_TYPE_FWD = OutputType.FWD.toString();
+   public static final String PARAM_TYPE_DIS = OutputType.DISPATCH.toString();
+
+   
+   private int delay;
+   private int reps;
+   private OutputType type;
+   private String msg;
+   private boolean autoDispatch;
+
+   /**
+    * This method is called by the portlet container to initialize the bean at
+    * the beginning of a request.
+    */
+   @Override
+   public void deserialize(String[] state) {
+      if (state.length == 0) {
+         delay = 1000;
+         reps = 1;
+         type = OutputType.TEXT;
+         msg = null;
+         autoDispatch = true;
+      } else {
+         delay = Integer.parseInt(state[0]);
+         reps = Integer.parseInt(state[1]);
+         type = OutputType.valueOf(state[2]);
+         msg = state[3];
+         autoDispatch = Boolean.parseBoolean(state[4]);
+      }
+      LOGGER.fine("deserialized: " + Arrays.asList(state).toString());
+   }
+
+   /**
+    * Called by the portlet container at the end of an action or event request
+    * to store the serialized data as a portlet render parameter.
+    */
+   @Override
+   public String[] serialize() {
+      String[] state = {""+delay, ""+reps, type.toString(), msg, ""+autoDispatch};
+      LOGGER.fine("serialized: " + Arrays.asList(state).toString());
+      return state;
+   }
+
+
+   /**
+    * @return the delay
+    */
+   public int getDelay() {
+      return delay;
+   }
+
+   /**
+    * @param delay the delay to set
+    */
+   public void setDelay(int delay) {
+      this.delay = delay;
+   }
+
+   /**
+    * @return the reps
+    */
+   public int getReps() {
+      return reps;
+   }
+
+   /**
+    * @param reps the reps to set
+    */
+   public void setReps(int reps) {
+      this.reps = reps;
+   }
+
+   /**
+    * @return the type
+    */
+   public OutputType getType() {
+      return type;
+   }
+
+   /**
+    * @param type the type to set
+    */
+   public void setType(OutputType type) {
+      this.type = type;
+   }
+
+   /**
+    * @return the msg
+    */
+   public String getMsg() {
+      return msg == null ? "" : msg;
+   }
+
+   /**
+    * @param msg the msg to set
+    */
+   public void setMsg(String msg) {
+      this.msg = msg;
+   }
+
+   /**
+    * @return the autoDispatch
+    */
+   public boolean isAutoDispatch() {
+      return autoDispatch;
+   }
+
+   /**
+    * @param autoDispatch the autoDispatch to set
+    */
+   public void setAutoDispatch(boolean autoDispatch) {
+      this.autoDispatch = autoDispatch;
+   }
+
+   /**
+    * Displays the dialog
+    * 
+    * @return the action form as string
+    */
+   @RenderMethod(portletNames = "AsyncPortlet", ordinal = 100, 
+         include = "/WEB-INF/jsp/asyncDialog.jsp")
+   public void getDialog() {
+   }
+
+   /**
+    * Sets values based on form input
+    */
+   @ActionMethod(portletName = "AsyncPortlet")
+   public void handleDialog(ActionRequest req, ActionResponse resp) throws PortletException, IOException {
+      
+      msg = null;
+      
+      String strType = req.getActionParameters().getValue(PARAM_TYPE);
+      if (strType != null) {
+         try {
+            type = OutputType.valueOf(strType);
+         } catch (Exception e) {
+            msg = "try again. bad type: " + strType;
+         }
+      }
+      
+      String strReps = req.getActionParameters().getValue(PARAM_REPS);
+      if (strReps != null) {
+         try {
+            reps = Integer.parseInt(strReps);
+            if (reps <= 0 || reps > 8) throw new Exception("broken");
+         } catch (Exception e) {
+            msg = "try again. bad repetitions.";
+         }
+      }
+      
+      String strDelay = req.getActionParameters().getValue(PARAM_DELAY);
+      if (strDelay != null) {
+         try {
+            delay = Integer.parseInt(strDelay);
+            if (delay < 0) throw new Exception("broken");
+         } catch (Exception e) {
+            msg = "try again. bad delay.";
+         }
+      }
+
+      String auto = req.getActionParameters().getValue(PARAM_AUTO);
+      if (auto != null) {
+         autoDispatch = true;
+      } else {
+         autoDispatch = false;
+      }
+
+      String[] state = {""+delay, ""+reps, type.toString(), msg, ""+autoDispatch};
+      LOGGER.fine("Resulting params: " + Arrays.asList(state).toString());
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
new file mode 100644
index 0000000..221de53
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
@@ -0,0 +1,117 @@
+/*  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.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.inject.Inject;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.portlet.annotations.ServeResourceMethod;
+import javax.servlet.AsyncContext;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.portals.samples.AsyncDialogBean.OutputType;
+
+/**
+ * Implements the async resource method for the async portlet.
+ * 
+ * @author Scott Nicklous
+ *
+ */
+public class AsyncPortletResource {
+   private static final Logger LOGGER = Logger.getLogger(AsyncPortletResource.class.getName());
+   
+   private final static String JSP = "/WEB-INF/jsp/asyncOutput.jsp";
+
+   private class AsyncRunnable implements Runnable {
+      
+      private final AsyncContext ctx;
+      private final int delay;
+      private final OutputType type;
+      private final boolean done;
+
+      public AsyncRunnable(AsyncContext ctx, int delay, OutputType type, boolean done) {
+         this.ctx = ctx;
+         this.delay = delay;
+         this.type = type;
+         this.done = done;
+      }
+
+      @Override
+      public void run() {
+         try {
+            Thread.sleep(delay);
+
+            HttpServletRequest hreq = (HttpServletRequest) ctx.getRequest();
+            HttpServletResponse hresp = (HttpServletResponse) ctx.getResponse();
+            RequestDispatcher rd;
+
+            switch (type) {
+               case TEXT:
+                  StringBuilder txt = new StringBuilder(128);
+                  txt.append("<p>AsyncRunnable:");
+                  txt.append(" dispatcher type: ").append(hreq.getDispatcherType().toString());
+                  txt.append("</p>");
+                  hresp.getWriter().write(txt.toString());
+                  if (done) {
+                     ctx.complete();
+                  }
+                  break;
+               case AUTO:
+                  ctx.dispatch();
+                  break;
+               case DISPATCH:
+                  ctx.dispatch(JSP);
+                  break;
+               case FWD:
+                  rd = hreq.getRequestDispatcher(JSP);
+                  rd.forward(hreq, hresp);
+                  if (done) {
+                     ctx.complete();
+                  }
+                  break;
+               case INC:
+                  rd = hreq.getRequestDispatcher(JSP);
+                  rd.include(hreq, hresp);
+                  if (done) {
+                     ctx.complete();
+                  }
+                  break;
+            }
+
+         } catch (Exception e) {
+            LOGGER.warning("Exception during async processing: " + e.toString());
+         }
+      }
+
+   }
+
+   @Inject
+   private AsyncDialogBean adb;
+
+   @ServeResourceMethod(portletNames = "AsyncPortlet", asyncSupported = true)
+   public void getResource(ResourceRequest req, ResourceResponse resp) throws IOException {
+      resp.getWriter().write("<p>Hello from the resource method!</p>");
+   }
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/CSSIncludingHeaderMethod.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/CSSIncludingHeaderMethod.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/CSSIncludingHeaderMethod.java
index f5f56b6..5fda1b8 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/CSSIncludingHeaderMethod.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/CSSIncludingHeaderMethod.java
@@ -43,7 +43,7 @@ public class CSSIncludingHeaderMethod {
       String contextRoot = req.getContextPath();
       StringBuilder txt = new StringBuilder(128);
       txt.append("<link href='").append(contextRoot);
-      txt.append("/resources/css/styles.css' rel='stylesheet' type='text/css'>");
+      txt.append("/resources/css/infobox.css' rel='stylesheet' type='text/css'>");
       
       resp.getWriter().write(txt.toString());
    }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncDialog.jsp
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncDialog.jsp b/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncDialog.jsp
new file mode 100644
index 0000000..9c59c35
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncDialog.jsp
@@ -0,0 +1,72 @@
+<%--
+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.
+--%>
+<%@ page session="false" %>
+<%@ taglib uri="http://xmlns.jcp.org/portlet_3_0"  prefix="portlet" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ page import="static org.apache.portals.samples.AsyncDialogBean.*" %>
+
+<portlet:defineObjects />
+
+<h3>Async Portlet</h3>
+<div class='parmbox'>
+${adb.getMsg()}
+<FORM  ACTION='<portlet:actionURL/>' id='<portlet:namespace/>-setParams' method='POST' enctype='application/x-www-form-urlencoded'>
+   <table style='width:100%;'><tr><td align='left'>
+
+   Delay:
+   </td><td>
+   <input id='<portlet:namespace/>-delay' name='<%=PARAM_DELAY%>' type='text' value='${adb.getDelay() }' size='5' maxlength='5'>
+   </td><td>
+   Repetitions:
+   </td><td>
+   <input id='<portlet:namespace/>-reps' name='<%=PARAM_REPS%>' type='text' value='${adb.getReps() }' size='5' maxlength='5'>
+   </td><td>
+   <input name='<%=PARAM_AUTO%>' value='<%=PARAM_AUTO%>' type='checkbox' ${adb.isAutoDispatch() ? "checked" : "" } >recursive
+
+   </td></tr><tr><td>
+   Output type:
+   </td><td>
+   <input type='radio' name='<%=PARAM_TYPE%>' value='<%=PARAM_TYPE_TXT%>' ${adb.getType() == "TEXT" ? "checked" : "" } >text
+   </td><td>
+   <input type='radio' name='<%=PARAM_TYPE%>' value='<%=PARAM_TYPE_INC%>' ${adb.getType() == "INC" ? "checked" : "" } >include
+   </td><td>
+   <input type='radio' name='<%=PARAM_TYPE%>' value='<%=PARAM_TYPE_FWD%>' ${adb.getType() == "FWD" ? "checked" : "" } >forward
+   </td><td>
+   <input type='radio' name='<%=PARAM_TYPE%>' value='<%=PARAM_TYPE_DIS%>' ${adb.getType() == "DISPATCH" ? "checked" : "" } >dispatch
+
+   </td></tr><tr><td>
+   <INPUT id ='<portlet:namespace/>-send' VALUE='execute' TYPE='submit'>
+   </td></tr></table>
+</FORM>
+</div>
+<div class='infobox' id='<portlet:namespace/>putResourceHere'></div>
+
+
+<script>
+(function () {
+   var xhr = new XMLHttpRequest();
+   xhr.onreadystatechange=function() {
+      if (xhr.readyState==4 && xhr.status==200) {
+         document.getElementById('<portlet:namespace/>putResourceHere').innerHTML=xhr.responseText;
+      }
+   };
+   xhr.open('GET', '<portlet:resourceURL/>', true);
+   xhr.send();
+})();
+</script>

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncOutput.jsp
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncOutput.jsp b/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncOutput.jsp
new file mode 100644
index 0000000..fbffdb2
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/webapp/WEB-INF/jsp/asyncOutput.jsp
@@ -0,0 +1,9 @@
+<%@ page session="false" %>
+<%@ page isELIgnored ="false" %> 
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
+
+
+<h5>Async JSP Output</h5>
+<p>Dispatch type: <%=request.getDispatcherType() %></p>
+<hr>

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/f0e0b11c/PortletV3AnnotatedDemo/src/main/webapp/resources/css/infobox.css
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/webapp/resources/css/infobox.css b/PortletV3AnnotatedDemo/src/main/webapp/resources/css/infobox.css
index e38c4e9..4629532 100644
--- a/PortletV3AnnotatedDemo/src/main/webapp/resources/css/infobox.css
+++ b/PortletV3AnnotatedDemo/src/main/webapp/resources/css/infobox.css
@@ -12,7 +12,7 @@
    border-style:solid; 
    border-width:2px; 
    padding:4px; 
-   margin:3px;
+   margin:0 0 3px 0;
    overflow:auto;
    border-color:#008800; 
    background:#E0FFE0;