1
2
3
4
5
6
7
8
9 package net.kwfgrid.gwes.servlet;
10
11 import net.kwfgrid.gwes.client.RemoteGWES;
12 import net.kwfgrid.gwes.exception.GWESException;
13 import org.antlr.stringtemplate.StringTemplate;
14 import org.apache.log4j.Logger;
15
16 import javax.servlet.http.HttpServlet;
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19 import javax.servlet.http.HttpSession;
20 import java.io.BufferedReader;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.rmi.RemoteException;
25 import java.util.Map;
26
27
28
29
30
31
32 public class WorkflowTutorialServlet extends HttpServlet {
33
34 final static Logger logger = Logger.getLogger(WorkflowTutorialServlet.class);
35 private static GWESProperties pr;
36
37 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
38 ServletLogger.log(request);
39
40 response.setContentType("text/html");
41 PrintWriter out = response.getWriter();
42
43
44 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
45
46
47 String error = (String) request.getAttribute("error");
48 String success = (String) request.getAttribute("newworkflowinfo");
49 String workflowID = (String) request.getAttribute("workflowid");
50
51
52 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("workflowTutorial");
53 t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "Tutorial"));
54 if (error != null) t.setAttribute("error", error);
55 if (success != null) t.setAttribute("success", success);
56 if (workflowID != null) t.setAttribute("workflowID", workflowID);
57 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
58 out.print(t.toString());
59 }
60
61 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
62
63
64 if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
65
66
67 try {
68 String workflowPath = null;
69 Map<String, String[]> parameters = request.getParameterMap();
70 for (String key : parameters.keySet()) {
71 if (key.endsWith(".x")) {
72 int i = key.indexOf(":");
73 String value = key.substring(i+1,key.length()-2);
74 key = key.substring(0,i);
75 logger.debug("key = " + key + "; value = "+value);
76 if (key.equals("Start")) {
77 workflowPath = value;
78 logger.debug("example workflow " + workflowPath);
79 }
80 }
81 }
82
83 if (workflowPath != null && workflowPath.startsWith("examples")) {
84 String userID = request.getUserPrincipal().getName();
85 String catalinaBase = System.getProperty("catalina.base");
86
87 if (catalinaBase.endsWith("/")) catalinaBase = catalinaBase.substring(0,catalinaBase.length()-1);
88 String xml = readFile(catalinaBase + "/webapps/gwes/" + workflowPath);
89 String workflowid = null;
90 workflowid = initiateWorkflow(userID, xml);
91 HttpSession session = request.getSession();
92
93 logger.debug("session workflow = " + workflowid);
94 session.setAttribute("workflowid", workflowid);
95 request.setAttribute("workflowid", workflowid);
96 StringBuffer newworkflowinfo = new StringBuffer();
97 newworkflowinfo.append("A new workflow with id \"")
98 .append(workflowid)
99 .append("\" has been initiated based on the uploaded workflow description \"")
100 .append(workflowPath)
101 .append("\". ");
102 request.setAttribute("newworkflowinfo", newworkflowinfo.toString());
103
104 } else {
105 request.setAttribute("error", "Could not read file " + workflowPath);
106 }
107
108 } catch (Exception e) {
109 request.setAttribute("error", "Error:" + e);
110 logger.error("Error: " + e, e);
111 } catch (GWESException e) {
112 request.setAttribute("error", "Error:" + e);
113 logger.error("Error: " + e, e);
114 }
115
116 doGet(request, response);
117 }
118
119
120
121
122 private String initiateWorkflow(String userID, String xml) throws Exception, GWESException {
123 return RemoteGWES.getInstance().initiate(xml, userID);
124 }
125
126
127
128
129
130
131
132 private static String readFile(String fileName) throws IOException {
133 if (!SecurityChecker.checkPath(fileName)) {
134 throw new IOException("Not allowed to access filename" + fileName);
135 }
136 StringBuffer buffer = new StringBuffer();
137 BufferedReader fileReader;
138 fileReader = new BufferedReader(new FileReader(fileName));
139
140 String line;
141 line = fileReader.readLine();
142 while (line != null) {
143 buffer.append(line);
144 buffer.append('\n');
145 line = fileReader.readLine();
146 }
147 return buffer.toString();
148 }
149
150 }