Search in sources :

Example 21 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project Xponents by OpenSextant.

the class XlayerClient method process.

/**
     * 
     * @param text
     * @return
     * @throws IOException
     * @throws JSONException
     */
public List<TextMatch> process(String docid, String text) throws IOException, JSONException {
    ClientResource client = new ClientResource(serviceContext, serviceURI);
    org.json.JSONObject content = new JSONObject();
    content.put("text", text);
    content.put("docid", docid);
    content.put("features", "places,coordinates,countries,persons,orgs,reverse-geocode");
    /* Coordinates mainly are XY locations; Reverse Geocode them to find what country the location resides */
    StringWriter data = new StringWriter();
    try {
        Representation repr = new JsonRepresentation(content.toString());
        repr.setCharacterSet(CharacterSet.UTF_8);
        //log.debug("CLIENT {} {}", serviceAddress, client);
        // Process and read response fully.
        Representation response = client.post(repr, MediaType.APPLICATION_JSON);
        response.write(data);
        response.exhaust();
        response.release();
        JSONObject json = new JSONObject(data.toString());
        log.debug("POST: response  {}", json.toString(2));
        JSONObject meta = json.getJSONObject("response");
        JSONArray annots = json.getJSONArray("annotations");
        List<TextMatch> matches = new ArrayList<TextMatch>();
        for (int x = 0; x < annots.length(); ++x) {
            Object m = annots.get(x);
            matches.add(Transforms.parseAnnotation(m));
        }
        return matches;
    } catch (ResourceException restErr) {
        if (restErr.getCause() instanceof IOException) {
            throw (IOException) restErr.getCause();
        } else {
            throw restErr;
        }
    } catch (java.net.SocketException err) {
        // This never happens. Restlet wraps everything.
        throw err;
    } finally {
        client.release();
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) TextMatch(org.opensextant.extraction.TextMatch) IOException(java.io.IOException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) ClientResource(org.restlet.resource.ClientResource) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Example 22 with JsonRepresentation

use of org.restlet.ext.json.JsonRepresentation in project Xponents by OpenSextant.

the class TaggerResource method status.

/**
	 * Status.
	 *
	 * @param status
	 *            the status
	 * @param error
	 *            the error
	 * @return the json representation
	 */
protected JsonRepresentation status(String status, String error) {
    JSONObject s = new JSONObject();
    try {
        if (error != null) {
            s.put("status", status);
            s.put("error", error);
        } else {
            s.put("status", status);
        }
    } catch (JSONException jsonErr) {
        error("Trivial JSON issue!!!!", jsonErr);
    }
    return new JsonRepresentation(s.toString());
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

JsonRepresentation (org.restlet.ext.json.JsonRepresentation)22 JSONObject (org.json.JSONObject)16 Test (org.testng.annotations.Test)13 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)7 Representation (org.restlet.representation.Representation)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Map (java.util.Map)5 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)4 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 Date (java.util.Date)2 AuditEvent (org.forgerock.audit.events.AuditEvent)2 JsonValue (org.forgerock.json.JsonValue)2 OAuth2Exception (org.forgerock.oauth2.core.exceptions.OAuth2Exception)2 OAuth2RestletException (org.forgerock.oauth2.restlet.OAuth2RestletException)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 TextMatch (org.opensextant.extraction.TextMatch)2