/** Simple méthode utilitaire. */ private static String formatJsonString(String json) { try { return new JSONObject(json).toString(2); } catch (JSONException ex) { throw new RuntimeException(ex.getMessage(), ex); } }
public void sendJson(Object jsonData) throws Log4AllException { HttpPut addLogPut; if (jsonData instanceof JSONArray) { addLogPut = new HttpPut(url + "/api/logs"); } else { addLogPut = new HttpPut(url + "/api/log"); } String rawResponse = ""; JSONObject jsonResp; try { JSONObject jsonReq; if (jsonData instanceof JSONArray) { jsonReq = new JSONObject(); jsonReq.put("logs", jsonData); } else { jsonReq = (JSONObject) jsonData; } jsonReq.put("application", this.application); if (token != null) { jsonReq.put("application_token", this.token); } HttpEntity postData = new StringEntity(jsonReq.toString()); addLogPut.setEntity(postData); HttpResponse resp = getHttpClient().execute(addLogPut); rawResponse = IOUtils.toString(resp.getEntity().getContent()); jsonResp = new JSONObject(rawResponse); if (!jsonResp.getBoolean("success")) { throw new Log4AllException(jsonResp.getString("message")); } } catch (IOException e) { throw new Log4AllException(e.getMessage() + "httpResp:" + rawResponse, e); } catch (JSONException e) { throw new Log4AllException(e.getMessage() + "httpResp:" + rawResponse, e); } }
/** * Get the URL of the file to download. This has not been modified from the original version. * * <p>For more information, please see: <a href= * "https://collegereadiness.collegeboard.org/educators/higher-ed/reporting-portal-help#features"> * https://collegereadiness.collegeboard.org/educators/higher-ed/reporting- * portal-help#features</a> * * <p>Original code can be accessed at: <a href= * "https://collegereadiness.collegeboard.org/zip/pascoredwnld-java-sample.zip"> * https://collegereadiness.collegeboard.org/zip/pascoredwnld-java-sample.zip </a> * * @see #login(String, String) * @see org.collegeboard.scoredwnld.client.FileInfo * @author CollegeBoard * @param accessToken Access token obtained from {@link #login(String, String)} * @param filePath File to download * @return FileInfo descriptor of file to download */ private FileInfo getFileUrlByToken(String accessToken, String filePath) { Client client = getClient(); WebResource webResource = client.resource( scoredwnldUrlRoot + "/pascoredwnld/file?tok=" + accessToken + "&filename=" + filePath); ClientResponse response = webResource.accept("application/json").get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } try { JSONObject json = new JSONObject(response.getEntity(String.class)); FileInfo fileInfo = new FileInfo(); fileInfo.setFileName(filePath); fileInfo.setFileUrl(String.valueOf(json.get("fileUrl"))); return fileInfo; } catch (ClientHandlerException e) { log("Error: " + e.getMessage()); e.printStackTrace(); } catch (UniformInterfaceException e) { log("Error: " + e.getMessage()); e.printStackTrace(); } catch (JSONException e) { log("Error: " + e.getMessage()); e.printStackTrace(); } return null; }
public void log(String msg, String level, String stack) throws Log4AllException { JSONObject jsonData = null; try { jsonData = toJSON(msg, level, stack, new Date()); log(jsonData); } catch (JSONException e) { throw new Log4AllException(e.getMessage(), e); } }
private void generateResults(MessageContext messageContext, boolean resultStatus) { ResultPayloadCreater resultPayload = new ResultPayloadCreater(); String responce = "<result><success>" + resultStatus + "</success></result>"; try { OMElement element = resultPayload.performSearchMessages(responce); resultPayload.preparePayload(messageContext, element); } catch (XMLStreamException e) { log.error(e.getMessage()); handleException(e.getMessage(), messageContext); } catch (IOException e) { log.error(e.getMessage()); handleException(e.getMessage(), messageContext); } catch (JSONException e) { log.error(e.getMessage()); handleException(e.getMessage(), messageContext); } }
@Override public synchronized void deliver(Interest i, JSONObject payload) { try { JSONObject obj = new JSONObject(); obj.put("deliveryFor", i.handle); obj.put("payload", payload); send(obj); } catch (JSONException ex) { sendError(ex.getMessage()); } }
@Override public JSONObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JSONObject ob = new JSONObject(); JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); } for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) { String fieldName = jp.getCurrentName(); t = jp.nextToken(); try { switch (t) { case START_ARRAY: ob.put(fieldName, JSONArrayDeserializer.instance.deserialize(jp, ctxt)); continue; case START_OBJECT: ob.put(fieldName, deserialize(jp, ctxt)); continue; case VALUE_STRING: ob.put(fieldName, jp.getText()); continue; case VALUE_NULL: ob.put(fieldName, JSONObject.NULL); continue; case VALUE_TRUE: ob.put(fieldName, Boolean.TRUE); continue; case VALUE_FALSE: ob.put(fieldName, Boolean.FALSE); continue; case VALUE_NUMBER_INT: ob.put(fieldName, jp.getNumberValue()); continue; case VALUE_NUMBER_FLOAT: ob.put(fieldName, jp.getNumberValue()); continue; case VALUE_EMBEDDED_OBJECT: ob.put(fieldName, jp.getEmbeddedObject()); continue; } } catch (JSONException e) { throw ctxt.mappingException("Failed to construct JSONObject: " + e.getMessage()); } throw ctxt.mappingException("Urecognized or unsupported JsonToken type: " + t); } return ob; }
@ResponseBody @ExceptionHandler(JSONException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public String error(final JSONException e) { return e.getMessage(); }