private RestfulResponse(final Response response, final Class<T> returnType) { this.response = response; this.httpStatusCode = HttpStatusCode.statusFor(response.getStatus()); this.returnType = returnType; }
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(); }
private final static synchronized HttpStatusCode syncStatusFor(final int statusCode) { HttpStatusCode httpStatusCode = statusCodes.get(statusCode); if (httpStatusCode == null) { httpStatusCode = new HttpStatusCode(statusCode, null); statusCodes.put(statusCode, httpStatusCode); } return httpStatusCode; }
@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 RestfulResponse.HttpStatusCode determineStatusCode(final T ex) { final List<Throwable> chain = Throwables.getCausalChain(ex); RestfulResponse.HttpStatusCode statusCode; if(ex instanceof Failure) { Failure failure = (Failure) ex; statusCode = RestfulResponse.HttpStatusCode.statusFor(failure.getErrorCode()); } else if(!FluentIterable.from(chain).filter(RecoverableException.class).isEmpty()) { statusCode = RestfulResponse.HttpStatusCode.OK; } else if(ex instanceof ExceptionWithHttpStatusCode) { ExceptionWithHttpStatusCode exceptionWithHttpStatusCode = (ExceptionWithHttpStatusCode) ex; statusCode = exceptionWithHttpStatusCode.getHttpStatusCode(); } else { statusCode = RestfulResponse.HttpStatusCode.INTERNAL_SERVER_ERROR; } return statusCode; }
Response buildResponse(final T ex) { final RestfulResponse.HttpStatusCode httpStatusCode = determineStatusCode(ex); final String message = messageFor(ex); if(ex instanceof ExceptionWithBody) { final ExceptionWithBody exceptionWithBody = (ExceptionWithBody) ex; final JsonRepresentation body = exceptionWithBody.getBody(); if(body != null) { return buildResponse(httpStatusCode, message, body); } } final ExceptionPojo exceptionPojo = new ExceptionPojo( httpStatusCode.getStatusCode(), message, detailIfRequired(httpStatusCode, ex) ); return buildResponse(httpStatusCode, exceptionPojo); }
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(); }
@Test public void knownStatusCode() { assertThat(HttpStatusCode.statusFor(200), is(HttpStatusCode.OK)); }
protected static Response.ResponseBuilder of(final RestfulResponse.HttpStatusCode httpStatusCode) { return Response.status(httpStatusCode.getJaxrsStatusType()).type(MediaType.APPLICATION_JSON_TYPE); }
protected static ResponseBuilder responseOf(final HttpStatusCode httpStatusCode) { return Response.status(httpStatusCode.getJaxrsStatusType()).type(MediaType.APPLICATION_JSON_TYPE); }
@Test public void simpleNoMessage() throws Exception { // given final HttpStatusCode status = HttpStatusCode.BAD_REQUEST; final RestfulObjectsApplicationException ex = RestfulObjectsApplicationException.create(status); // when final Response response = exceptionMapper.toResponse(ex); // then assertThat(HttpStatusCode.lookup(response.getStatus()), is(status)); assertThat(response.getMetadata().get("Warning"), is(nullValue())); // and then final String entity = (String) response.getEntity(); assertThat(entity, is(nullValue())); }
@Test public void knownStatusCode() { assertThat(HttpStatusCode.statusFor(200), is(HttpStatusCode.OK)); }
@Override public Response toResponse(final RuntimeException ex) { HttpStatusCode statusCode = HttpStatusCode.INTERNAL_SERVER_ERROR; if(ex instanceof Failure) { Failure failure = (Failure) ex; statusCode = HttpStatusCode.statusFor(failure.getErrorCode()); } final ResponseBuilder builder = Response.status(statusCode.getJaxrsStatusType()).type(RestfulMediaType.APPLICATION_JSON_ERROR).entity(jsonFor(ex)); return builder.build(); }
@Test public void unknownStatusCode() { final HttpStatusCode statusFor = HttpStatusCode.statusFor(600); assertThat(statusFor.getStatusCode(), is(600)); assertThat(statusFor.getFamily(), is(Family.OTHER)); }
private int getHttpStatusCodeIfAny(final Throwable ex) { if (!(ex instanceof HasHttpStatusCode)) { return 0; } final HasHttpStatusCode hasHttpStatusCode = (HasHttpStatusCode) ex; return hasHttpStatusCode.getHttpStatusCode().getStatusCode(); }
private Step addStep(final String key, final LinkRepresentation link, final JsonRepresentation body, final RestfulResponse<JsonRepresentation> jsonResp, final String error, final Exception ex) { final Step step = new Step(key, link, body, jsonResp, error, ex); steps.add(0, step); if (error != null) { if (jsonResp.getStatus().getFamily() != Family.SUCCESSFUL) { step.error = "response status code: " + jsonResp.getStatus(); } } return step; }
@Test public void unknownStatusCode() { final HttpStatusCode statusFor = HttpStatusCode.statusFor(600); assertThat(statusFor.getStatusCode(), is(600)); assertThat(statusFor.getFamily(), is(Family.OTHER)); }