private Response buildResponse( final RestfulResponse.HttpStatusCode httpStatusCode, final String message, final JsonRepresentation body) { final ResponseBuilder builder = Response.status(httpStatusCode.getJaxrsStatusType()); if (message != null) { builder.header(RestfulResponse.Header.WARNING.getName(), RestfulResponse.Header.WARNING.render(message)); } final SerializationStrategy serializationStrategy = SerializationStrategy.JSON; // hmm; the mediaType doesn't seem to be specified in the RO spec builder.type(serializationStrategy.type(RepresentationType.GENERIC)); builder.entity(body.toString()); return builder.build(); }
private Response buildResponse( final RestfulResponse.HttpStatusCode httpStatusCode, final ExceptionPojo exceptionPojo) { final ResponseBuilder builder = Response.status(httpStatusCode.getJaxrsStatusType()); final List<MediaType> acceptableMediaTypes = httpHeaders.getAcceptableMediaTypes(); final SerializationStrategy serializationStrategy = acceptableMediaTypes.contains(MediaType.APPLICATION_XML_TYPE) || acceptableMediaTypes.contains(RepresentationType.OBJECT_LAYOUT.getXmlMediaType()) ? SerializationStrategy.XML : SerializationStrategy.JSON; final String message = exceptionPojo.getMessage(); if (message != null) { builder.header(RestfulResponse.Header.WARNING.getName(), RestfulResponse.Header.WARNING.render(message)); } builder.type(serializationStrategy.type(RepresentationType.ERROR)); builder.entity(serializationStrategy.entity(exceptionPojo)); return builder.build(); }
@Override public Response toResponse(final RestfulObjectsApplicationException ex) { final ResponseBuilder builder = Response.status(ex.getHttpStatusCode().getJaxrsStatusType()); // body and content-type final JsonRepresentation bodyRepr = ex.getBody(); final Throwable cause = ex.getCause(); if (bodyRepr != null) { final String body = bodyRepr.toString(); builder.entity(body); builder.type(MediaType.APPLICATION_JSON); // generic; the spec doesn't define what the media type should be } else if(cause == null) { builder.type(MediaType.APPLICATION_JSON); // generic; the spec doesn't define what the media type should be } else { String body; try { body = JsonMapper.instance().write(ExceptionPojo.create(cause)); } catch (final Exception e) { // fallback body = "{ \"exception\": \"" + ExceptionUtils.getFullStackTrace(cause) + "\" }"; } builder.entity(body); builder.type(RestfulMediaType.APPLICATION_JSON_ERROR); } final String message = ex.getMessage(); if (message != null) { builder.header(RestfulResponse.Header.WARNING.getName(), RestfulResponse.Header.WARNING.render(message)); } return builder.build(); }
private static <V> V getHeader(final Response response, final Header<V> header) { final MultivaluedMap<String, Object> metadata = response.getMetadata(); // always returns a String final String value = (String) metadata.getFirst(header.getName()); return header.parse(value); }