use of jakarta.json.stream.JsonGeneratorFactory in project org.openntf.xsp.jakartaee by OpenNTF.
the class GenericThrowableMapper method createResponseFromException.
protected Response createResponseFromException(final Throwable throwable, final int status, ResourceInfo resourceInfo, HttpServletRequest req) {
MediaType type = getMediaType(resourceInfo);
if (MediaType.TEXT_HTML_TYPE.isCompatible(type)) {
// Handle as HTML
return Response.status(status).type(MediaType.TEXT_HTML_TYPE).entity((StreamingOutput) out -> {
try (PrintWriter w = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))) {
XSPErrorPage.handleException(w, throwable, req.getRequestURL().toString(), false);
} catch (ServletException e) {
throw new IOException(e);
}
}).build();
} else {
// Handle as JSON
return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity((StreamingOutput) out -> {
Objects.requireNonNull(out);
String message = "";
Throwable t = throwable;
while ((message == null || message.length() == 0) && t != null) {
if (t instanceof NotesException) {
message = ((NotesException) t).text;
} else if (t instanceof ConstraintViolationException) {
message = t.getMessage();
if (message == null || message.isEmpty()) {
List<String> cvMsgList = new ArrayList<>();
for (@SuppressWarnings("rawtypes") ConstraintViolation cv : ((ConstraintViolationException) t).getConstraintViolations()) {
String cvMsg = cv.getPropertyPath() + ": " + cv.getMessage();
cvMsgList.add(cvMsg);
}
message = String.join(",", cvMsgList);
}
} else {
message = t.getMessage();
}
t = t.getCause();
}
JsonGeneratorFactory jsonFac = Json.createGeneratorFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
try (JsonGenerator json = jsonFac.createGenerator(out)) {
json.writeStartObject();
json.write("message", throwable.getClass().getName() + ": " + message);
json.writeKey("stackTrace");
json.writeStartArray();
for (Throwable cause = throwable; cause != null; cause = cause.getCause()) {
json.writeStartArray();
json.write(cause.getClass().getName() + ": " + cause.getLocalizedMessage());
Arrays.stream(cause.getStackTrace()).map(String::valueOf).map(line -> " at " + line).forEach(json::write);
json.writeEnd();
}
json.writeEnd();
json.writeEnd();
}
}).build();
}
}
use of jakarta.json.stream.JsonGeneratorFactory in project keycloak-extensions-for-fhir by Alvearie.
the class PropertyGroupTest method setup.
@BeforeClass
public static void setup() {
// Build a JSON object for testing.
obj = BUILDER_FACTORY.createObjectBuilder().add("level1", BUILDER_FACTORY.createObjectBuilder().add("level2", BUILDER_FACTORY.createObjectBuilder().add("scalars", BUILDER_FACTORY.createObjectBuilder().add("stringProp", "stringValue").add("intProp", 123).add("booleanProp", true).add("booleanProp-2", "true")).add("arrays", BUILDER_FACTORY.createObjectBuilder().add("int-array", BUILDER_FACTORY.createArrayBuilder().add(1).add(2).add(3)).add("string-array", BUILDER_FACTORY.createArrayBuilder().add("one").add("two")).add("object-array", BUILDER_FACTORY.createArrayBuilder().add(BUILDER_FACTORY.createObjectBuilder().add("attr1", "val1")).add(BUILDER_FACTORY.createObjectBuilder().add("attr2", "val2")))).add("nulls", BUILDER_FACTORY.createObjectBuilder().add("nullProp", JsonValue.NULL).add("nullArrayProp", BUILDER_FACTORY.createArrayBuilder().add(JsonValue.NULL))))).build();
if (DEBUG) {
Map<String, Object> config = Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true);
JsonGeneratorFactory factory = Json.createGeneratorFactory(config);
JsonGenerator generator = factory.createGenerator(System.out);
generator.write(obj);
generator.flush();
System.out.println();
}
}
use of jakarta.json.stream.JsonGeneratorFactory in project org.openntf.xsp.jakartaee by OpenNTF.
the class NotFoundMapper method toResponse.
public Response toResponse(NotFoundException exception, Request request, UriInfo uriInfo) {
List<Variant> options = Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_HTML_TYPE).build();
Variant preferred = request.selectVariant(options);
if (MediaType.TEXT_HTML_TYPE.isCompatible(preferred.getMediaType())) {
return Response.status(Status.NOT_FOUND).type(MediaType.TEXT_HTML_TYPE).entity((StreamingOutput) out -> {
try (OutputStreamWriter outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
PrintWriter w = new PrintWriter(outWriter)) {
XSPErrorPage.handlePageNotFound(w, uriInfo.getRequestUri().toString(), exception, null, false);
}
}).build();
} else {
return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON_TYPE).entity((StreamingOutput) out -> {
JsonGeneratorFactory jsonFac = Json.createGeneratorFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
try (JsonGenerator json = jsonFac.createGenerator(out)) {
json.writeStartObject();
json.write("success", false);
json.write("status", Status.NOT_FOUND.getStatusCode());
json.write("errorMessage", exception.toString());
json.writeEnd();
}
}).build();
}
}
Aggregations