@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); } }
private User getUserFromJsonResponse(String accessToken) { User user = new User(); HttpClient httpclient = new DefaultHttpClient(); try { if (accessToken != null && !"".equals(accessToken)) { String newUrl = "https://graph.facebook.com/me?access_token=" + accessToken; httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(newUrl); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); JSONObject json = (JSONObject) JSONSerializer.toJSON(responseBody); String facebookId = json.getString("id"); URL imageURL = new URL("http://graph.facebook.com/" + facebookId + "/picture?type=large"); URLConnection conn = imageURL.openConnection(); ((HttpURLConnection) conn).setInstanceFollowRedirects(false); String imageLocation = conn.getHeaderField("Location"); user.setFirstName(json.getString("first_name")); user.setLastName(json.getString("last_name")); user.setEmail(json.getString("email")); user.setImage(imageLocation); } } catch (ClientProtocolException e) { log.error(e); } catch (IOException e) { log.error(e); } finally { httpclient.getConnectionManager().shutdown(); } return user; }
protected JSONObject representationToJSONObject(Representation representation) throws ResourceException { String posted; try { posted = representation.getText(); } catch (IOException e) { throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "IOException on input."); } if (!JSONUtils.mayBeJSON(posted)) { throw error(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE, "Please PUT in JSON format."); } JSONObject json; try { JSON input = JSONSerializer.toJSON(posted); if (!input.isArray()) { json = (JSONObject) input; } else { throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "Please send a proper JSON object."); } } catch (JSONException e) { throw error(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE, "JSON format required."); } return json; }
public String showJson() { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonBeanProcessor( Sucursal.class, new JsonBeanProcessor() { @Override public JSONObject processBean(Object bean, JsonConfig jsonConfig) { Sucursal sucursal = (Sucursal) bean; JSONObject row = new JSONObject(); row.element("id", sucursal.getId()); JSONArray cell = new JSONArray(); cell.add(sucursal.getNombre()); cell.add(sucursal.getDireccion()); cell.add(sucursal.getTelefono()); row.element("cell", cell); return row; } }); JSONObject rootObj = new JSONObject(); JSONArray jrows = (JSONArray) JSONSerializer.toJSON(sucursales, jsonConfig); rootObj.element("rows", jrows); rootObj.element("records", sucursales.size()); rootObj.element("total", Math.ceil((double) sucursales.size() / rows)); rootObj.element("page", page); writeResponse(rootObj); return JSON; }
@Override public void run() { try { StopWatch stp2 = new StopWatch(); stp2.start(); JSONObject json = new JSONObject(); job.setStatus("running"); if (job.progress() == 100.0) { finalizeJob(job); return; } Vector<String> ids = new Vector<String>(); Vector<String> original_names = new Vector<String>(); String data = job.getNextDataBatch(); if (data == null || data.equals("")) return; String[] lines = data.split("\n"); if (job.containsId()) { for (int i = 0; i < lines.length; i++) { if (lines[i].trim().equals("")) continue; ids.add(NameUtil.getNameId(lines[i])); } } for (int i = 0; i < lines.length; i++) { original_names.add(NameUtil.processName(lines[i], job.containsId())); } String names = NameUtil.CleanNames(lines, job); if (names.equals("")) return; if (job.getType() == TnrsJob.NAME_MATCH_JOB) { TaxamatchInterface taxa_match = new TaxamatchInterface(tnrsBaseUrl); String result = taxa_match.queryTaxamatch(names, job); json = (JSONObject) JSONSerializer.toJSON(result); } else if (job.getType() == TnrsJob.PARSING_JOB) { json = gni_interface.parseNames(names); } if (job.outstandingNames() == 0) { JobHelper.persistJobInfo(baseFolder, job); } saveResults(job, json, ids, original_names, ""); job.setStatus("idle"); stp2.stop(); log.info("overall :" + stp2.toString()); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); job.setStatus("failed"); ex.printStackTrace(); } }
@Override public void handle(HttpExchange arg0) throws IOException { try { String jsons = IOUtils.toString(arg0.getRequestBody()); JSONObject json = (JSONObject) JSONSerializer.toJSON(jsons); if (!json.containsKey("valid")) return; Email response = new SimpleEmail(); response.setHostName(properties.getProperty("org.iplantc.tnrs.mail.host")); response.setSmtpPort( Integer.parseInt(properties.getProperty("org.iplantc.tnrs.mail.port"))); response.setFrom("*****@*****.**"); response.setSubject("TNRS support Ticket"); response.setMsg( "TNRS support ticket from: " + json.getString("name") + " (" + json.getString("email") + "). " + "\n\n\n" + json.getString("contents")); response.addTo("*****@*****.**"); response.send(); } catch (Exception ex) { log.error(ExceptionUtils.getFullStackTrace(ex)); throw new IOException(ex); } }
@Override protected void onTextMessage(CharBuffer message) throws IOException { String temp = message.toString(); JSONObject json = (JSONObject) JSONSerializer.toJSON(temp); String type = json.getString("type").toLowerCase().replaceAll(" ", ""); String location = json.getString("location"); String lat = json.getString("lat"); String lng = json.getString("lng"); String radius = json.getString("radius"); String keywords = json.getString("keywords"); String result = null; try { if (type.equals("overview")) result = getOverViewData(location, lat, lng, radius, keywords); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult.toString(); } if (result == null || result.equals("")) { JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); result = jsonResult.toString(); } Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); CharBuffer uCharBuffer = CharBuffer.wrap(result); ByteBuffer bbuf = encoder.encode(uCharBuffer); CharBuffer cbuf = decoder.decode(bbuf); getWsOutbound().writeTextMessage(cbuf); }
private JSONObject reformLastestTrafficData(String sensorURL, JSONArray filterArr) { JSONObject result = new JSONObject(); try { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(sensorURL); if (sensor == null) return result; Observation obs = sensorManager.getNewestObservationForOneSensor(sensorURL); JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); OutputStream out = null; out = DatabaseUtilities.getNewestSensorData(sensorURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(out.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); System.out.println(json); result = json; } catch (Exception e) { e.printStackTrace(); JSONObject jsonResult = new JSONObject(); jsonResult.put("error", "true"); jsonResult.put("ntriples", ""); result = jsonResult; } return result; }
protected AccessTokenResponse getAccessToken( Client client, PartnerConfiguration partnerConfiguration) throws UnsupportedEncodingException { String tokenParams = String.format( "grant_type=client_credentials&scope=all&client_id=%s&client_secret=%s", partnerConfiguration.userName, partnerConfiguration.password); ClientResponse postResponse = client .resource(TOKEN_URL) .entity(tokenParams, MediaType.APPLICATION_FORM_URLENCODED_TYPE) .header( "Authorization", basic(partnerConfiguration.userName, partnerConfiguration.password)) .post(ClientResponse.class); String json = postResponse.getEntity(String.class); JSONObject accessToken = (JSONObject) JSONSerializer.toJSON(json); AccessTokenResponse response = new AccessTokenResponse(); response.setAccessToken(accessToken.getString("access_token")); response.setExpiresIn(accessToken.getLong("expires_in")); response.setRefreshToken(accessToken.getString("refresh_token")); response.setTokenType(accessToken.getString("token_type")); return response; }
@RequestMapping(value = "/BoothRentFeeList", produces = "application/json") @ResponseBody public Map<String, Object> boothInfoList(HttpServletRequest request) throws Exception { Map<String, Object> model = new HashMap<String, Object>(); HashMap<String, String> param = new HashMap<String, String>(); List<BoothRentFeeDto> list = null; String groupId = request.getParameter("groupId"); String boothId = request.getParameter("boothId"); String rentYear = request.getParameter("rentYear"); String rentMonth = request.getParameter("rentMonth"); String rentFeeType = request.getParameter("rentFeeType"); String payOnsite = request.getParameter("payOnsite"); if (StringUtils.isNotBlank(groupId)) param.put("groupId", groupId); if (StringUtils.isNotBlank(boothId)) param.put("boothId", boothId); if (StringUtils.isNotBlank(rentYear)) param.put("rentYear", rentYear); if (StringUtils.isNotBlank(rentMonth)) param.put("rentMonth", rentMonth); if (StringUtils.isNotBlank(rentFeeType)) param.put("rentFeeType", rentFeeType); if (StringUtils.isNotBlank(payOnsite)) param.put("payOnsite", payOnsite); list = boothRentFee.getRentFeeList(param); JSONArray jsonList = new JSONArray(); jsonList = JSONArray.fromObject(JSONSerializer.toJSON(list)); model.put("jsonList", jsonList); return model; }
/** * @param jsontxt * @return Autor[] - Extract autorun entries from Json representation. */ public static Autorun[] extractAutorun(String jsontxt) { JSONObject jsonAutorunlist = (JSONObject) JSONSerializer.toJSON(jsontxt); // JSONObject Offline = json.getJSONObject("hooked-browsers").getJSONObject("offline"); Autorun Autorunlist[] = new Autorun[jsonAutorunlist.size()]; if (jsonAutorunlist.isEmpty()) { System.out.println("No Autorun, may be list is empty !"); } else { for (int i = 0; i < jsonAutorunlist.size(); i++) { try { Autorun Autor = new Autorun(); JSONObject Autorid = jsonAutorunlist.getJSONObject("" + i); Autor.setId(Autorid.getString("id")); Autor.setName(Autorid.getString("name")); Autor.setCc(Autorid.getString("cc")); Autor.setBrowser(Autorid.getString("browser")); Autor.setParam(Autorid.getString("Param")); Autor.setCategory(Autorid.getString("category")); Autorunlist[i] = Autor; } catch (JSONException e) { System.out.println("ERROR: " + e); } } } return Autorunlist; }
private JSONResult parse(String jsonString) { JSONResult ret = new JSONResult(); JSON json = JSONSerializer.toJSON(jsonString); JSONObject jo = (JSONObject) json; ret.total = jo.getInt("totalCount"); Set<String> names; JSONArray arrResults = jo.optJSONArray("results"); if (arrResults != null) { names = getArray(arrResults); } else { JSONObject results = jo.optJSONObject("results"); if (results != null) { names = Collections.singleton(getSingle(results)); } else { LOGGER.warn("No results found"); names = Collections.EMPTY_SET; } } ret.names = names; ret.returnedCount = names.size(); return ret; }
/** * vraag JSON data op uit Sense * * @param type soort JSON data (devices, sensors, sensor data etc) 1. = devices 2. = sensors 3. = * sensor data * @return JSONObject */ public JSONObject executeJSON(JSON_TYPES type) throws Exception { switch (type) { case device: sense_login = new URL("http://api.sense-os.nl/devices.json"); break; case sensor: sense_login = new URL("http://api.sense-os.nl/sensors.json"); break; default: sense_login = null; } connection = (HttpURLConnection) sense_login.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("X-SESSION_ID", session_id); connection.setConnectTimeout(5000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder strbuilder = new StringBuilder(); while ((inputLine = in.readLine()) != null) strbuilder.append(inputLine); in.close(); JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(strbuilder.toString()); return jsonObject; }
public void run() { try { File file = new File( Globals.CONFIG_FILE_DIR + "/" + TalesSystem.getFolderGitBranchName("~/tales-templates/") + ".json"); String data = FileUtils.readFileToString(file); Config.json = (JSONObject) JSONSerializer.toJSON(data); Thread.sleep(Globals.RELOAD_CONFIG_INTERNAL); Thread t = new Thread(this); t.start(); } catch (Exception e) { try { new TalesException(new Throwable(), e); } catch (Exception e1) { e.printStackTrace(); } } }
public ERXRestRequestNode parseRestRequest( IERXRestRequest request, ERXRestFormat.Delegate delegate) { ERXRestRequestNode rootRequestNode = null; String contentString = request.stringContent(); if (contentString != null && contentString.length() > 0) { // MS: Support direct updating of primitive type keys -- so if you don't want to // wrap your request in XML, this will allow it // if (!contentStr.trim().startsWith("<")) { // contentStr = "<FakeWrapper>" + contentStr.trim() + "</FakeWrapper>"; // } ERXMutableURL url = new ERXMutableURL(); try { url.setQueryParameters(contentString); } catch (MalformedURLException e) { throw new RuntimeException(e); } String requestJSON = url.queryParameter("requestJSON"); JSON rootJSON = JSONSerializer.toJSON(requestJSON, ERXJSONRestWriter._config); System.out.println("ERXGianduiaRestParser.parseRestRequest: " + rootJSON); rootRequestNode = ERXJSONRestParser.createRequestNodeForJSON(null, rootJSON, true, delegate); } else { rootRequestNode = new ERXRestRequestNode(null, true); rootRequestNode.setNull(true); } return rootRequestNode; }
private String getOverViewData( String location, String lat, String lng, String radius, String keywords) { JSONObject json = new JSONObject(); SensorManager sensorManager = new SensorManager(); ArrayList<Sensor> sensors = sensorManager.getAllSpecifiedSensorAroundPlace("traffic", lng, lat, radius); ArrayList<String> arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("traffic", arr); sensors = sensorManager.getAllSpecifiedSensorAroundPlace("airport", lng, lat, "20"); arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("airport", arr); sensors = sensorManager.getAllSpecifiedSensorAroundPlace("railwaystation", lng, lat, radius); arr = new ArrayList<>(); for (Sensor sensor : sensors) { arr.add(sensor.getSource()); } json.put("railwaystation", arr); String sindice = DatabaseUtilities.getSindiceSearch(keywords); JSONObject jsonSindice = (JSONObject) JSONSerializer.toJSON(sindice); String[] aryStrings = {"Title", "Formats", "Updated"}; jsonSindice.put("vars", aryStrings); json.put("sindice", jsonSindice); return json.toString(); }
public Result queryMenu(Request request) { MenuItem menu = menuSrv.getMenuByRoles(CapSecurityContext.getRoleIds()); if (menu != null) { return new AjaxFormResult(JSONSerializer.toJSON(menu).toString()); } return new AjaxFormResult(); }
/** * Attempts to parse the string as JSON. returns true on success * * @param possiblyJson A string that may be JSON * @return true or false. True if string is valid JSON. */ private boolean isTaskJson(String possiblyJson) { try { JSONSerializer.toJSON(possiblyJson); return true; } catch (JSONException ex) { return false; } }
/** Get the Facebook profile details of a user */ public String getUserDetails(String accessToken) throws IOException { String detailsURL = getUserDetailsURL(accessToken); URL details = new URL(detailsURL); String userDetails = HTTP.readURL(details, "getUserDetails"); userDetailsJson = (JSONObject) JSONSerializer.toJSON(userDetails); // Returns JSON text of the user's facebook details, given the user's access token return userDetails; }
public Facebook(String accessToken) { userDetails = null; try { userDetails = getUserDetails(accessToken); } catch (IOException e) { e.printStackTrace(); } json = (JSONObject) JSONSerializer.toJSON(userDetails); }
private AchievementBean[] parseAchievementsJSon(String json) { JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(Category.class)); JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(Difficulty.class)); JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); jsonConfig.setRootClass(AchievementBean.class); return (AchievementBean[]) JSONSerializer.toJava(jsonArray, jsonConfig); }
private JSONObject duplicateJSONObject(JSONObject source) { String json = source.toString(); JSONObject out = null; out = (JSONObject) JSONSerializer.toJSON(json); out.put("duplicate", ""); clearAllMappings(out); return out; }
public static void main(String[] args) throws Exception { InputStream is = ConvertJSONtoXMLNoHints.class.getResourceAsStream("sample-json.txt"); String jsonData = IOUtils.toString(is); XMLSerializer serializer = new XMLSerializer(); JSON json = JSONSerializer.toJSON(jsonData); serializer.setTypeHintsEnabled(false); String xml = serializer.write(json); System.out.println(xml); }
/** * json to xml {"node":{"key":{"@label":"key1","#text":"value1"}}} conver <o><node * class="object"><key class="object" label="key1">value1</key></node></o> * * @param json * @return */ public static String jsontoXml(String json) { try { XMLSerializer serializer = new XMLSerializer(); JSON jsonObject = JSONSerializer.toJSON(json); return serializer.write(jsonObject); } catch (Exception e) { e.printStackTrace(); } return null; }
@Override protected final void renderMergedOutputModel( Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception { if (map == null || map.isEmpty()) { JSONObject.fromObject("{}").write(response.getWriter()); return; } JSON json = JSONSerializer.toJSON(map); json.write(response.getWriter()); }
private JSONObject extractTika(String contents) { JSONObject jObj = (JSONObject) JSONSerializer.toJSON(contents); if (jObj.containsKey("_source")) { JSONObject jObjSource = jObj.getJSONObject("_source"); if (jObjSource.containsKey("raw_content")) { String rawHtml = jObjSource.getString("raw_content"); ByteArrayInputStream bIs = new ByteArrayInputStream(rawHtml.getBytes()); Metadata metadata = new Metadata(); AutoDetectParser adp = new AutoDetectParser(); ContentHandler handler = new BodyContentHandler(10 * 1024 * 1024); try { adp.parse(bIs, handler, metadata); String[] metadataNames = metadata.names(); JSONObject jObjMetadata = new JSONObject(); for (String metadataName : metadataNames) { String[] values = metadata.getValues(metadataName); JSONArray jArray = new JSONArray(); for (String mValue : values) { jArray.add(mValue); } jObjMetadata.accumulate(metadataName, jArray); } // remove empty lines from the text String rawTextAdjusted = handler.toString().replaceAll("(?m)^[ \t]*\r?\n", ""); // detect language LanguageIdentifier li = new LanguageIdentifier(rawTextAdjusted); jObjSource.accumulate("tikametadata", jObjMetadata); jObjSource.accumulate("raw_text", rawTextAdjusted); jObjSource.accumulate("rawtextdetectedlanguage", li.getLanguage()); } catch (Exception e) { LOG.error("Error:", e); ; } } } return jObj; }
private JSONObject getJSONObjectFromWalletExplorerResponseOrThrowException( ClientResponse response) { String jsonStr = response.getEntity(String.class); try { JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonStr); return json; } catch (JSONException e) { System.err.println("Could not make json string from: " + jsonStr); throw e; } }
private JSONObject reformLastestAirportData(String airportURL) { SensorManager sensorManager = new SensorManager(); Sensor sensor = sensorManager.getSpecifiedSensorWithSensorId(airportURL); JSONObject j = new JSONObject(); if (sensor == null) return j; JSONObject metaJson = new JSONObject(); metaJson.put("name", sensor.getName()); metaJson.put("source", sensor.getSource()); metaJson.put("sourceType", sensor.getSourceType()); metaJson.put("city", sensor.getPlace().getCity()); metaJson.put("country", sensor.getPlace().getCountry()); String airportCode = sensor .getName() .substring(sensor.getName().lastIndexOf("(") + 1, sensor.getName().lastIndexOf(")")); ArrayList<ArrayList> depatures = sensorManager.getAllFlightsWithSpecifiedDeparture(airportCode); ArrayList<ArrayList> arrivals = sensorManager.getAllFlightsWithSpecifiedDestination(airportCode); ArrayList<List> radarFlights = FlightAroundXMLParser.getFlightAroundInformation( sensor.getPlace().getLat(), sensor.getPlace().getLng()); JSONObject depaturesJson = new JSONObject(); String[] aryStrings = {"CallSign", "FlightNumber", "Route"}; depaturesJson.put("vars", aryStrings); depaturesJson.put("data", depatures); JSONObject arrivalsJson = new JSONObject(); arrivalsJson.put("vars", aryStrings); arrivalsJson.put("data", arrivals); JSONObject radarFlightsJson = new JSONObject(); String[] flightStrings = { "Registration", "Model", "CallSign", "Route", "Latitude", "Longitude", "Distance(km)" }; radarFlightsJson.put("vars", flightStrings); radarFlightsJson.put("data", radarFlights); OutputStream result = null; result = DatabaseUtilities.getSensorMetadata(airportURL); JSONObject json = (JSONObject) JSONSerializer.toJSON(result.toString()); json.put("updated", DateUtil.date2StandardString(new Date())); json.put("ntriples", JSONUtil.JSONToNTriple(json.getJSONObject("results"))); json.put("error", "false"); json.put("meta", metaJson); json.put("departures", depaturesJson); json.put("arrivals", arrivalsJson); json.put("radars", radarFlightsJson); System.out.println(json); return json; }
public void executeQuery(String query, int pageSize, String index, String docType, int docLimit) { Search search = new Search.Builder(query) .addIndex(index) .addType(docType) .setParameter(Parameters.SEARCH_TYPE, SearchType.SCAN) .setParameter(Parameters.SIZE, pageSize) .setParameter(Parameters.SCROLL, SCROLL) .build(); System.out.println(query + "$$$$"); try { JestResult searchResult = client.execute(search); // System.out.println(searchResult.getJsonString()); String scrollId = searchResult.getJsonObject().get("_scroll_id").getAsString(); int currentResultSize = 0; int numDocs = 0; do { JSONArray jArrayResult = new JSONArray(); SearchScroll scrollRequest = new SearchScroll.Builder(scrollId, SCROLL) .setParameter(Parameters.SIZE, pageSize) .build(); JestResult scrollResult = client.execute(scrollRequest); scrollId = scrollResult.getJsonObject().get("_scroll_id").getAsString(); JSONObject jObj = (JSONObject) JSONSerializer.toJSON(scrollResult.getJsonString()); JSONArray jArrayHits = jObj.getJSONObject("hits").getJSONArray("hits"); for (int i = 0; i < jArrayHits.size(); i++) { jArrayResult.add(extractTika(jArrayHits.getString(i)).toString()); } writeToFile(jArrayResult); // Note: Current result size will be Page Size * number of shards currentResultSize = jArrayHits.size(); numDocs += currentResultSize; System.out.println("num docs:" + String.valueOf(numDocs)); if (docLimit != -1 && numDocs >= docLimit) { break; } } while (currentResultSize != 0); } catch (IOException e) { LOG.error("Error retrieving from Elasticsearch", e); } }
@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); } }