From 7c4ddd4fddefbefdf1666812beec24a269504a01 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Thu, 2 Aug 2007 23:44:15 +0000 Subject: [PATCH] Merged V2.1 to HEAD 6374: AR-1639 Web Script Content Upload Patch merge: Schema target 71 changed to 101 git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6404 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../web/scripts/AbstractWebScript.java | 4 + .../org/alfresco/web/scripts/FormData.java | 266 ++++++++++++++++++ .../alfresco/web/scripts/WebScriptStatus.java | 2 +- 3 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 source/java/org/alfresco/web/scripts/FormData.java diff --git a/source/java/org/alfresco/web/scripts/AbstractWebScript.java b/source/java/org/alfresco/web/scripts/AbstractWebScript.java index 127780447d..91c5605ee0 100644 --- a/source/java/org/alfresco/web/scripts/AbstractWebScript.java +++ b/source/java/org/alfresco/web/scripts/AbstractWebScript.java @@ -243,6 +243,10 @@ public abstract class AbstractWebScript implements WebScript // add web script context model.put("args", createArgModel(req)); + if (req instanceof WebScriptServletRequest) + { + model.put("formdata", new FormData(((WebScriptServletRequest)req).getHttpServletRequest())); + } model.put("guest", req.isGuest()); model.put("url", new URLModel(req)); model.put("server", new ServerModel(descriptorService.getServerDescriptor())); diff --git a/source/java/org/alfresco/web/scripts/FormData.java b/source/java/org/alfresco/web/scripts/FormData.java new file mode 100644 index 0000000000..3f1c69a824 --- /dev/null +++ b/source/java/org/alfresco/web/scripts/FormData.java @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program 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 General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.scripts; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Collections; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.alfresco.repo.jscript.Scopeable; +import org.alfresco.repo.jscript.ScriptNode; +import org.alfresco.repo.jscript.ScriptNode.ScriptContent; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.FileUploadException; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Scriptable; + + +/** + * Form Data + * + * @author davidc + */ +public class FormData implements Serializable, Scopeable +{ + private static final long serialVersionUID = 1832644544828452385L; + + private Scriptable scope; + private HttpServletRequest req; + private ServletFileUpload upload; + private List files = null; + + /** + * Construct + * + * @param req + */ + public FormData(HttpServletRequest req) + { + this.req = req; + } + + /* (non-Javadoc) + * @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable) + */ + public void setScope(Scriptable scope) + { + this.scope = scope; + } + + /** + * Determine if multi-part form data has been provided + * + * @return true => multi-part + */ + public boolean getIsMultiPart() + { + return upload.isMultipartContent(req); + } + + public boolean jsGet_isMultipart() + { + return getIsMultiPart(); + } + + /** + * Determine if form data has specified field + * + * @param name field to look for + * @return true => form data contains field + */ + public boolean hasField(String name) + { + List files = getFiles(); + for (FileItem file : files) + { + if (file.getFieldName().equals(name)) + { + return true; + } + } + return false; + } + + /** + * Gets the Form fields + * + * @return array of FormField + */ + public Scriptable getFields() + { + List files = getFiles(); + Object[] fields = new Object[files.size()]; + int i = 0; + for (FileItem file : files) + { + fields[i++] = new FormField(file); + } + return Context.getCurrentContext().newArray(this.scope, fields); + } + + public Scriptable jsGet_fields() + { + return getFields(); + } + + /** + * Helper to parse servlet request form data + * + * @return parsed form data + */ + private List getFiles() + { + // NOTE: This class is not thread safe - it is expected to be constructed on each thread. + if (files == null) + { + FileItemFactory factory = new DiskFileItemFactory(); + upload = new ServletFileUpload(factory); + try + { + files = upload.parseRequest(req); + } + catch(FileUploadException e) + { + // NOTE: assume no files can be located + files = Collections.EMPTY_LIST; + } + } + return files; + } + + + /** + * Form Field + * + * @author davidc + */ + public class FormField implements Serializable + { + private FileItem file; + + /** + * Construct + * + * @param file + */ + public FormField(FileItem file) + { + this.file = file; + } + + /** + * @return field name + */ + public String getName() + { + return file.getFieldName(); + } + + public String jsGet_name() + { + return getName(); + } + + /** + * @return true => field represents a file + */ + public boolean getIsFile() + { + return !file.isFormField(); + } + + public boolean jsGet_isFile() + { + return getIsFile(); + } + + /** + * @return field value (for file, attempts conversion to string) + */ + public String getValue() + { + return file.getString(); + } + + public String jsGet_value() + { + return getValue(); + } + + /** + * @return field as content + */ + public ScriptContent getContent() + { + try + { + return new ScriptNode.ScriptContentStream(file.getInputStream(), getMimetype(), null); + } + catch(IOException e) + { + return null; + } + } + + public ScriptContent jsGet_content() + { + return getContent(); + } + + /** + * @return mimetype + */ + public String getMimetype() + { + return file.getContentType(); + } + + public String jsGet_mimetype() + { + return getMimetype(); + } + + /** + * @return filename (only for file fields, otherwise null) + */ + public String getFilename() + { + return file.getName(); + } + + public String jsGet_filename() + { + return getFilename(); + } + + } + +} diff --git a/source/java/org/alfresco/web/scripts/WebScriptStatus.java b/source/java/org/alfresco/web/scripts/WebScriptStatus.java index 87f91b5847..46b41b9f01 100644 --- a/source/java/org/alfresco/web/scripts/WebScriptStatus.java +++ b/source/java/org/alfresco/web/scripts/WebScriptStatus.java @@ -40,7 +40,7 @@ public class WebScriptStatus { private Throwable exception = null; private int code = HttpServletResponse.SC_OK; - private String message = null; + private String message = ""; private boolean redirect = false;