Example usage for com.google.gson JsonObject add

List of usage examples for com.google.gson JsonObject add

Introduction

In this page you can find the example usage for com.google.gson JsonObject add.

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:arces.unibo.SEPA.commons.protocol.SPARQL11Protocol.java

License:Open Source License

public SPARQL11Protocol(SPARQL11Properties properties) throws IllegalArgumentException {

    if (properties == null) {
        logger.fatal("Properties are null");
        throw new IllegalArgumentException("Properties are null");
    }//w  ww .j  a  v  a 2 s . c om

    this.properties = properties;

    responseHandler = new ResponseHandler<String>() {
        @Override
        public String handleResponse(final HttpResponse response) {
            /*SPARQL 1.1 Protocol (https://www.w3.org/TR/sparql11-protocol/)
                    
            UPDATE
            2.2 update operation
            The response to an update request indicates success or failure of the request via HTTP response status code.
                    
            QUERY
            2.1.5 Accepted Response Formats
                    
            Protocol clients should use HTTP content negotiation [RFC2616] to request response formats that the client can consume. See below for more on potential response formats.
                    
            2.1.6 Success Responses
                    
            The SPARQL Protocol uses the response status codes defined in HTTP to indicate the success or failure of an operation. Consult the HTTP specification [RFC2616] for detailed definitions of each status code. While a protocol service should use a 2XX HTTP response code for a successful query, it may choose instead to use a 3XX response code as per HTTP.
                    
            The response body of a successful query operation with a 2XX response is either:
                    
            a SPARQL Results Document in XML, JSON, or CSV/TSV format (for SPARQL Query forms SELECT and ASK); or,
            an RDF graph [RDF-CONCEPTS] serialized, for example, in the RDF/XML syntax [RDF-XML], or an equivalent RDF graph serialization, for SPARQL Query forms DESCRIBE and CONSTRUCT).
            The content type of the response to a successful query operation must be the media type defined for the format of the response body.
                    
            2.1.7 Failure Responses
                    
            The HTTP response codes applicable to an unsuccessful query operation include:
                    
            400 if the SPARQL query supplied in the request is not a legal sequence of characters in the language defined by the SPARQL grammar; or,
            500 if the service fails to execute the query. SPARQL Protocol services may also return a 500 response code if they refuse to execute a query. This response does not indicate whether the server may or may not process a subsequent, identical request or requests.
            The response body of a failed query request is implementation defined. Implementations may use HTTP content negotiation to provide human-readable or machine-processable (or both) information about the failed query request.
                    
            A protocol service may use other 4XX or 5XX HTTP response codes for other failure conditions, as per HTTP.
            */
            JsonObject json = new JsonObject();

            // Status code
            int code = response.getStatusLine().getStatusCode();

            //Body
            String body = null;
            HttpEntity entity = response.getEntity();
            try {
                body = EntityUtils.toString(entity, Charset.forName("UTF-8"));
            } catch (ParseException e) {
                code = 500;
                body = e.getMessage();
            } catch (IOException e) {
                code = 500;
                body = e.getMessage();
            }

            JsonObject jsonBody = null;
            try {
                jsonBody = new JsonParser().parse(body).getAsJsonObject();
            } catch (JsonParseException | IllegalStateException e) {
                json.add("body", new JsonPrimitive(body));
            }

            if (jsonBody != null)
                json.add("body", jsonBody);

            json.add("code", new JsonPrimitive(code));
            return json.toString();
        }
    };
}

From source file:arces.unibo.SEPA.commons.SPARQL.ARBindingsResults.java

License:Open Source License

public ARBindingsResults(BindingsResults added, BindingsResults removed) {
    JsonObject nullResults = new JsonObject();
    JsonArray arr = new JsonArray();
    nullResults.add("bindings", arr);

    JsonObject nullHead = new JsonObject();
    nullHead.add("vars", new JsonArray());

    JsonObject addedResults = nullResults;
    JsonObject removedResults = nullResults;
    JsonObject head = nullHead;//from  ww w.  j  av  a  2 s.c o m

    if (added != null) {
        head = added.toJson().get("head").getAsJsonObject();
        addedResults = added.toJson().get("results").getAsJsonObject();
    }

    if (removed != null) {
        head = removed.toJson().get("head").getAsJsonObject();
        removedResults = removed.toJson().get("results").getAsJsonObject();
    }

    results.add("addedresults", addedResults);
    results.add("removedresults", removedResults);
    results.add("head", head);
}

From source file:arces.unibo.SEPA.commons.SPARQL.ARBindingsResults.java

License:Open Source License

public BindingsResults getAddedBindings() {
    JsonObject ret = new JsonObject();
    ret.add("results", results.get("addedresults"));
    ret.add("head", results.get("head"));
    return new BindingsResults(ret);
}

From source file:arces.unibo.SEPA.commons.SPARQL.ARBindingsResults.java

License:Open Source License

