/** * Decode a given JSON string to a POJO of the given type. * @param str the JSON string. * @param type the type to map to. * @param <T> the generic type. * @return an instance of T * @throws DecodeException when there is a parsing or invalid mapping. */ public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException { try { return mapper.readValue(str, type); } catch (Exception e) { throw new DecodeException("Failed to decode: " + e.getMessage(), e); } }
protected JsonObject getJsonFromFileOrString(String jsonFileOrString, String argName) { JsonObject conf; if (jsonFileOrString != null) { try (Scanner scanner = new Scanner(new File(jsonFileOrString), "UTF-8").useDelimiter("\\A")) { String sconf = scanner.next(); try { conf = new JsonObject(sconf); } catch (DecodeException e) { log.error("Configuration file " + sconf + " does not contain a valid JSON object"); return null; } } catch (FileNotFoundException e) { try { conf = new JsonObject(jsonFileOrString); } catch (DecodeException e2) { // The configuration is not printed for security purpose, it can contain sensitive data. log.error("The -" + argName + " argument does not point to an existing file or is not a valid JSON object"); e2.printStackTrace(); return null; } } } else { conf = null; } return conf; }
private JsonObject getPayload() { String encodedJson; try { encodedJson = this.token.split("\\.")[1]; } catch (ArrayIndexOutOfBoundsException e) { throw new IllegalArgumentException(e.getMessage()); } String decodedJson = new String(Base64.getDecoder().decode(encodedJson)); JsonObject j; try { j = new JsonObject(decodedJson); } catch (DecodeException e) { throw new IllegalArgumentException(e.getMessage()); } return j; }
@Override public void resourceChanged(String resourceUri, String resource) { if (configResourceUri != null && configResourceUri.equals(resourceUri)) { log.info("Got notified about configuration resource update for " + resourceUri + " with new data: " + resource); try { JsonObject obj = new JsonObject(resource); Integer requestHopsLimitValue = obj.getInteger(REQUEST_HOPS_LIMIT_PROPERTY); if (requestHopsLimitValue != null) { log.info("Got value '" + requestHopsLimitValue + "' for property '"+ REQUEST_HOPS_LIMIT_PROPERTY +"'. Request hop validation is now activated"); requestHopsLimit = requestHopsLimitValue; } else { log.warn("No value for property '"+ REQUEST_HOPS_LIMIT_PROPERTY +"' found. Request hop validation will not be activated"); requestHopsLimit = null; } } catch (DecodeException ex) { log.warn("Unable to decode configuration resource for " + resourceUri + " with data: " + resource + " Reason: " + ex.getMessage()); requestHopsLimit = null; } } }
/** * Decode a given JSON string to a POJO of the given class type. * @param str the JSON string. * @param clazz the class to map to. * @param <T> the generic type. * @return an instance of T * @throws DecodeException when there is a parsing or invalid mapping. */ public static <T> T decodeValue(String str, Class<T> clazz) throws DecodeException { try { return mapper.readValue(str, clazz); } catch (Exception e) { throw new DecodeException("Failed to decode: " + e.getMessage()); } }
System.exit(1); } catch (DecodeException e) { System.err.println("Invalid config file: " + e.getMessage()); System.exit(1);
protected JsonObject getJsonFromFileOrString(String jsonFileOrString, String argName) { JsonObject conf; if (jsonFileOrString != null) { try (Scanner scanner = new Scanner(new File(jsonFileOrString), "UTF-8").useDelimiter("\\A")) { String sconf = scanner.next(); try { conf = new JsonObject(sconf); } catch (DecodeException e) { log.error("Configuration file " + sconf + " does not contain a valid JSON object"); return null; } } catch (FileNotFoundException e) { try { conf = new JsonObject(jsonFileOrString); } catch (DecodeException e2) { // The configuration is not printed for security purpose, it can contain sensitive data. log.error("The -" + argName + " argument does not point to an existing file or is not a valid JSON object"); e2.printStackTrace(); return null; } } } else { conf = null; } return conf; }
<T> T convert(Class<T> type) { try { return Json.mapper.readValue(buffer.asParser(), type); } catch (Exception e) { throw new DecodeException(e.getMessage()); } } }
private boolean isListenerJsonInvalid(HttpServerRequest request, Buffer hookData) { if (isHookJsonInvalid(request, hookData)) { // No further checks required. hook definitively is invalid. return true; } final JsonObject hook; try { // Badly we need to parse that JSON one more time. hook = new JsonObject(hookData); } catch (DecodeException e) { log.error("Cannot decode JSON", e); badRequest(request, "Cannot decode JSON", e.getMessage()); return true; } final JsonArray methods = hook.getJsonArray("methods"); if (methods != null) { for (Object method : methods) { if (!QueueProcessor.httpMethodIsQueueable(HttpMethod.valueOf((String) method))) { final String msg = "Listener registration request tries to hook for not allowed '" + method + "' method."; log.error(msg); badRequest(request, "Bad Request", msg + "\n"); return true; } } } return false; }
@Override public <T> T mapTo(Class<T> type) { if (buffer != null) { try { return Json.mapper.readValue(buffer.asParser(), type); } catch (Exception e) { throw new DecodeException(e.getMessage()); } } else { return Json.decodeValue(String.valueOf(value), type); } }
} catch (DecodeException e) { request.response().setStatusCode(BAD_REQUEST); request.response().end(e.getMessage()); return;
@Override public <T> T mapTo(TypeReference<T> type) { if (buffer != null) { try { return Json.mapper.readValue(buffer.asParser(), type); } catch (Exception e) { throw new DecodeException(e.getMessage()); } } else { return Json.decodeValue(String.valueOf(value), type); } }
} catch (DecodeException e) { log.error("Cannot decode JSON", e); badRequest(request, "Cannot decode JSON", e.getMessage()); return;
/** * Decode a given JSON buffer to a POJO of the given class type. * @param buf the JSON buffer. * @param clazz the class to map to. * @param <T> the generic type. * @return an instance of T * @throws DecodeException when there is a parsing or invalid mapping. */ public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException { try { return mapper.readValue((InputStream) new ByteBufInputStream(buf.getByteBuf()), clazz); } catch (Exception e) { throw new DecodeException("Failed to decode:" + e.getMessage(), e); } }
hook = new JsonObject(hookData.toString()); } catch (DecodeException e) { badRequest(request, "Cannot decode JSON", e.getMessage()); return;
/** * Decode a given JSON buffer to a POJO of the given class type. * @param buf the JSON buffer. * @param type the type to map to. * @param <T> the generic type. * @return an instance of T * @throws DecodeException when there is a parsing or invalid mapping. */ public static <T> T decodeValue(Buffer buf, TypeReference<T> type) throws DecodeException { try { return mapper.readValue(new ByteBufInputStream(buf.getByteBuf()), type); } catch (Exception e) { throw new DecodeException("Failed to decode:" + e.getMessage(), e); } }
@Override public void handle(Buffer event) { byte[] bytes = event.getBytes(); try { parser.feedInput(bytes, 0, bytes.length); } catch (IOException e) { if (exceptionHandler != null) { exceptionHandler.handle(e); return; } else { throw new DecodeException(e.getMessage()); } } checkPending(); }
exceptionHandler.handle(e); } else { throw new DecodeException(e.getMessage());
public static <T> T decodeValue(String str, Class<T> clazz) throws DecodeException { try { return mapper.readValue(str, clazz); } catch (Exception e) { throw new DecodeException("Failed to decode"); } } }
/** * Decode a given JSON string to a POJO of the given type. * @param str the JSON string. * @param type the type to map to. * @param <T> the generic type. * @return an instance of T * @throws DecodeException when there is a parsing or invalid mapping. */ public static <T> T decodeValue(String str, TypeReference<T> type) throws DecodeException { try { return mapper.readValue(str, type); } catch (Exception e) { throw new DecodeException("Failed to decode: " + e.getMessage(), e); } }