View Javadoc

1   /*
2    * $Id: WorkflowDetailsServlet.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 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.ServletException;
17  import javax.servlet.http.HttpServlet;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  import java.net.MalformedURLException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * Servlet that shows details of a specific workflow.
28   * Security should be implemented by means of the Servlet container (e.g. using tomcat-users.xml)
29   *
30   * @author Andreas Hoheisel
31   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
32   * @version $Id: WorkflowDetailsServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
33   */
34  public class WorkflowDetailsServlet extends HttpServlet {
35  
36      private static GWESProperties pr;
37      final static Logger logger = Logger.getLogger(WorkflowDetailsServlet.class);
38  
39      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40          ServletLogger.log(request);
41          
42          response.setContentType("text/html");
43          PrintWriter out = response.getWriter();
44          String error = null;
45          String success = null;
46          String info = null;
47  
48          Object obj = request.getAttribute("error");
49          if (obj instanceof String) error = (String) obj;
50  
51          obj = request.getAttribute("success");
52          if (obj instanceof String) success = (String) obj;
53  
54          obj = request.getAttribute("info");
55          if (obj instanceof String) info = (String) obj;
56  
57          //read gwes.properties
58          if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
59  
60          // get user
61          String userID = request.getRemoteUser();
62          if (userID == null) userID = "nn";
63  
64          // get workflow ID
65          String workflowID = request.getParameter("workflowid");
66          if (workflowID == null) {
67              obj = request.getSession().getAttribute("workflowid");
68              if (obj instanceof String) workflowID = (String) obj;
69          } else {
70              logger.debug("session workflow = " + workflowID);
71              request.getSession().setAttribute("workflowid", workflowID);
72          }
73  
74          WorkflowInformation workflow = null;
75          try {
76              workflow = getWorkflowInformation(workflowID, userID);
77          } catch (Throwable e) {
78              logger.error("exception: " + e, e);
79              error = ((error == null) ? "" : error) + "Could not get workflow information: " + e.getMessage();
80              info = "<p>Please select an available workflow in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a> first!</p>";
81              workflowID = null;
82          }
83  
84          if (error == null && (workflowID == null || workflowID.length() == 0)) {
85              info = "<p>Please select the workflow in the <a href=\"" + pr.gwesBaseUrlExternal + "/servlet/WorkflowListServlet\">Workflow List</a> first!</p>";
86          }
87  
88          try {
89              StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("workflowDetails");
90              if (workflow != null) t.setAttribute("workflow", workflow);
91              if (success != null) t.setAttribute("success", success);
92              if (error != null) t.setAttribute("error", error);
93              if (info != null) t.setAttribute("info", info);
94              t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "Details"));
95              out.print(t.toString());
96          } catch (Exception e) {
97              out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
98              out.println("<pre>");
99              e.printStackTrace(out);
100             out.println("</pre>");
101             out.println("<hr>");
102         }
103     }
104 
105     /**
106      * name="Start"
107      * name="Suspend"
108      * name="Resume"
109      * name="Abort"
110      * name="Remove"
111      * name="Restart"
112      * name="Restore"
113      * name="Checkpoints"
114      * name="Xml"
115      * name="Store"
116      * name="Description"
117      *
118      * @param request
119      * @param response
120      * @throws ServletException
121      * @throws IOException
122      */
123     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
124         int removeLevel = 0;
125         String method = null;
126         String workflowID = null;
127         String[] values = null;
128 
129         //read gwes.properties
130         if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
131 
132         try {
133             // Parse the request
134             Map<String, String[]> parameters = request.getParameterMap();
135             for (String key : parameters.keySet()) {
136                 logger.debug("===========> original key = "+key+" value = "+parameters.get(key));
137                 // remove level checkbox
138                 if (key.startsWith("RemoveLevel:")) {
139                     String removeLevelStr = key.substring(12);
140                     if (removeLevelStr.equals("memory")) removeLevel += 1;
141                     else if (removeLevelStr.equals("database")) removeLevel += 2;
142                     else if (removeLevelStr.equals("filesystems")) removeLevel += 4;
143                 }
144                 // methods with icons
145                 else if (key.endsWith(".x")) {
146                     int i = key.indexOf(":");
147                     workflowID = key.substring(i + 1, key.length() - 2);
148                     method = key.substring(0, i);
149                     logger.debug("method = " + method + "; workflowID = " + workflowID);
150                 }
151                 // other methods
152                 else {
153                     int i = key.indexOf("Description:");
154                     if (i >= 0) {
155                         workflowID = key.substring(i + 12);
156                         method = key.substring(0, i + 11);
157                         values = parameters.get(key);
158                         logger.debug("method = " + method + "; workflowID = " + workflowID);
159                     }
160                 }
161             }
162 
163             // invoke the methods
164             String userID = request.getUserPrincipal().getName();
165             if (method.equals("Start")) {
166                 RemoteGWES.getInstance().start(workflowID, userID);
167             } else if (method.equals("Suspend")) {
168                 RemoteGWES.getInstance().suspend(workflowID, userID);
169             } else if (method.equals("Resume")) {
170                 RemoteGWES.getInstance().resume(workflowID, userID);
171             } else if (method.equals("Abort")) {
172                 RemoteGWES.getInstance().abort(workflowID, userID);
173             } else if (method.equals("Remove")) {
174                 logger.debug("removeLevel="+removeLevel);
175                 RemoteGWES.getInstance().remove(workflowID, removeLevel, request.getUserPrincipal().getName());
176                 // if removed from Memory AND Database then remove focus on workflow
177                 if ((removeLevel & 1) != 0 && (removeLevel & 2) != 0) {
178                     request.getSession().removeAttribute("workflowid");
179                     request.removeAttribute("workflowid");
180                 }
181                 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRemoveWorkflow");
182                 t.setAttribute("workflowID", workflowID);
183                 t.setAttribute("from", Utils.getFromString(removeLevel));
184                 request.setAttribute("success", t.toString());
185             } else if (method.equals("Restart")) {
186                 String workflowid = RemoteGWES.getInstance().restart(workflowID, request.getUserPrincipal().getName());
187                 logger.debug("session workflow = " + workflowid);
188                 request.getSession().setAttribute("workflowid", workflowid);
189                 request.setAttribute("workflowid", workflowid);
190                 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRestartWorkflow");
191                 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
192                 t.setAttribute("workflowID", workflowid);
193                 t.setAttribute("oldworkflowID", workflowID);
194                 request.setAttribute("success", t.toString());
195             } else if (method.equals("Restore")) {
196                 String workflowid = RemoteGWES.getInstance().restore(workflowID, request.getUserPrincipal().getName());
197                 logger.debug("session workflow = " + workflowid);
198                 request.getSession().setAttribute("workflowid", workflowid);
199                 request.setAttribute("workflowid", workflowid);
200                 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("successRestoreWorkflow");
201                 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
202                 t.setAttribute("workflowID", workflowid);
203                 t.setAttribute("oldworkflowID", workflowID);
204                 request.setAttribute("success", t.toString());
205             } else if (method.equals("Checkpoints")) {
206                 String[] checkpoints = RemoteGWES.getInstance().getCheckpoints(workflowID, userID);
207                 StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("infoCheckpoints");
208                 t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
209                 t.setAttribute("checkpoints", checkpoints);
210                 request.setAttribute("info", t.toString());
211             } else if (method.equals("Xml")) {
212                 response.setContentType("text/xml");
213                 PrintWriter out = response.getWriter();
214                 out.print(RemoteGWES.getInstance().getWorkflowDescription(workflowID, userID));
215                 return;
216             } else if (method.equals("Store")) {
217                 RemoteGWES.getInstance().store(workflowID, userID);
218                 request.setAttribute("success", "Workflow checkpoint stored in database.");
219             } else if (method.equals("Description") && values != null && values.length > 0) {
220                 RemoteGWES.getInstance().setDescription(workflowID, values[0], userID);
221                 request.setAttribute("success", "Workflow description changed.");
222             }
223         } catch (Exception e) {
224             request.setAttribute("error", "Exception during workflow action :" + e);
225             logger.error("Exception during workflow action :" + e, e);
226         } catch (GWESException e) {
227             request.setAttribute("error", "Exception during workflow action :" + e);
228             logger.error("Exception during workflow action :" + e, e);
229         }
230 
231         doGet(request, response);
232     }
233 
234     private WorkflowInformation getWorkflowInformation(String workflowID, String userID) throws Exception, GWESException {
235         if (workflowID == null) return null;
236 
237         // get workflow properties from GWES
238         String[][] p = RemoteGWES.getInstance().getProperties(workflowID, userID);
239         Map<String, String> wfprops = new HashMap<String, String>(p.length);
240         for (String[] aP : p) {
241             wfprops.put(aP[0], aP[1]);
242         }
243 
244         // create workflow information object
245         WorkflowInformation wf = new WorkflowInformation(
246                 pr.existUrl,
247                 pr.gwesBaseUrlExternal,
248                 workflowID,
249                 RemoteGWES.getInstance().getDescription(workflowID, userID),
250                 wfprops
251         );
252 
253         return wf;
254     }
255 
256 }