Exemplo n.º 1
0
 @GET
 @Produces("application/json")
 public String index(@PathParam("sid") String systemIdd) {
   String systemId = java.net.URLDecoder.decode(systemIdd);
   Wupsifer w = Wupsifer.getInstance();
   SCSCPClient client = w.getClients().get(systemId);
   if (null == client) return "[]";
   List<Computation> cc = client.getComputations();
   DateFormat df = DateFormat.getInstance();
   JSONStringer js = new JSONStringer();
   try {
     // Begin array
     js.array();
     for (Computation c : cc) {
       js.object();
       js.key("id").value(c.getToken());
       js.key("startedAt").value(df.format(c.getStartedAt()));
       js.key("finishedAt").value(df.format(c.getStartedAt()));
       js.endObject();
     }
     js.endArray();
     // end array
   } catch (Exception e) {
     e.printStackTrace();
     System.exit(1);
   }
   return js.toString();
 }
Exemplo n.º 2
0
 @GET
 @Produces("application/json")
 @Path("/{id}")
 public String show(@PathParam("sid") String systemIdd, @PathParam("id") String idd) {
   String systemId = java.net.URLDecoder.decode(systemIdd);
   String id = java.net.URLDecoder.decode(idd);
   SCSCPClient sys = Wupsifer.getInstance().getClients().get(systemId);
   Computation c = sys.getComputation(id);
   //        System.out.println(" ----> " + c.toString() + " " + id + " " + systemId);
   DateFormat df = DateFormat.getInstance();
   JSONStringer js = new JSONStringer();
   try {
     js.object();
     js.key("id").value(c.getToken());
     js.key("startedAt").value(df.format(c.getStartedAt()));
     js.key("finishedAt").value(df.format(c.getStartedAt()));
     js.key("commandL").value(c.getRequest().toLatex());
     js.key("commandP").value(c.getRequest().toPopcorn());
     js.key("commandX").value(c.getRequest().toXml());
     js.key("resultL").value(c.getResult().toLatex());
     js.key("resultP").value(c.getResult().toPopcorn());
     js.key("resultX").value(c.getResult().toXml());
     js.endObject();
   } catch (Exception e) {
     e.printStackTrace();
     System.exit(1);
   }
   return js.toString();
 }
Exemplo n.º 3
0
  /**
   * Entry point of all the handled actions
   *
   * @param target
   * @param request
   * @param response
   * @param dispatch
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String path = request.getPathInfo();
    System.out.println("REQUEST path: " + path);
    String error = "";
    String res = null;

    if (path.equals("/") || path.equals("/index") || path.equals("/index.html")) {
      response.sendRedirect(HOME);
      return;
    }

    if (path.equals("/compute")) {
      String systemId = request.getParameter("systemId");
      if (null == systemId) {
        error += "ERROR. Must specify \"systemId\". ";
      }
      SCSCPClient client = w.getClients().get(systemId);
      if (null == client) {
        error += "ERROR. No system with systemId '" + systemId + "' found. ";
      }
      String command = request.getParameter("command");
      if (null == command) {
        error += "ERROR. Must specify 'command'. ";
      }
      String outputFormat = request.getParameter("outputFormat");
      if (null == outputFormat) {
        outputFormat = "xml";
      }
      OpenMathBase omb = null;
      try {
        omb = OpenMathBase.parse(command);
      } catch (Exception e) {
        error += "ERROR. Command could not be parsed: " + e.getMessage();
      }
      if (error.length() == 0) {
        OpenMathBase result = null;
        try {
          String token = client.compute(omb);
          while (!client.resultAvailable(token)) {
            try {
              Thread.sleep(80);
            } catch (Exception e) {
            }
          }
          result = client.getResult(token);
        } catch (Exception oops) {
          error = "ERROR. Parsing or Computing failed.";
        }
        if (error.length() == 0) {
          if (outputFormat.equalsIgnoreCase("ALL")) {
            res =
                "POPCORN: "
                    + result.toPopcorn()
                    + "\n\nLATEX: "
                    + result.toLatex()
                    + "\n\nOPENMATH: "
                    + result.toXml()
                    + "\n\n";
          } else if (outputFormat.equalsIgnoreCase("XML")) {
            res = result.toXml();
          } else if (outputFormat.equalsIgnoreCase("POPCORN")) {
            res = result.toPopcorn();
          } else if (outputFormat.equalsIgnoreCase("LATEX")) {
            res = result.toLatex();
          } else {
            error = "Illegal output format: " + outputFormat;
          }
        }
      }
    } else if (path.equals("/print")) {
      String command = request.getParameter("command");
      if (null == command) {
        error += "ERROR. Must specify 'command'. ";
      }
      String outputFormat = request.getParameter("outputFormat");
      if (null == outputFormat) {
        outputFormat = "xml";
      }
      OpenMathBase omb = null;
      try {
        omb = OpenMathBase.parse(command);
      } catch (Exception e) {
        error += "ERROR. Command could not be parsed: " + e.getMessage();
      }
      if (error.length() == 0) {
        OpenMathBase result = omb;
        if (error.length() == 0) {
          if (outputFormat.equalsIgnoreCase("ALL")) {
            res =
                "POPCORN: "
                    + result.toPopcorn()
                    + "\n\nLATEX: "
                    + result.toLatex()
                    + "\n\nOPENMATH: "
                    + result.toXml()
                    + "\n\n";
          } else if (outputFormat.equalsIgnoreCase("XML")) {
            res = result.toXml();
          } else if (outputFormat.equalsIgnoreCase("POPCORN")) {
            res = result.toPopcorn();
          } else if (outputFormat.equalsIgnoreCase("LATEX")) {
            res = result.toLatex();
          } else {
            error = "Illegal output format: " + outputFormat;
          }
        }
      }
    } else {
      response.sendRedirect(HOME);
      return;
    }

    if (res != null && error.length() == 0) {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println(res);
      ((Request) request).setHandled(true);
    } else if (res != null && error.length() > 0) {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println(error);
      ((Request) request).setHandled(true);
    } else {
      response.setContentType("text/html");
      response.setStatus(HttpServletResponse.SC_OK);
      response.getWriter().println("ERROR. Was war denn das?!");
      ((Request) request).setHandled(true);
    }
  }