View Javadoc

1   /*
2    * $Id: GWUIServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
3    *
4    * Copyright (c) 2005-2006, The K-Wf Grid Consortium
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.kwfgrid.eu and 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.log4j.Logger;
13  
14  import javax.servlet.ServletException;
15  import javax.servlet.http.HttpServlet;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.io.IOException;
19  import java.io.PrintWriter;
20  import java.net.MalformedURLException;
21  
22  import net.kwfgrid.gwes.client.RemoteGWES;
23  import net.kwfgrid.gwes.exception.GWESException;
24  
25  /**
26   * Servlet for creating html for the GWUI applet.
27   *
28   * @author Andreas Hoheisel
29   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
30   * @version $Id: GWUIServlet.java 1537 2011-07-27 15:34:04Z hoheisel $
31   */
32  public class GWUIServlet extends HttpServlet {
33  
34      final static Logger logger = Logger.getLogger(GWUIServlet.class);
35      private static GWESProperties pr;
36  
37      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
38          ServletLogger.log(request);
39  
40          //read gwes.properties
41          if (pr == null) pr = GWESProperties.getInstance("/gwes.properties");
42  
43          response.setContentType("text/html");
44          PrintWriter out = response.getWriter();
45          String error = null;
46          String info = null;
47  
48          // get user
49          String userID = request.getRemoteUser();
50          if (userID == null) userID = "nn";
51  
52          // get workflow ID
53          String workflowID = request.getParameter("workflowid");
54          if (workflowID == null) {
55              workflowID = (String) request.getSession().getAttribute("workflowid");
56          } else {
57              logger.debug("session workflow = "+workflowID);
58              request.getSession().setAttribute("workflowid",workflowID);
59          }
60          if (workflowID == null || workflowID.length() == 0) {
61              info = "Please select the workflow in the <a href=\""+pr.gwesBaseUrlExternal+"/servlet/WorkflowListServlet\">Workflow List</a> first!";
62          }
63  
64          try {
65              // check workflowID
66              if (workflowID != null && !checkWorkflowID(workflowID, request.getUserPrincipal().getName())) {
67                  error = "Not able to display the workflow graph, because the selected Workflow is not in memory! Please select a workflow with level \"MEMORY\" from the <a href=\""+pr.gwesBaseUrlExternal+"/servlet/WorkflowListServlet\">Workflow List</a> or upload a <a href=\""+pr.gwesBaseUrlExternal+"/servlet/WorkflowUploadServlet\">New Workflow</a>";
68                  workflowID = null;
69              }
70          } catch (Throwable e) {
71              error = "Not able to access the GWES: "+e;
72          }
73  
74  
75          try {
76              StringTemplate t = Template.getStringTemplateGroup("html_gwes-servlets").getInstanceOf("graph");
77              t.setAttribute("menu", new Menu(pr.gwesBaseUrlExternal, "Graph"));
78              if (error != null) t.setAttribute("error", error);
79              if (info != null) t.setAttribute("info", info);
80              t.setAttribute("width", pr.width);
81              t.setAttribute("height", pr.height);
82              if (workflowID != null) t.setAttribute("workflowID", workflowID);
83              t.setAttribute("userID", userID);
84              t.setAttribute("gwesBaseUrlExternal", pr.gwesBaseUrlExternal);
85              t.setAttribute("graphvizServiceUri", pr.graphvizServiceUri);
86              t.setAttribute("gworkflowdlXsdPath", pr.gworkflowdlXsdPath);
87              out.print(t.toString());
88          } catch (Exception e) {
89              out.println("<hr><b>ERROR connecting to GWES: " + e + "</b>");
90              out.println("<pre>");
91              e.printStackTrace(out);
92              out.println("</pre>");
93              out.println("<hr>");
94          }
95      }
96  
97      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
98          doGet(request, response);
99      }
100 
101     private boolean checkWorkflowID(String workflowID, String userID) throws Exception, GWESException {
102         String[] workflowIDs = RemoteGWES.getInstance().getWorkflowIDs(1, userID);
103         if (workflowIDs == null) return false;
104         for (String wf : workflowIDs) {
105             if (wf.equals(workflowID)) return true;
106         }
107         return false;
108     }
109 
110 }