Search in sources :

Example 61 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class ResourceHandler method getResource.

/**
 * Returns a {@link JsonObject} containing the response to the request given
 * to the implementing class.
 * The response is made up of a request object and a value object.
 * In some cases the value object is removed in the case of an exception and
 * replaced with an error object.
 *
 * @return The {@link JsonObject} containing the response to the request.
 */
public JsonObject getResource() {
    JsonObject resourceResponse = Json.createObjectBuilder().build();
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    try {
        JsonObject requestObject = getRequestObject();
        objectBuilder.add(RestMonitoringAppResponseToken.getRequestKey(), requestObject);
        JsonValue valueObject = getValueObject();
        objectBuilder.add(RestMonitoringAppResponseToken.getValueKey(), valueObject);
        setStatus(Response.Status.OK);
        if (errorThrown(status)) {
            JsonObject traceObject = (JsonObject) resourceResponse.get(RestMonitoringAppResponseToken.getValueKey());
            objectBuilder.add(RestMonitoringAppResponseToken.getStacktraceKey(), traceObject.get(RestMonitoringAppResponseToken.getStacktraceKey()));
            objectBuilder.add(RestMonitoringAppResponseToken.getErrorTypeKey(), traceObject.get(RestMonitoringAppResponseToken.getErrorTypeKey()));
            objectBuilder.add(RestMonitoringAppResponseToken.getErrorKey(), traceObject.get(RestMonitoringAppResponseToken.getErrorKey()));
            resourceResponse.remove(RestMonitoringAppResponseToken.getValueKey());
        } else {
            Long millis = System.currentTimeMillis();
            objectBuilder.add(RestMonitoringAppResponseToken.getTimestampKey(), millis);
        }
        int statusCode = status.getStatusCode();
        objectBuilder.add(RestMonitoringAppResponseToken.getHttpStatusKey(), statusCode);
    } catch (JsonException ex) {
        // @TODO - FANG-6: Properly handle any JsonException caught in the ResourceHandler class.
        // Is this the best way to handle it? Return the response built so far and log the issue.
        // Don't exactly want to return a Json error in the response.
        // 
        // Options:
        // 1. Place each put in it's own try-catch block so that what can be
        // added to the object is added to the object before returning it - ie.
        // just carry on.
        // 2. Leave it as it currently is.
        // 3. Don't return the response object but instead a string denoting
        // internal error or something?
        Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return objectBuilder.build();
}
Also used : JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 62 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class CompositeTypeProcessor method processObject.

private JsonObject processObject(CompositeDataSupport compositeObject) throws JsonException {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    for (String entry : compositeObject.getCompositeType().keySet()) {
        Object entryObject = compositeObject.get(entry);
        TypeProcessor<?> processor = ProcessorFactory.getTypeProcessor(entryObject);
        objectBuilder.add(entry, processor.processObject(entryObject));
    }
    return objectBuilder.build();
}
Also used : JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 63 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class RestCollectionProvider method getContent.

/**
 * Converts a {@link RestCollection} into a Json object and then returns it as as String representation.
 * @param proxy
 * @return
 */
@Override
public String getContent(RestCollection proxy) {
    StringBuilder sb = new StringBuilder();
    final List<String> wrapObjectHeader = requestHeaders.get().getRequestHeader("X-Wrap-Object");
    final List<String> skipMetadataHeader = requestHeaders.get().getRequestHeader("X-Skip-Metadata");
    boolean wrapObject = ((wrapObjectHeader != null) && (wrapObjectHeader.size() > 0));
    boolean skipMetadata = ((skipMetadataHeader != null) && (skipMetadataHeader.get(0).equalsIgnoreCase("true")));
    JsonArrayBuilder models = Json.createArrayBuilder();
    JsonArrayBuilder metadata = Json.createArrayBuilder();
    for (Map.Entry<RestModelMetadata, RestModel> entry : (Set<Map.Entry<RestModelMetadata, RestModel>>) proxy.entrySet()) {
        try {
            models.add(JsonUtil.getJsonValue(entry.getValue()));
            RestModelMetadata md = entry.getKey();
            JsonObjectBuilder mdo = Json.createObjectBuilder();
            mdo.add("id", md.getId());
            metadata.add(mdo.build());
        } catch (JsonException e) {
            Logger.getLogger(RestCollectionProvider.class.getName()).log(Level.SEVERE, "Unable to parse create Json", e);
        }
    }
    JsonObjectBuilder response = Json.createObjectBuilder();
    try {
        response.add("items", models.build());
        if (!skipMetadata) {
            response.add("metadata", metadata.build());
        }
        sb.append(response.toString());
    } catch (JsonException e) {
        Logger.getLogger(RestCollectionProvider.class.getName()).log(Level.SEVERE, "Unable to parse create Json", e);
    }
    return (wrapObject ? " { items : " : "") + sb.toString() + (wrapObject ? "}" : "");
}
Also used : JsonException(javax.json.JsonException) Set(java.util.Set) RestModel(org.glassfish.admin.rest.composite.RestModel) RestModelMetadata(org.glassfish.admin.rest.composite.metadata.RestModelMetadata) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 64 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class ResponseVerifier method body.

public ResponseVerifier body(StringValue want) throws Exception {
    ObjectValue objectWant = new ObjectValue();
    objectWant.put("string", want);
    JsonObjectBuilder objectHave = Json.createObjectBuilder();
    objectHave.add("string", getResponse().getStringBody());
    verifyData(objectWant, objectHave.build());
    return this;
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 65 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class ResponseVerifier method headers.

public ResponseVerifier headers(ObjectValue objectWant) throws Exception {
    JsonObjectBuilder objectHave = Json.createObjectBuilder();
    for (Entry<String, List<String>> header : getResponse().getJaxrsResponse().getStringHeaders().entrySet()) {
        String name = header.getKey();
        JsonArrayBuilder values = Json.createArrayBuilder();
        for (String value : header.getValue()) {
            values.add(value);
        }
        objectHave.add(name, values.build());
    }
    verifyData(objectWant, objectHave.build());
    return this;
}
Also used : List(java.util.List) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)76 JsonArrayBuilder (javax.json.JsonArrayBuilder)31 Map (java.util.Map)19 JsonObject (javax.json.JsonObject)19 JsonException (javax.json.JsonException)14 HashMap (java.util.HashMap)9 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)6 JsonValue (javax.json.JsonValue)6 List (java.util.List)5 JsonString (javax.json.JsonString)5 PrintWriter (java.io.PrintWriter)3 Entry (java.util.Map.Entry)3 JsonArray (javax.json.JsonArray)3 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)2 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 JsonWriter (javax.json.JsonWriter)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2