List of usage examples for com.fasterxml.jackson.databind JsonNode get
public JsonNode get(String paramString)
From source file:com.cyphermessenger.client.SyncRequest.java
public static CypherSession userLogin(String username, byte[] serverPassword, byte[] localPassword) throws IOException, APIErrorException { String passwordHashEncoded = Utils.BASE64_URL.encode(serverPassword); HttpURLConnection conn = doRequest("login", null, new String[] { "username", "password" }, new String[] { username, passwordHashEncoded }); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); }//from w w w .ja v a2s . c o m InputStream in = conn.getInputStream(); JsonNode node = MAPPER.readTree(in); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode == StatusCode.OK) { long userID = node.get("userID").asLong(); ECKey key; try { key = Utils.decodeKey(node.get("publicKey").asText(), node.get("privateKey").asText(), localPassword); } catch (InvalidCipherTextException ex) { throw new RuntimeException(ex); } long keyTimestamp = node.get("keyTimestamp").asLong(); key.setTime(keyTimestamp); CypherUser newUser = new CypherUser(username, localPassword, serverPassword, userID, key, keyTimestamp); String sessionID = node.get("sessionID").asText(); return new CypherSession(newUser, sessionID); } else { throw new APIErrorException(statusCode); } }
From source file:com.cyphermessenger.client.SyncRequest.java
public static void sendMessage(CypherSession session, CypherUser contactUser, CypherMessage message) throws IOException, APIErrorException { CypherUser user = session.getUser(); byte[] timestampBytes = Utils.longToBytes(message.getTimestamp()); int messageIDLong = message.getMessageID(); Encrypt encryptionCtx = new Encrypt(user.getKey().getSharedSecret(contactUser.getKey())); encryptionCtx.updateAuthenticatedData(Utils.longToBytes(messageIDLong)); encryptionCtx.updateAuthenticatedData(timestampBytes); byte[] payload; try {/*w w w. j av a 2 s . c o m*/ payload = encryptionCtx.process(message.getText().getBytes()); } catch (InvalidCipherTextException e) { throw new RuntimeException(e); } HashMap<String, String> pairs = new HashMap<>(6); pairs.put("payload", Utils.BASE64_URL.encode(payload)); pairs.put("contactID", contactUser.getUserID() + ""); pairs.put("messageID", messageIDLong + ""); pairs.put("messageTimestamp", message.getTimestamp() + ""); pairs.put("userKeyTimestamp", session.getUser().getKeyTime() + ""); pairs.put("contactKeyTimestamp", contactUser.getKeyTime() + ""); HttpURLConnection conn = doRequest("message", session, pairs); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); } InputStream in = conn.getInputStream(); JsonNode node = MAPPER.readTree(in); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode != StatusCode.OK) { throw new APIErrorException(statusCode); } }
From source file:com.cyphermessenger.client.SyncRequest.java
private static ArrayList<CypherContact> handleContactNode(JsonNode node) { ArrayList<CypherContact> array = new ArrayList<>(); JsonNode arrayNode = node.get("contacts"); if (arrayNode.isArray()) { for (JsonNode selectedNode : arrayNode) { long receivedContactID = selectedNode.get("contactID").asLong(); long timestamp = selectedNode.get("contactTimestamp").asLong(); long keyTime = selectedNode.get("keyTimestamp").asLong(); String username = selectedNode.get("username").asText(); String contactStatus = selectedNode.get("contactStatus").asText(); ECKey publicKey = Utils.decodeKey(selectedNode.get("publicKey").asText()); boolean isFirst = selectedNode.get("isFirst").asBoolean(); publicKey.setTime(keyTime);//w w w . ja va 2 s . co m CypherContact newContact = new CypherContact(username, receivedContactID, publicKey, keyTime, contactStatus, timestamp, isFirst); array.add(newContact); } } return array; }
From source file:com.cyphermessenger.client.SyncRequest.java
public static List<String> findUser(CypherSession session, String username, int limit) throws IOException, APIErrorException { String[] keys = new String[] { "username", "limit" }; String[] vals = new String[] { username, limit + "" }; HttpURLConnection conn = doRequest("find", session, keys, vals); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); }/*from w ww.ja v a 2 s. c om*/ JsonNode node = MAPPER.readTree(conn.getInputStream()); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode != StatusCode.OK) { throw new APIErrorException(statusCode); } else { return Utils.MAPPER.treeToValue(node.get("users"), ArrayList.class); } }
From source file:com.tda.sitefilm.allocine.JSONAllocineAPIHelper.java
private static Release parseRelease(JsonNode rootNode) { // parse release JsonNode node = rootNode.get("release"); if (node != null && (node.size() > 0)) { Release release = new Release(); release.setReleaseDate(getValueAsString(node.get("releaseDate"))); node = node.get("distributor"); if (node != null && (node.size() > 0)) { Distributor distributor = new Distributor(); distributor.setName(getValueAsString(node.get("name"))); release.setDistributor(distributor); }//from w w w . j a v a 2 s .com return release; } return null; }
From source file:com.cyphermessenger.client.SyncRequest.java
public static CypherUser registerUser(String username, String password, String captchaValue, Captcha captcha) throws IOException, APIErrorException { if (!captcha.verify(captchaValue)) { throw new APIErrorException(StatusCode.CAPTCHA_INVALID); }/*from w w w .jav a 2s . c o m*/ username = username.toLowerCase(); captchaValue = captchaValue.toLowerCase(); byte[] serverPassword = Utils.cryptPassword(password.getBytes(), username); byte[] localPassword = Utils.sha256(password); String serverPasswordEncoded = Utils.BASE64_URL.encode(serverPassword); ECKey key = new ECKey(); byte[] publicKey = key.getPublicKey(); byte[] privateKey = key.getPrivateKey(); try { privateKey = Encrypt.process(localPassword, privateKey); } catch (InvalidCipherTextException ex) { throw new RuntimeException(ex); } String[] keys = new String[] { "captchaToken", "captchaValue", "username", "password", "publicKey", "privateKey" }; String[] vals = new String[] { captcha.captchaToken, captchaValue, username, serverPasswordEncoded, Utils.BASE64_URL.encode(publicKey), Utils.BASE64_URL.encode(privateKey) }; HttpURLConnection conn = doRequest("register", null, keys, vals); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); } InputStream in = conn.getInputStream(); JsonNode node = MAPPER.readTree(in); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode == StatusCode.OK) { long userID = node.get("userID").asLong(); long keyTime = node.get("timestamp").asLong(); key.setTime(keyTime); return new CypherUser(username, localPassword, serverPassword, userID, key, keyTime); } else { throw new APIErrorException(statusCode); } }
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static JsonNode getNormalPriceMoneyNode(JsonNode item) { ProductType type = getProductType(item); JsonNode structuredContent = item.path("structuredContent"); switch (type) { case PHYSICAL: case SERVICE: case GIFT_CARD: JsonNode variants = structuredContent.path("variants"); if (variants.size() == 0) { return DEFAULT_MONEY_NODE; }/*from w w w . j a v a 2s . c o m*/ JsonNode moneyNode = variants.get(0).path("priceMoney"); BigDecimal price = getAmountFromMoneyNode(moneyNode); for (int i = 1; i < variants.size(); i++) { JsonNode currMoneyNode = variants.get(i).path("priceMoney"); BigDecimal curr = getAmountFromMoneyNode(currMoneyNode); if (curr.compareTo(price) > 0) { price = curr; moneyNode = currMoneyNode; } } return moneyNode; case DIGITAL: JsonNode digitalMoneyNode = structuredContent.path("priceMoney"); return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode; default: return DEFAULT_MONEY_NODE; } }
From source file:com.cyphermessenger.client.SyncRequest.java
private static CypherContact manageContact(CypherSession session, String contactName, boolean add) throws IOException, APIErrorException { String action = "block"; if (add) {/* www . jav a 2 s.c o m*/ action = "add"; } String[] keys = new String[] { "action", "contactName" }; String[] vals = new String[] { action, contactName }; HttpURLConnection conn = doRequest("contact", session, keys, vals); if (conn.getResponseCode() != 200) { throw new IOException("Server error"); } InputStream in = conn.getInputStream(); JsonNode node = MAPPER.readTree(in); conn.disconnect(); int statusCode = node.get("status").asInt(); if (statusCode == StatusCode.OK && add) { long userID = node.get("contactID").asLong(); long keyTimestamp = node.get("keyTimestamp").asLong(); long contactTimestamp = node.get("contactTimestamp").asLong(); ECKey key = Utils.decodeKey(node.get("publicKey").asText()); key.setTime(keyTimestamp); boolean isFirst = node.get("isFirst").asBoolean(); return new CypherContact(contactName, userID, key, keyTimestamp, CypherContact.ACCEPTED, contactTimestamp, isFirst); } else { boolean isFirst = node.get("isFirst").asBoolean(); String status; switch (statusCode) { case StatusCode.CONTACT_WAITING: status = CypherContact.WAITING; break; case StatusCode.OK: case StatusCode.CONTACT_BLOCKED: status = CypherContact.BLOCKED; break; case StatusCode.CONTACT_DENIED: status = CypherContact.DENIED; break; default: throw new APIErrorException(statusCode); } return new CypherContact(contactName, null, null, null, status, null, isFirst); } }
From source file:com.bbva.kltt.apirest.core.util.parser.ParserUtil.java
/** * @param nodeName with the node name//from www . j a v a 2s .c om * @param node with the node to search the field * @param fieldName with the field name to be searched * @param isRequired true if the field is required * @return the field value as set * @throws APIRestGeneratorException with an occurred exception */ public static Set<String> getNodeValueSetField(final String nodeName, final JsonNode node, final String fieldName, final boolean isRequired) throws APIRestGeneratorException { Set<String> outcome = null; final boolean hasField = node.has(fieldName); if (hasField) { JsonNode jsonNode = node.get(fieldName); if (jsonNode instanceof ArrayNode) { outcome = ParserUtil.generateSetStringFromString((ArrayNode) jsonNode); } else { outcome = new HashSet<String>(); if (!jsonNode.isNull()) { outcome.add(jsonNode.asText()); } } } else if (!hasField && isRequired) { ParserUtil.generateExceptionRequiredField(nodeName, fieldName); } return outcome; }
From source file:com.bbva.kltt.apirest.core.util.parser.ParserUtil.java
/** * @param nodeName with the node name/*w w w .ja va 2s . c o m*/ * @param node with the node to search the field * @param fieldName with the field name to be searched * @param isRequired true if the field is required * @return the field value * @throws APIRestGeneratorException with an occurred exception */ public static String getNodeValueField(final String nodeName, final JsonNode node, final String fieldName, final boolean isRequired) throws APIRestGeneratorException { String outcome = null; final boolean hasField = node.has(fieldName); if (hasField) { final JsonNode attributeNode = node.get(fieldName); if (attributeNode.isNull() && isRequired) { ParserUtil.generateExceptionRequiredNodeContent(nodeName, fieldName); } outcome = attributeNode.asText(); } else if (!hasField && isRequired) { ParserUtil.generateExceptionRequiredField(nodeName, fieldName); } return outcome; }