Example usage for com.fasterxml.jackson.databind JsonNode asText

List of usage examples for com.fasterxml.jackson.databind JsonNode asText

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode asText.

Prototype

public abstract String asText();

Source Link

Usage

From source file:snow.http.auth.Facebook.java

private Term fetchProfile(String accessToken) {
    try {//from  w w w  .j  av  a2 s .c o  m

        URL url = new URL(PROFILE + accessToken);// +"&appsecret_proof="+MDigest.HmacSHA256(AppSecret,
                                                 // accessToken));

        // System.out.println(url);

        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

        try (InputStream stream = con.getInputStream()) {

            if (con.getResponseCode() == 200) {

                ObjectMapper mapper = new ObjectMapper();

                JsonNode root = mapper.readTree(url.openStream());

                JsonNode node = root.get("id");
                if (node != null) {
                    String fbID = node.asText();

                    //                        try (Transaction tx = Core.DB.beginTx()) {
                    //                            TermMutable term;
                    //                            Node termNode = Core.search(FACEBOOK_ID, fbID);
                    //                            if (termNode == null) {
                    //                                term = Unstable.term();
                    //
                    //                                term.property(FACEBOOK_ID, fbID);
                    //
                    //                                term.type(Primitives.PERSON);
                    //                            } else {
                    //                                term = TermInDB.term(termNode);
                    //                            }
                    //
                    //                            term.link(new URL("https://www.facebook.com/profile.php?id=" + fbID));
                    //
                    //                            for (String field : FIELDS) {
                    //                                node = root.get(field);
                    //
                    //                                if (node != null) {
                    //                                    switch (field) {
                    //                                    case "languages":
                    //                                        // XXX: code
                    //                                        break;
                    //
                    //                                    case "picture":
                    //                                        JsonNode data = node.get("data");
                    //                                        if (data != null) {
                    //                                            JsonNode picURL = data.get("url");
                    //                                            if (picURL != null) {
                    //                                                try {
                    //                                                    URL imgURL = new URL(picURL.asText());
                    //
                    //                                                    term.property("facebook:" + field, imgURL.toString());
                    //
                    //                                                    term.visual(imgURL);
                    //                                                } catch (MalformedURLException e) {
                    //                                                }
                    //                                            }
                    //                                        }
                    //
                    //                                        break;
                    //
                    //                                    case "name":
                    //                                        term.label(ORIGINAL_LANGUAGE, node.asText());
                    //
                    //                                    default:
                    //                                        term.property("facebook:" + field, node.asText());
                    //
                    //                                        break;
                    //                                    }
                    //                                }
                    //                            }
                    //
                    //                            // System.out.println("term = "+term.debug(0));
                    //
                    //                            TermInDB person = TermInDB.term(term);
                    //
                    //                            tx.success();
                    //
                    //                            return person;
                    //                        }
                }
            }
        } catch (IOException e) {
            // e.printStackTrace();
            //
            // System.out.println("response code = "+con.getResponseCode());
            //
            // try (InputStream stream = con.getErrorStream()) {
            // java.util.Scanner s = new
            // java.util.Scanner(stream).useDelimiter("\\A");
            // System.out.println(s.hasNext() ? s.next() : "");
            // }
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.baasbox.service.push.providers.GCMServer.java

public boolean send(String message, List<String> deviceid, JsonNode bodyJson)
        throws PushNotInitializedException, InvalidRequestException, UnknownHostException, IOException,
        PushTimeToLiveFormatException, PushCollapseKeyFormatException {
    PushLogger pushLogger = PushLogger.getInstance();
    pushLogger.addMessage("............ GCM Push Message: -%s- to the device(s) %s", message, deviceid);

    try {/*from   w ww  .j  av a 2 s .  c om*/
        if (BaasBoxLogger.isDebugEnabled())
            BaasBoxLogger.debug("GCM Push message: " + message + " to the device " + deviceid);
        if (!isInit) {
            pushLogger.addMessage("............ GCMS is not initialized!");
            return true;
        }
        JsonNode customDataNodes = bodyJson.get("custom");

        Map<String, JsonNode> customData = new HashMap<String, JsonNode>();

        if (!(customDataNodes == null)) {
            customData.put("custom", customDataNodes);
        }

        JsonNode collapse_KeyNode = bodyJson.findValue("collapse_key");
        String collapse_key = null;

        if (!(collapse_KeyNode == null)) {
            if (!(collapse_KeyNode.isTextual()))
                throw new PushCollapseKeyFormatException("Collapse_key MUST be a String");
            collapse_key = collapse_KeyNode.asText();
        } else
            collapse_key = "";

        JsonNode timeToLiveNode = bodyJson.findValue("time_to_live");
        int time_to_live = 0;

        if (!(timeToLiveNode == null)) {
            if (!(timeToLiveNode.isNumber()))
                throw new PushTimeToLiveFormatException("Time_to_live MUST be a positive number or equal zero");
            else if (timeToLiveNode.asInt() < 0)
                throw new PushTimeToLiveFormatException("Time_to_live MUST be a positive number or equal zero");
            else if (timeToLiveNode.asInt() > MAX_TIME_TO_LIVE) {
                time_to_live = MAX_TIME_TO_LIVE;
            } else
                time_to_live = timeToLiveNode.asInt();

        } else
            time_to_live = MAX_TIME_TO_LIVE; //IF NULL WE SET DEFAULT VALUE (4 WEEKS)

        if (BaasBoxLogger.isDebugEnabled())
            BaasBoxLogger.debug("collapse_key: " + collapse_key.toString());

        if (BaasBoxLogger.isDebugEnabled())
            BaasBoxLogger.debug("time_to_live: " + time_to_live);

        if (BaasBoxLogger.isDebugEnabled())
            BaasBoxLogger.debug("Custom Data: " + customData.toString());

        pushLogger.addMessage("............ messgae: %s", message);
        pushLogger.addMessage("............ collapse_key: %s", collapse_key);
        pushLogger.addMessage("............ time_to_live: %s", time_to_live);
        pushLogger.addMessage("............ custom: %s", customData);
        pushLogger.addMessage("............ device(s): %s", deviceid);

        Sender sender = new Sender(apikey);
        Message msg = new Message.Builder().addData("message", message).addData("custom", customData.toString())
                .collapseKey(collapse_key.toString()).timeToLive(time_to_live).build();

        MulticastResult result = sender.send(msg, deviceid, 1);

        pushLogger.addMessage("............ %d message(s) sent", result.getTotal());
        pushLogger.addMessage("................. success: %s", result.getSuccess());
        pushLogger.addMessage("................. failure: %s", result.getFailure());

        for (Result r : result.getResults()) {
            pushLogger.addMessage("............ MessageId (null == error): %s", r.getMessageId());
            pushLogger.addMessage("............... Error Code Name: %s", r.getErrorCodeName());
            pushLogger.addMessage("............... Canonincal Registration Id: %s",
                    r.getCanonicalRegistrationId());
        }
        // icallbackPush.onError(ExceptionUtils.getMessage(e));

        // icallbackPush.onSuccess();
        return false;
    } catch (Exception e) {
        pushLogger.addMessage("Error sending push notification (GCM)...");
        pushLogger.addMessage(ExceptionUtils.getMessage(e));
        throw e;
    }

}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * Annotates a single document and returns an {@link InputStream} from
 * which the contents of the serialized annotated document can be read
 * /*from  w  w  w . java 2 s  . c  om*/
 * @param documentText the contents of the document which will be annotated
 * @param documentMimeType the MIME type of the file which will be annotated
 * @param serializationFormat the format which will be used for serialization of the annotated document
 * @return an {@link InputStream} from which the serialization of the annotated document can be read
 * @throws S4ServiceClientException
 */
public InputStream annotateDocumentAsStream(String documentText, SupportedMimeType documentMimeType,
        ResponseFormat serializationFormat) throws S4ServiceClientException {

    ServiceRequest rq = new ServiceRequest(documentText, documentMimeType);
    try {
        return client.requestForStream("", "POST", rq, "Accept", serializationFormat.acceptHeader);
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
  * Classifies a single document and returns an {@link InputStream} from
  * which the contents of the serialized annotated document can be read
  *// w  ww.j  a  va2  s  .  c  om
  * @param documentText the contents of the document which will be classified
  * @param documentMimeType the MIME type of the file which will be classified
 * @return an {@link InputStream} from which the serialization of the classified document can be read
 * @throws S4ServiceClientException
  */
public InputStream classifyDocumentAsStream(String documentText, SupportedMimeType documentMimeType)
        throws S4ServiceClientException {

    ServiceRequest rq = new ServiceRequest(documentText, documentMimeType);
    try {
        return client.requestForStream("", "POST", rq, "Accept", ResponseFormat.JSON.acceptHeader);
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * Annotates a single document publicly available under a given URL.
 * Returns the annotated document serialized into the specified format
 * //from w  w w .j  a v a 2s.  c  om
 * @param documentUrl the publicly accessible URL from where the document will be downloaded
 * @param documentMimeType the MIME type of the document which will be annotated
 * @param serializationFormat the serialization format of the output
 * @return an {@link InputStream} from where the serialized output can be read
 * @throws S4ServiceClientException
 */
public InputStream annotateDocumentFromUrlAsStream(URL documentUrl, SupportedMimeType documentMimeType,
        ResponseFormat serializationFormat) throws S4ServiceClientException {

    ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);
    try {
        return client.requestForStream("", "POST", rq, "Accept", serializationFormat.acceptHeader);
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * Classifies a single document publicly available under a given URL.
 * Returns the classified document serialized into the specified format
 *
 * @param documentUrl the publicly accessible URL from where the document will be downloaded
 * @param documentMimeType the MIME type of the document which will be classified
 * @return an {@link InputStream} from where the serialized output can be read
 * @throws S4ServiceClientException//from w  w w .  j a va  2 s.co  m
 */
public InputStream classifyDocumentFromUrlAsStream(URL documentUrl, SupportedMimeType documentMimeType)
        throws S4ServiceClientException {

    ServiceRequest rq = new ServiceRequest(documentUrl, documentMimeType);
    try {
        return client.requestForStream("", "POST", rq, "Accept", ResponseFormat.JSON.acceptHeader);
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * This low level method allows the user to explicitly specify all the parameters sent to the service.
  * This is done by constructing the appropriate ServiceRequest object.
  * Returns the contents of the annotated document
 * //from  w ww  .  j  ava 2 s  .  c om
 * @param rq the request which will be sent
 * @param serializationFormat the format in which to output the annotated document
 * @param requestCompression whether to allow GZIP compression for large documents
 * @return an{@link InputStream} for the serialization of the annotated document in the specified format
 * @throws S4ServiceClientException
 */
public InputStream processRequestForStream(ServiceRequest rq, ResponseFormat serializationFormat,
        boolean requestCompression) throws S4ServiceClientException {

    try {
        if (requestCompression) {
            return client.requestForStream("", "POST", rq, "Accept", serializationFormat.acceptHeader,
                    "Accept-Encoding", "gzip");
        } else {
            return client.requestForStream("", "POST", rq, "Accept", serializationFormat.acceptHeader);
        }
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:org.waarp.common.json.AdaptativeJsonHandler.java

/**
 * //from www . ja v  a 2s  . co  m
 * @param node
 * @param field
 * @param defValue
 * @return the String if the field exists, else defValue
 */
public final String getValue(ObjectNode node, String field, String defValue) {
    JsonNode elt = node.get(field);
    if (elt != null) {
        String val = elt.asText();
        if (val.equals("null")) {
            return defValue;
        }
        return val;
    }
    return defValue;
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * This low level method allows the user to specify every parameter explicitly by setting the properties
  * of the OnlineService request object. Returns an object which wraps the annotated document.
 * /*from www  . j a v a 2s.co  m*/
 * @param rq the request which will be sent to the service
 * @param requestCompression whether to allow GZIP compression for large documents
 * @return an {@link AnnotatedDocument} containing the original content as well as the annotations produced
 * @throws S4ServiceClientException
 */
public AnnotatedDocument processRequest(ServiceRequest rq, boolean requestCompression)
        throws S4ServiceClientException {
    try {

        if (requestCompression) {
            return client.request("", "POST", new TypeReference<AnnotatedDocument>() {
            }, rq, "Accept", ResponseFormat.GATE_JSON.acceptHeader, "Accept-Encoding", "gzip");
        } else {
            return client.request("", "POST", new TypeReference<AnnotatedDocument>() {
            }, rq, "Accept", ResponseFormat.GATE_JSON.acceptHeader);
        }
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}

From source file:com.ontotext.s4.service.S4ServiceClient.java

/**
 * This low level method allows the user to specify every parameter explicitly by setting the properties
 * of the OnlineService request object. Returns an object which wraps the classified document.
 *
 * @param rq the request which will be sent
 * @param requestCompression whether to allow GZIP compression for large documents
 * @return a {@link ClassifiedDocument} containing the original content as well as the annotations produced
 * @throws S4ServiceClientException//from w  ww.  ja v  a  2 s.  c o  m
 */
public ClassifiedDocument classifyRequest(ServiceRequest rq, boolean requestCompression)
        throws S4ServiceClientException {

    try {
        if (requestCompression) {
            return client.request("", "POST", new TypeReference<ClassifiedDocument>() {
            }, rq, "Accept", ResponseFormat.JSON.acceptHeader, "Accept-Encoding", "gzip");
        } else {
            return client.request("", "POST", new TypeReference<ClassifiedDocument>() {
            }, rq, "Accept", ResponseFormat.JSON.acceptHeader);
        }
    } catch (HttpClientException e) {
        JsonNode response = e.getResponse();
        if (response == null) {
            throw new S4ServiceClientException(e.getMessage(), e);
        }
        JsonNode msg = response.get("message");
        throw new S4ServiceClientException(msg == null ? e.getMessage() : msg.asText(), e);
    }
}