View Javadoc

1   /*
2    * $Id: WorkflowUploadServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
3    *
4    * Copyright (c) 2007
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.first.fraunhofer.de for more details.
7    */
8   
9   package net.kwfgrid.gwes.servlet;
10  
11  import org.antlr.stringtemplate.StringTemplate;
12  import org.apache.commons.fileupload.FileItem;
13  import org.apache.commons.fileupload.FileItemFactory;
14  import org.apache.commons.fileupload.FileUploadException;
15  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
16  import org.apache.commons.fileupload.servlet.ServletFileUpload;
17  import org.apache.log4j.Logger;
18  
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpSession;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.util.List;
26  
27  import net.kwfgrid.gwes.client.RemoteGWES;
28  import net.kwfgrid.gwes.exception.GWESException;
29  
30  /**
31   * @author Andreas Hoheisel
32   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
33   * @version $Id: WorkflowUploadServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
34   */
35  public class WorkflowUploadServlet extends HttpServlet {
36  
37      final static Logger logger = Logger.getLogger(WorkflowUploadServlet.class);
38      private static GWESProperties pr;
39  
40      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
41          ServletLogger.log(request);
42          
43          response.setContentType("text/html");
44          PrintWriter out = response.getWriter();
45  
46          //read gwes.properties
47          if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
48  
49          //get request attributes
50          String error = (String) request.getAttribute("error");
51          String success = (String) request.getAttribute("newworkflowinfo");
52          String workflowID = (String) request.getAttribute("workflowid");
53  
54          // print html
55          StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("workflowUpload");
56          t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "New"));
57          if (error != null) t.setAttribute("error", error);
58          if (success != null) t.setAttribute("success", success);
59          if (workflowID != null) t.setAttribute("workflowID", workflowID);
60          t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
61          out.print(t.toString());
62      }
63  
64      public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
65          //read gwes.properties
66          if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
67  
68          // test for file upload request
69          boolean isMultipart = ServletFileUpload.isMultipartContent(request);
70          if (isMultipart) logger.debug("=============> This is an file upload");
71  
72          // Create a factory for disk-based file items
73          FileItemFactory factory = new DiskFileItemFactory();
74  
75          // Create a new file upload handler
76          ServletFileUpload upload = new ServletFileUpload(factory);
77  
78          // Parse the request
79          try {
80              String workflowXml = null;
81              String fileName = null;
82              long sizeInBytes = 0;
83              List items = upload.parseRequest(request);
84              // Process the uploaded items
85              for (Object item1 : items) {
86                  FileItem item = (FileItem) item1;
87  
88                  if (item.isFormField()) {
89                      String name = item.getFieldName();
90                      String value = item.getString();
91                      logger.debug("=========> " + name + "=" + value);
92                  } else {
93                      // String fieldName = item.getFieldName();
94                      fileName = item.getName();
95                      String contentType = item.getContentType();
96                      // boolean isInMemory = item.isInMemory();
97                      sizeInBytes = item.getSize();
98                      logger.info("=========> file upload: " + fileName + " (" + sizeInBytes + "Bytes)");
99  //                    if (!contentType.equals("text/xml"))
100 //                        request.setAttribute("error", "Wrong file type \"" + contentType + "\". Must be of type \"text/xml\".");
101 //                    else {
102                         workflowXml = new String(item.get());
103                         // logger.info("=========> GWorkflowDL XML: "+workflowXml);
104 //                    }
105                 }
106             }
107             if (workflowXml != null) {
108                 // initiate new workflow
109                 String userID = request.getUserPrincipal().getName();
110                 String workflowid = initiateWorkflow(userID, workflowXml);
111                 HttpSession session = request.getSession();
112                 // parameter set for session (visible to other Servlets of same web application)
113                 logger.debug("session workflow = "+workflowid);
114                 session.setAttribute("workflowid", workflowid);
115                 request.setAttribute("workflowid", workflowid);
116                 StringBuffer newworkflowinfo = new StringBuffer();
117                 newworkflowinfo.append("A new workflow with id \"")
118                         .append(workflowid)
119                         .append("\" has been initiated based on the uploaded workflow description \"")
120                         .append(fileName)
121                         .append("\" (").append(sizeInBytes).append("Bytes). ");
122                 request.setAttribute("newworkflowinfo", newworkflowinfo.toString());
123             }
124         } catch (FileUploadException e) {
125             request.setAttribute("error", "Could not upload file: <p>" + e);
126             logger.error("Could not upload file: " + e, e);
127         } catch (Exception e) {
128             request.setAttribute("error", "Exception when uploading a workflow: <p>" + e);
129             logger.error("Exception when uploading a workflow: "+e,e);
130         } catch (GWESException e) {
131             request.setAttribute("error", "Exception when uploading a workflow: <p>" + e);
132             logger.error("Exception when uploading a workflow: "+e,e);
133         }
134 
135         doGet(request, response);
136     }
137 
138     /**
139      * Initiate a new workflow.
140      */
141     private String initiateWorkflow(String userID, String xml) throws Exception, GWESException {
142         return RemoteGWES.getInstance().initiate(xml, userID);
143     }
144 
145 }