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();
}
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();
}
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 ? "}" : "");
}
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;
}
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;
}
Aggregations