@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject request = (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody())); String email = request.getString("email"); String key = request.getString("key"); for (int i = 0; i < jobs.size(); i++) { TnrsJob job = jobs.get(i); if (job.getRequest().getId().equals(key) && job.getRequest().getEmail().equals(email)) { JSONObject json = (JSONObject) JSONSerializer.toJSON(job.toJsonString()); json.put("status", "incomplete"); json.put("progress", job.progress()); HandlerHelper.writeResponseRequest(arg0, 200, json.toString(), "application/json"); return; } } if (JobHelper.jobFileExists(baseFolder, email, key)) { TnrsJob job = JobHelper.readJobInfo(baseFolder, email, key); HandlerHelper.writeResponseRequest(arg0, 200, job.toJsonString(), "application/json"); } else { HandlerHelper.writeResponseRequest( arg0, 500, "No such job exists o it might have expired", "text/plain"); } } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); throw new IOException(ex); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject json = (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody())); JSONObject result = new JSONObject(); String email = json.getString("email"); String key = json.getString("key"); for (int i = 0; i < jobs.size(); i++) { TnrsJob job = jobs.get(i); if (job.getRequest().getEmail().equals(email) && job.getRequest().getId().equals(key)) { if (job.status().equals("failed") || job.status().equals("error")) { result.put("type", "failed"); } else { result.put("type", "incomplete"); double progress = job.progress(); if (job.progress() == 100.0) { progress = 99.0; } result.put("progress", progress); } HandlerHelper.writeResponseRequest(arg0, 200, result.toString(), "application/json"); return; } } String filename = baseFolder + email.replace("@", "-").replace(".", "-") + "/result" + key; File results = new File(filename); if (!results.exists()) { result.put("type", "non-existent"); } else { result.put("type", "complete"); TnrsJob job = JobHelper.readJobInfo(baseFolder, email, key); result.put("job_type", job.getTypeString()); } HandlerHelper.writeResponseRequest(arg0, 200, result.toString(), "application/json"); return; } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); throw new IOException(ex); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject command = (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody())); String job_id = command.getString("job_id"); TnrsJob job = null; for (TnrsJob jobc : jobs) { if (jobc.getRequest().getId().equals(job_id)) { job = jobc; break; } } if (job == null) { HandlerHelper.writeResponseRequest(arg0, 500, "Invalid job identifier", "text/plain"); } if (command.getString("command").equals("stop")) { synchronized (job) { job.disable(); } } else if (command.getString("command").equals("pause")) { synchronized (job) { job.pause(); } } else if (command.getString("command").equals("resume")) { synchronized (job) { job.resume(); } } else { HandlerHelper.writeResponseRequest(arg0, 500, "Wrong command", "text/plain"); return; } HandlerHelper.writeResponseRequest(arg0, 200, "", "text/plain"); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { String message = "<html><body><table border=\"1\"><tr><th>Email</th><th>Submitted at:</th><th>Progress:</th><th>Status:</th><th>Job id</th></tr>"; for (int i = 0; i < jobs.size(); i++) { TnrsJob job = jobs.get(i); message += "<tr> <td> " + job.getRequest().getEmail() + "</td><td>" + job.getSubmissionDate() + " </td><td> " + job.progress() + "% </td><td> " + job.status() + "</td><td>" + job.getRequest().getId() + "</td></tr>\n"; } message += "</table>"; HandlerHelper.writeResponseRequest(arg0, 200, message, "text/html"); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); throw new IOException(ex); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject json = (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody())); String contents = json.getString("names"); byte[] fileContents = contents.getBytes("UTF-8"); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); GZIPOutputStream gzipStr = new GZIPOutputStream(byteArray); gzipStr.write(fileContents); gzipStr.close(); ByteArrayOutputStream byteArray2 = new ByteArrayOutputStream(); Base64OutputStream base64 = new Base64OutputStream(byteArray2); base64.write(byteArray.toByteArray()); base64.close(); String value = new String(byteArray2.toByteArray()); json.remove("names"); json.put("upload", value); HttpClient client = new HttpClient(); PostMethod post = new PostMethod( "http://" + properties.getProperty("org.iplantc.tnrs.servicesHost") + "/tnrs-svc/upload"); post.setRequestEntity(new StringRequestEntity(json.toString(), "text/plain", "UTF-8")); client.executeMethod(post); HandlerHelper.writeResponseRequest(arg0, 200, "", "text/plain"); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); ex.printStackTrace(); throw new IOException(ex); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject json = new JSONObject(); JSONArray array = new JSONArray(); for (int i = 0; i < jobs.size(); i++) { JSONObject job_info = new JSONObject(); TnrsJob job = jobs.get(i); job_info.put("email", job.getRequest().getEmail()); job_info.put("progress", job.progress()); array.add(job_info); } json.put("jobs", array); HandlerHelper.writeResponseRequest(arg0, 200, json.toString(), "application/json"); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); throw new IOException(ex); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { JSONObject json = (JSONObject) JSONSerializer.toJSON(IOUtils.toString(arg0.getRequestBody())); String email = json.getString("email"); String key = json.getString("key"); String session_id = json.getString("session_id"); TnrsJob job = JobHelper.readJobInfo(baseFolder, email, key); if (job.getType() == TnrsJob.PARSING_JOB) { ParsingResultsFile results = new ParsingResultsFile(job, baseFolder); results.createFileForDownload(properties.getProperty("org.iplantc.folder.tmp")); } else { MatchingResultsFile results = new MatchingResultsFile(job, baseFolder, session_id, false); results.createFileForDownload( properties.getProperty("org.iplantc.tnrs.folder.tmp"), json); results.close(); } String url = servicesUrl + "getcsv?id=" + key; HandlerHelper.writeResponseRequest(arg0, 200, url, null); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); ex.printStackTrace(); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { String request = IOUtils.toString(arg0.getRequestBody()); JSONObject datas = (JSONObject) JSONSerializer.toJSON(request); UUID uid = UUID.randomUUID(); HashMap<String, String> info = new HashMap<String, String>(); info.put("email", datas.getString("email")); info.put("institution", ""); info.put("name", ""); info.put("sensitivity", datas.getString("sensitivity")); info.put("has_id", datas.getString("has_id")); info.put("id", uid.toString().replace("-", "")); info.put("type", datas.getString("type")); info.put("sources", datas.getString("sources")); info.put("classification", datas.getString("classification")); info.put("match_to_rank", datas.getString("match_to_rank")); /** * * * * <p>To be replaced by better code */ byte[] data = null; if (datas.has("upload")) { data = IOUtils.toByteArray( new GZIPInputStream( new Base64InputStream( new ByteArrayInputStream(datas.getString("upload").getBytes())))); } else if (datas.has("names")) { data = datas.getString("names").getBytes(); } else { HandlerHelper.writeResponseRequest(arg0, 500, "Invalid request!", null); return; } UniversalDetector detector = new UniversalDetector(null); detector.handleData(data, 0, data.length); detector.dataEnd(); String content = ""; String encoding = detector.getDetectedCharset(); if (encoding != null) { if (encoding.equals("WINDOWS-1252")) { encoding = "ISO-8859-1"; ByteArrayOutputStream t = new ByteArrayOutputStream(); OutputStreamWriter wr = new OutputStreamWriter(t, "UTF-8"); wr.write(new String(data, encoding)); wr.close(); content = t.toString("UTF-8"); } else { content = new String(data, "UTF-8"); } } else { content = new String(data, "UTF-8"); } info.put("upload", content); /** */ JSONObject json = createJSONJobRequest(info); json.put("submitted_date", new Date().toString().replace(":", "")); json.put("original", datas.getString("file_name")); json.put("sensitivity", datas.getString("sensitivity")); TnrsJob job = submitJob(json.toString()); job.setTaxonomic(datas.getBoolean("taxonomic")); job.setEmail(!info.containsKey("noemail")); job.setSources(info.get("sources")); job.setClassification(info.get("classification")); job.setAllowPartial(Boolean.parseBoolean(info.get("match_to_rank"))); log.info(job.getRequest().getId() + " " + job.getRequest().getEmail()); HandlerHelper.writeResponseRequest(arg0, 200, job.getRequest().getId(), null); if (info.get("email").trim().equals("*****@*****.**") || !job.email()) return; try { sendSubmissionEmail(job); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); ex.printStackTrace(); } } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); ex.printStackTrace(); throw new IOException(ex); } }