@POST
 @Produces("application/json")
 public String create(@PathParam("sid") String systemIdd, @Context UriInfo ui) {
   String systemId = java.net.URLDecoder.decode(systemIdd);
   MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
   String command = queryParams.getFirst("command");
   SCSCPClient sys = Wupsifer.getInstance().getClients().get(systemId);
   Wupsifer w = Wupsifer.getInstance();
   try {
     w.compute(sys, OpenMathBase.parse(command));
   } catch (Exception ignored) {
     return ("Failure");
   }
   return "OK";
 }
Beispiel #2
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);
    }
  }