Search in sources :

Example 1 with JsonGeneratorFactory

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();
    }
}
Also used : Arrays(java.util.Arrays) XSPErrorPage(com.ibm.designer.runtime.domino.adapter.util.XSPErrorPage) Context(jakarta.ws.rs.core.Context) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) WebApplicationException(jakarta.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) Response(jakarta.ws.rs.core.Response) Controller(jakarta.mvc.Controller) OutputStreamWriter(java.io.OutputStreamWriter) Priority(jakarta.annotation.Priority) Produces(jakarta.ws.rs.Produces) Request(jakarta.ws.rs.core.Request) Method(java.lang.reflect.Method) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) NotesException(lotus.domino.NotesException) PrintWriter(java.io.PrintWriter) ConstraintViolation(jakarta.validation.ConstraintViolation) NotFoundException(jakarta.ws.rs.NotFoundException) IOException(java.io.IOException) ConstraintViolationException(jakarta.validation.ConstraintViolationException) UriInfo(jakarta.ws.rs.core.UriInfo) JsonGenerator(jakarta.json.stream.JsonGenerator) StandardCharsets(java.nio.charset.StandardCharsets) Json(jakarta.json.Json) Priorities(jakarta.ws.rs.Priorities) Objects(java.util.Objects) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) ResourceInfo(jakarta.ws.rs.container.ResourceInfo) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Collections(java.util.Collections) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) IOException(java.io.IOException) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) ServletException(javax.servlet.ServletException) NotesException(lotus.domino.NotesException) ConstraintViolation(jakarta.validation.ConstraintViolation) MediaType(jakarta.ws.rs.core.MediaType) ConstraintViolationException(jakarta.validation.ConstraintViolationException) JsonGenerator(jakarta.json.stream.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ArrayList(java.util.ArrayList) List(java.util.List) PrintWriter(java.io.PrintWriter)

Example 2 with JsonGeneratorFactory

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();
    }
}
Also used : JsonGenerator(jakarta.json.stream.JsonGenerator) JsonObject(jakarta.json.JsonObject) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) BeforeClass(org.junit.BeforeClass)

Example 3 with JsonGeneratorFactory

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();
    }
}
Also used : Variant(jakarta.ws.rs.core.Variant) JsonGenerator(jakarta.json.stream.JsonGenerator) StreamingOutput(jakarta.ws.rs.core.StreamingOutput) OutputStreamWriter(java.io.OutputStreamWriter) JsonGeneratorFactory(jakarta.json.stream.JsonGeneratorFactory) PrintWriter(java.io.PrintWriter)

Aggregations

JsonGenerator (jakarta.json.stream.JsonGenerator)3 JsonGeneratorFactory (jakarta.json.stream.JsonGeneratorFactory)3 StreamingOutput (jakarta.ws.rs.core.StreamingOutput)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 XSPErrorPage (com.ibm.designer.runtime.domino.adapter.util.XSPErrorPage)1 Priority (jakarta.annotation.Priority)1 Json (jakarta.json.Json)1 JsonObject (jakarta.json.JsonObject)1 Controller (jakarta.mvc.Controller)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 ConstraintViolation (jakarta.validation.ConstraintViolation)1 ConstraintViolationException (jakarta.validation.ConstraintViolationException)1 NotFoundException (jakarta.ws.rs.NotFoundException)1 Priorities (jakarta.ws.rs.Priorities)1 Produces (jakarta.ws.rs.Produces)1 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 ResourceInfo (jakarta.ws.rs.container.ResourceInfo)1 Context (jakarta.ws.rs.core.Context)1