public BindingsResults getRemovedBindings() {
    JsonObject ret = new JsonObject();
    ret.add("results", results.get("removedresults"));
    ret.add("head", results.get("head"));
    return new BindingsResults(ret);
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public BindingsResults(Set<String> varSet, List<Bindings> solutions) {
    results = new JsonObject();

    JsonObject vars = new JsonObject();
    JsonArray varArray = new JsonArray();
    if (varSet != null)
        for (String var : varSet) {
            varArray.add(var);
        }/*from   w  w  w. j a va2s.  c  o  m*/
    vars.add("vars", varArray);
    results.add("head", vars);

    JsonArray bindingsArray = new JsonArray();
    if (solutions != null)
        for (Bindings solution : solutions)
            bindingsArray.add(solution.toJson());
    JsonObject bindings = new JsonObject();
    bindings.add("bindings", bindingsArray);
    results.add("results", bindings);
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

public BindingsResults(BindingsResults newBindings) {
    results = new JsonObject();

    JsonObject vars = new JsonObject();
    JsonArray varArray = new JsonArray();
    if (newBindings != null)
        for (String var : newBindings.getVariables()) {
            varArray.add(var);
        }//from  w w  w.j  av  a 2 s .com
    vars.add("vars", varArray);
    results.add("head", vars);

    JsonArray bindingsArray = new JsonArray();
    if (newBindings != null)
        for (Bindings solution : newBindings.getBindings())
            bindingsArray.add(solution.toJson());
    JsonObject bindings = new JsonObject();
    bindings.add("bindings", bindingsArray);
    results.add("results", bindings);
}

From source file:arces.unibo.SEPA.server.core.EngineProperties.java

License:Open Source License

protected void defaults() {
    JsonObject properties = new JsonObject();

    // Engine timeouts
    JsonObject timeouts = new JsonObject();
    timeouts.add("scheduling", new JsonPrimitive(0));
    timeouts.add("queuesize", new JsonPrimitive(1000));
    timeouts.add("keepalive", new JsonPrimitive(5000));
    timeouts.add("http", new JsonPrimitive(5000));
    properties.add("timeouts", timeouts);

    // Default path, scheme and port
    properties.add("path", new JsonPrimitive("/sparql"));
    properties.add("scheme", new JsonPrimitive("http"));
    properties.add("port", new JsonPrimitive(8000));

    // Secure query
    JsonObject secureQuery = new JsonObject();
    secureQuery.add("port", new JsonPrimitive(8443));
    secureQuery.add("scheme", new JsonPrimitive("https"));
    properties.add("securequery", secureQuery);

    // Secure update
    JsonObject secureUpdate = new JsonObject();
    secureUpdate.add("port", new JsonPrimitive(8443));
    secureUpdate.add("scheme", new JsonPrimitive("https"));
    properties.add("secureupdate", secureUpdate);

    // Subscribe/*from www  . j  ava  2 s . co  m*/
    JsonObject subscribe = new JsonObject();
    subscribe.add("scheme", new JsonPrimitive("ws"));
    subscribe.add("port", new JsonPrimitive(9000));
    properties.add("subscribe", subscribe);

    // Secure subscribe
    JsonObject secureSubscribe = new JsonObject();
    secureSubscribe.add("scheme", new JsonPrimitive("wss"));
    secureSubscribe.add("port", new JsonPrimitive(9443));
    secureSubscribe.add("path", new JsonPrimitive("/secure/sparql"));
    properties.add("securesubscribe", secureSubscribe);

    // Authorization server
    JsonObject authServer = new JsonObject();
    authServer.add("port", new JsonPrimitive(8443));
    authServer.add("scheme", new JsonPrimitive("https"));
    authServer.add("register", new JsonPrimitive("/oauth/register"));
    authServer.add("tokenrequest", new JsonPrimitive("/oauth/token"));
    properties.add("authorizationserver", authServer);

    parameters.add("parameters", properties);
}

From source file:arces.unibo.SEPA.server.protocol.HTTPGate.java

License:Open Source License

/**
 * Builds the echo response./* w w  w.  ja  v a 2  s  . co m*/
 *
 * @param exchange
 *            the exchange
 * @return the json object
 */
private JsonObject buildEchoResponse(HttpExchange exchange) {
    JsonObject json = new JsonObject();

    json.add("method", new JsonPrimitive(exchange.getRequestMethod().toUpperCase()));
    json.add("protocol", new JsonPrimitive(exchange.getProtocol()));

    JsonObject headers = new JsonObject();
    for (String header : exchange.getRequestHeaders().keySet()) {
        headers.add(header, new JsonPrimitive(exchange.getRequestHeaders().get(header).toString()));
    }
    json.add("headers", headers);

    String body = "";
    try {
        body = IOUtils.toString(exchange.getRequestBody(), "UTF-8");
    } catch (IOException e) {
        logger.error(e.getMessage());
        body = e.getMessage();
    }
    json.add("body", new JsonPrimitive(body));

    json.add("contextPath", new JsonPrimitive(exchange.getHttpContext().getPath()));
    if (exchange.getRequestURI().getQuery() != null)
        json.add("query", new JsonPrimitive(exchange.getRequestURI().getQuery()));

    return json;
}

From source file:arces.unibo.SEPA.server.protocol.HTTPGate.java

License:Open Source License

/**
 * Failure response./*from   ww w .j a v  a 2s .  c o m*/
 *
 * @param exchange
 *            the exchange
 * @param httpResponseCode
 *            the http response code
 * @param responseBody
 *            the response body
 */
protected void failureResponse(HttpExchange exchange, int httpResponseCode, String responseBody) {
    JsonObject json = buildEchoResponse(exchange);

    json.add("body", new JsonPrimitive(responseBody));
    json.add("code", new JsonPrimitive(httpResponseCode));

    sendResponse(exchange, httpResponseCode, json.toString());
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonMapTypeAdapter.java

License:Apache License

@Override
public JsonElement serialize(Map<?, ?> object, TypeMetadata<JsonElement> typeMetadata) {
    JsonObject jsonObject = new JsonObject();
    TypeAdapter valueTypeAdapter = typeMetadata.getTypeParameterTypeAdapters().get(1);
    for (Object key : object.keySet()) {
        JsonElement valueElement = (JsonElement) valueTypeAdapter.serialize(object.get(key), typeMetadata);
        jsonObject.add(String.valueOf(key), valueElement);
    }//w  w  w .  ja  va2 s  .  c o m
    return jsonObject;
}