Author: julien(a)jboss.com
Date: 2007-04-06 20:31:37 -0400 (Fri, 06 Apr 2007)
New Revision: 6941
Added:
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FileUploadPortlet.java
Modified:
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml
trunk/core/src/main/org/jboss/portal/core/controller/ajax/AjaxResponseHandler.java
trunk/theme/src/bin/portal-ajax-war/dyna/dyna.js
Log:
- added a file upload portlet in the examples
- add the enctype when it is present on the XHR
- if enctype is multipart/formdata don't handle it as an ajax request
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/ajax/AjaxResponseHandler.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/ajax/AjaxResponseHandler.java 2007-04-06
22:47:51 UTC (rev 6940)
+++
trunk/core/src/main/org/jboss/portal/core/controller/ajax/AjaxResponseHandler.java 2007-04-07
00:31:37 UTC (rev 6941)
@@ -164,7 +164,7 @@
}
else
{
- throw new Error("todo");
+ throw new Error("todo " + commandResponse);
}
}
else if (commandResponse instanceof UpdateWindowMarkupResponse)
Added:
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FileUploadPortlet.java
===================================================================
--- trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FileUploadPortlet.java
(rev 0)
+++
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FileUploadPortlet.java 2007-04-07
00:31:37 UTC (rev 6941)
@@ -0,0 +1,108 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.portlet.test;
+
+import org.apache.commons.fileupload.portlet.PortletFileUpload;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+
+import javax.portlet.GenericPortlet;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.portlet.PortletException;
+import javax.portlet.PortletSecurityException;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.List;
+import java.util.Iterator;
+import java.util.ArrayList;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FileUploadPortlet extends GenericPortlet
+{
+
+
+ public void processAction(ActionRequest req, ActionResponse resp) throws
PortletException, PortletSecurityException, IOException
+ {
+
+ //
+ List fileNames = new ArrayList();
+ List fileSizes = new ArrayList();
+
+ //
+ try
+ {
+ DiskFileItemFactory factory = new DiskFileItemFactory();
+ PortletFileUpload upload = new PortletFileUpload(factory);
+ List fileItems = upload.parseRequest(req);
+ for (Iterator iterator = fileItems.iterator();iterator.hasNext();)
+ {
+ FileItem item = (FileItem)iterator.next();
+ if (!item.isFormField())
+ {
+ fileNames.add("" + item.getName());
+ fileSizes.add("" + item.getSize());
+ }
+ }
+ }
+ catch (FileUploadException e)
+ {
+ throw new PortletException(e);
+ }
+
+ //
+ resp.setRenderParameter("fileNames", (String[])fileNames.toArray(new
String[fileNames.size()]));
+ resp.setRenderParameter("fileSizes", (String[])fileSizes.toArray(new
String[fileSizes.size()]));
+ }
+
+ protected void doView(RenderRequest req, RenderResponse resp) throws PortletException,
PortletSecurityException, IOException
+ {
+ //
+ resp.setContentType("text/html");
+
+ //
+ PrintWriter writer = resp.getWriter();
+
+ String[] fileNames = req.getParameterValues("fileNames");
+ String[] fileSizes = req.getParameterValues("fileSizes");
+ if (fileNames != null && fileSizes != null && fileNames.length ==
fileSizes.length)
+ {
+ for (int i = 0;i < fileNames.length;i++)
+ {
+ writer.println("Received file " + fileNames[i] + " with size
" + fileSizes[i] + "<br/>");
+ }
+ }
+
+ //
+ writer.println("<form action=\"" + resp.createActionURL() +
"\" method=\"post\"
enctype=\"multipart/form-data\">");
+ writer.println("<input type=\"file\"
name=\"File\">");
+ writer.println("<input type=\"submit\"
name=\"Upload\">");
+ writer.println("</form>");
+ }
+}
Modified:
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml
===================================================================
---
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml 2007-04-06
22:47:51 UTC (rev 6940)
+++
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml 2007-04-07
00:31:37 UTC (rev 6941)
@@ -146,4 +146,10 @@
<portlet-ref>FSContentDrivenPortlet</portlet-ref>
</instance>
</deployment>
+ <deployment>
+ <instance>
+ <instance-id>FileUploadPortletInstance</instance-id>
+ <portlet-ref>FileUploadPortlet</portlet-ref>
+ </instance>
+ </deployment>
</deployments>
\ No newline at end of file
Modified: trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml 2007-04-06
22:47:51 UTC (rev 6940)
+++ trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml 2007-04-07
00:31:37 UTC (rev 6941)
@@ -347,6 +347,19 @@
<keywords>sample,test</keywords>
</portlet-info>
</portlet>
+ <portlet>
+ <description>File Upload Portlet</description>
+ <portlet-name>FileUploadPortlet</portlet-name>
+ <display-name>File Upload Portlet</display-name>
+
<portlet-class>org.jboss.portal.core.portlet.test.FileUploadPortlet</portlet-class>
+ <supports>
+ <mime-type>text/html</mime-type>
+ </supports>
+ <portlet-info>
+ <title>File Upload Portlet</title>
+ <keywords>sample,test</keywords>
+ </portlet-info>
+ </portlet>
<user-attribute>
<name>user.name.nickName</name>
</user-attribute>
Modified: trunk/theme/src/bin/portal-ajax-war/dyna/dyna.js
===================================================================
--- trunk/theme/src/bin/portal-ajax-war/dyna/dyna.js 2007-04-06 22:47:51 UTC (rev 6940)
+++ trunk/theme/src/bin/portal-ajax-war/dyna/dyna.js 2007-04-07 00:31:37 UTC (rev 6941)
@@ -47,19 +47,20 @@
// We found the window
if (container != null) {
-
- var form = source.nodeName == "FORM";
- var link = !form & source.nodeName == "A";
-
+ //
+ var options = new Object();
var url;
- var body;
- var method;
+ //
if (source.nodeName == "A")
{
url = source.href;
- method = "get";
- body = "";
+
+ // We have a get
+ options.method = "get"
+
+ // We don't block
+ options.asynchronous = true;
}
else if (source.nodeName == "INPUT" && source.type ==
"submit")
{
@@ -72,26 +73,38 @@
// Check we have a form and use it
if (current.nodeName == 'FORM') {
- //
- url = current.action;
+ var enctype = current.enctype
- // Get body if needed
- if (current.method.toLowerCase() == "post") {
- body = Form.serialize(current);
- method = "post";
+ // We don't handle file upload for now
+ if (enctype != "multipart/form-data") {
+ // Set URL
+ url = current.action;
+
+ // Set the specified enctype
+ options.enctype = enctype;
+
+ // Action blocks
+ options.asynchronous = false;
+
+ // Add body if needed
+ if (current.method.toLowerCase() == "post") {
+ options.method = "post"
+ options.postBody = Form.serialize(current);
+ }
+ else {
+ options.method = "get"
+ }
}
- else
- {
- body = "";
- method = "get";
- }
}
}
// Handle links here
if (url != null) {
- //
+
+ // Setup headers
var headers = ["ajax","true"];
+
+ // Add the view state value
if (view_state !== undefined)
{
headers.view_state = view_state;
@@ -101,54 +114,39 @@
// of a post, the parameters will be appended to the body of the query which
// will lead to a non correct request
- // If we process a form then we will perform a blocking interaction
- var asynchronous = !form;
-
- //
- var options = {
- asynchronous:asynchronous,
- method: method,
- postBody: body,
- requestHeaders:headers,
- onSuccess: function(t)
+ // Complete the ajax request options
+ options.requestHeaders = headers;
+ options.onSuccess = function(t)
+ {
+ var resp = "";
+ eval("resp =" + t.responseText + ";");
+ if (resp.type == "update_markup")
{
- var resp = "";
- eval("resp =" + t.responseText + ";");
- if (resp.type == "update_markup")
+ // Iterate all changes
+ for (var id in resp.fragments)
{
- // Iterate all changes
- for (var id in resp.fragments)
+ var matchingElt = document.getElementById(id);
+
+ // Different than 1 is not good
+ if (matchingElt != null)
{
- var matchingElt = document.getElementById(id);
+ var markup = resp.fragments[id];
+ markup = markup.substring(25, markup.length - 7);
- // Different than 1 is not good
- if (matchingElt != null)
- {
- var markup = resp.fragments[id];
- markup = markup.substring(25, markup.length - 7);
-
- var markupContainer = Element.up(matchingElt);
- markupContainer.innerHTML = markup;
- }
+ var markupContainer = Element.up(matchingElt);
+ markupContainer.innerHTML = markup;
}
-
- // update view state
- view_state = resp.view_state;
}
- else if (resp.type == "update_page")
- {
- document.location = resp.location;
- }
+
+ // update view state
+ view_state = resp.view_state;
}
+ else if (resp.type == "update_page")
+ {
+ document.location = resp.location;
+ }
};
- // Add the form post body
- // todo : detect file upload
- if (form)
- {
- options.postBody = Form.serialize(source);
- }
-
//
Event.stop(event);
new Ajax.Request(url, options);