001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.web;
023:
024: import java.io.IOException;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.zip.ZipInputStream;
028: import org.apache.commons.fileupload.DiskFileUpload;
029: import org.apache.commons.fileupload.FileItem;
030: import org.apache.commons.fileupload.FileUpload;
031: import org.apache.commons.fileupload.FileUploadException;
032: import org.jbpm.JbpmConfiguration;
033: import org.jbpm.JbpmContext;
034: import org.jbpm.graph.def.ProcessDefinition;
035:
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: public class ProcessUploadServlet extends HttpServlet {
044:
045: static JbpmConfiguration jbpmConfiguration = JbpmConfiguration
046: .getInstance();
047:
048: public static final String UPLOAD_TYPE_DEFINITION = "definition";
049: public static final String UPLOAD_TYPE_ARCHIVE = "archive";
050:
051: public void service(HttpServletRequest request,
052: HttpServletResponse response) throws IOException {
053: //upload from gpd has url mapping: /upload
054:
055: JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
056: try {
057: response.setContentType("text/html");
058: response.getWriter().println(handleRequest(request));
059: } finally {
060: jbpmContext.close();
061: }
062: }
063:
064: private String handleRequest(HttpServletRequest request) {
065: //check if request is multipart content
066: log.debug("Handling upload request");
067: if (!FileUpload.isMultipartContent(request)) {
068: log.debug("Not a multipart request");
069: return "Not a multipart request";
070: }
071:
072: try {
073: DiskFileUpload fileUpload = new DiskFileUpload();
074: List list = fileUpload.parseRequest(request);
075: log.debug("Upload from GPD");
076: Iterator iterator = list.iterator();
077: if (!iterator.hasNext()) {
078: log.debug("No process file in the request");
079: return "No process file in the request";
080: }
081: FileItem fileItem = (FileItem) iterator.next();
082: if (fileItem.getContentType().indexOf(
083: "application/x-zip-compressed") == -1) {
084: log.debug("Not a process archive");
085: return "Not a process archive";
086: }
087: try {
088: log.debug("Deploying process archive "
089: + fileItem.getName());
090: ZipInputStream zipInputStream = new ZipInputStream(
091: fileItem.getInputStream());
092: JbpmContext jbpmContext = JbpmContext
093: .getCurrentJbpmContext();
094: log.debug("Preparing to parse process archive");
095: ProcessDefinition processDefinition = ProcessDefinition
096: .parseParZipInputStream(zipInputStream);
097: log.debug("Created a processdefinition : "
098: + processDefinition.getName());
099: jbpmContext.deployProcessDefinition(processDefinition);
100: zipInputStream.close();
101: return "Deployed archive "
102: + processDefinition.getName() + " successfully";
103: } catch (IOException e) {
104: return "IOException";
105: }
106: } catch (FileUploadException e) {
107: e.printStackTrace();
108: return "FileUploadException";
109: }
110: }
111:
112: private static final Log log = LogFactory
113: .getLog(ProcessUploadServlet.class);
114: }
|