Example #1
0
  private void parseGetParameters(HttpExchange exchange) throws UnsupportedEncodingException {

    Map<String, Object> parameters = new HashMap<String, Object>();
    URI requestedUri = exchange.getRequestURI();
    String query = requestedUri.getRawQuery();
    parseQuery(query, parameters);
    exchange.setAttribute("parameters", parameters);
  }
  @Override
  public void handle(HttpExchange arg0) throws IOException {
    try {
      String jsons = IOUtils.toString(new InputStreamReader(arg0.getRequestBody(), "UTF-8"));

      JSONObject json = (JSONObject) JSONSerializer.toJSON(jsons);
      String email = json.getString("email");
      String key = json.getString("key");
      int start = json.getInt("start");
      int how_many = json.getInt("how_many");
      String session_id = json.getString("session_id");
      boolean order_by_source = true;
      boolean first_request = json.getBoolean("first");
      boolean taxonomic_constraint = true;

      if (json.containsKey("source_sorting")) {
        order_by_source = json.getBoolean("source_sorting");
      }

      if (json.containsKey("taxonomic_constraint")) {
        taxonomic_constraint = json.getBoolean("taxonomic_constraint");
      }

      TnrsJob job = JobHelper.readJobInfo("/tnrs-jobs/", email, key);

      JSONObject json_res = new JSONObject();

      if (job.getType() == TnrsJob.PARSING_JOB) {

        ParsingResultsFile data = new ParsingResultsFile(job, "/tnrs-jobs/");

        JSONArray results = data.getResultsInterval(start, how_many);

        json_res.put("items", results);
        json_res.put("total", data.getResultsSize());

      } else {
        MatchingResultsFile data =
            new MatchingResultsFile(job, "/tnrs-jobs/", session_id, first_request);

        JSONArray results =
            data.getResultsInterval(start, how_many, job, taxonomic_constraint, order_by_source);

        json_res.put("items", results);
        json_res.put("total", data.getResultsSize());
        data.close();
      }

      String result = json_res.toString();

      arg0.sendResponseHeaders(200, result.getBytes().length);
      arg0.setAttribute("Content-type", "application/json");

      OutputStreamWriter owr = new OutputStreamWriter(arg0.getResponseBody(), "UTF-8");
      BufferedWriter wr = new BufferedWriter(owr);
      wr.write(result);
      wr.flush();
      wr.close();

    } catch (Exception ex) {
      ex.printStackTrace();
      log.error(ExceptionUtils.getFullStackTrace(ex));
      throw new IOException(ex);
    }
  }