1
2
3
4
5
6
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
32
33
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
47 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
48
49
50 String error = (String) request.getAttribute("error");
51 String success = (String) request.getAttribute("newworkflowinfo");
52 String workflowID = (String) request.getAttribute("workflowid");
53
54
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
66 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
67
68
69 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
70 if (isMultipart) logger.debug("=============> This is an file upload");
71
72
73 FileItemFactory factory = new DiskFileItemFactory();
74
75
76 ServletFileUpload upload = new ServletFileUpload(factory);
77
78
79 try {
80 String workflowXml = null;
81 String fileName = null;
82 long sizeInBytes = 0;
83 List items = upload.parseRequest(request);
84
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
94 fileName = item.getName();
95 String contentType = item.getContentType();
96
97 sizeInBytes = item.getSize();
98 logger.info("=========> file upload: " + fileName + " (" + sizeInBytes + "Bytes)");
99
100
101
102 workflowXml = new String(item.get());
103
104
105 }
106 }
107 if (workflowXml != null) {
108
109 String userID = request.getUserPrincipal().getName();
110 String workflowid = initiateWorkflow(userID, workflowXml);
111 HttpSession session = request.getSession();
112
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
140
141 private String initiateWorkflow(String userID, String xml) throws Exception, GWESException {
142 return RemoteGWES.getInstance().initiate(xml, userID);
143 }
144
145 }