@Override public UUID replaceFacts(Facts facts) throws IOException { // TODO: This is rather odd since we json encode something that will // be json encoded again. String jsonFacts = connector.toJSON(facts); return postCommand("replace facts", 1, jsonFacts); }
/** * Executes the request and converts the result into an object of the desired <code>type</code>. * If the request results in a {@link HttpStatus#SC_NOT_FOUND}, then this method will return * <code>null</code>. * * @param uriStr The relative path to the endpoint * @param params Parameters to pass in the request * @param type The expected return type * @return The response or <code>null</code> in case no data was found * @throws IOException */ protected <V> V getSingletonResponse(String uriStr, Map<String, String> params, Type type) throws IOException { try { return connector.get(uriStr, params, type); } catch (HttpResponseException e) { if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) return null; throw e; } }
/** * Executes the request and converts the result into a map of the desired <code>type</code>. If * the request results in a {@link HttpStatus#SC_NOT_FOUND}, then this method will return an empty * map. * * @param uriStr The relative path to the endpoint * @param params Parameters to pass in the request * @param type The expected return type (must be a generic List declaration) * @return The response in list form or an empty list in case no data was found * @throws IOException */ protected <K, V> Map<K, V> getMapResponse(String uriStr, Map<String, String> params, Type type) throws IOException { try { return connector.get(uriStr, params, type); } catch (HttpResponseException e) { if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) return Collections.emptyMap(); throw e; } }
protected UUID postCommand(String command, int version, Object payload) throws IOException { CommandObject cmdObj = new CommandObject(); cmdObj.setCommand(command); cmdObj.setVersion(version); cmdObj.setPayload(payload); String json = connector.toJSON(cmdObj); Map<String, String> params = new HashMap<String, String>(); params.put("payload", json); try { MessageDigest md = MessageDigest.getInstance("SHA-1"); params.put("checksum", Hex.encodeHexString(md.digest(json.getBytes(HttpConnector.UTF_8)))); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } CommandResponse response = connector.post("/commands/", params, CommandResponse.class); return response == null ? null : UUID.fromString(response.getUuid()); }