private static Response createErrorReply(int status, Throwable cause, MediaType mediaType) { StringBuilder sb = new StringBuilder(cause.getClass().getName()); if (cause.getMessage() != null) { sb.append(": ").append(cause.getMessage()); } return createErrorReply(status, sb.toString(), mediaType); }
@Override public void writeTo(Object serializableObject, Class serializableClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, OutputStream outputStream) throws IOException, WebApplicationException { EncoderWrapper encoder = "json".equalsIgnoreCase(mediaType.getSubtype()) ? jsonEncoder : xmlEncoder; // XML codec may not be available if (encoder == null) { throw new WebApplicationException(createErrorReply(400, "No codec available to serialize content type " + mediaType, mediaType)); } encoder.encode(serializableObject, outputStream); }
@Override public Object readFrom(Class serializableClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, InputStream inputStream) throws IOException { DecoderWrapper decoder; if (MediaType.MEDIA_TYPE_WILDCARD.equals(mediaType.getSubtype())) { decoder = xmlDecoder; } else if ("json".equalsIgnoreCase(mediaType.getSubtype())) { decoder = jsonDecoder; } else { decoder = xmlDecoder; // default } try { return decoder.decode(inputStream, serializableClass); } catch (Throwable e) { if (e instanceof Error) { // See issue: https://github.com/Netflix/eureka/issues/72 on why we catch Error here. closeInputOnError(inputStream); throw new WebApplicationException(e, createErrorReply(500, e, mediaType)); } LOGGER.debug("Cannot parse request body", e); throw new WebApplicationException(e, createErrorReply(400, "cannot parse request body", mediaType)); } }
private static Response createErrorReply(int status, Throwable cause, MediaType mediaType) { StringBuilder sb = new StringBuilder(cause.getClass().getName()); if (cause.getMessage() != null) { sb.append(": ").append(cause.getMessage()); } return createErrorReply(status, sb.toString(), mediaType); }
@Override public void writeTo(Object serializableObject, Class serializableClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, OutputStream outputStream) throws IOException, WebApplicationException { EncoderWrapper encoder = "json".equalsIgnoreCase(mediaType.getSubtype()) ? jsonEncoder : xmlEncoder; // XML codec may not be available if (encoder == null) { throw new WebApplicationException(createErrorReply(400, "No codec available to serialize content type " + mediaType, mediaType)); } encoder.encode(serializableObject, outputStream); }
@Override public Object readFrom(Class serializableClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap headers, InputStream inputStream) throws IOException { DecoderWrapper decoder; if (MediaType.MEDIA_TYPE_WILDCARD.equals(mediaType.getSubtype())) { decoder = xmlDecoder; } else if ("json".equalsIgnoreCase(mediaType.getSubtype())) { decoder = jsonDecoder; } else { decoder = xmlDecoder; // default } try { return decoder.decode(inputStream, serializableClass); } catch (Throwable e) { if (e instanceof Error) { // See issue: https://github.com/Netflix/eureka/issues/72 on why we catch Error here. closeInputOnError(inputStream); throw new WebApplicationException(e, createErrorReply(500, e, mediaType)); } LOGGER.debug("Cannot parse request body", e); throw new WebApplicationException(e, createErrorReply(400, "cannot parse request body", mediaType)); } }