public <T> byte[] toBytes(MediaType mediaType, T o) throws IOException { if (o == null) { return null; } Class<T> clazz = (Class<T>) o.getClass(); MessageBodyWriter<T> writer = findWriter(clazz, null, EMPTY_ANNOTATIONS, mediaType); if (writer == null) { throw new IllegalArgumentException( "cannot convert " + clazz.getName() + " into byte[] (" + mediaType + ")"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(512); writer.writeTo(o, clazz, clazz, EMPTY_ANNOTATIONS, mediaType, EMPTY_OBJECT_HEADERS, baos); return baos.toByteArray(); }
@SuppressWarnings("unchecked") public void writeBody(GenericContainerResponse response, MessageBodyWriter entityWriter) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); Object entity = response.getEntity(); if (entity != null) { entityWriter.writeTo( entity, entity.getClass(), response.getEntityType(), null, response.getContentType(), response.getHttpHeaders(), out); body = out.toByteArray(); } }
/** * Write the response. * * <p>The status and headers will be written by calling the method {@link * ContainerResponseWriter#writeStatusAndHeaders} on the provided {@link ContainerResponseWriter} * instance. The {@link OutputStream} returned from that method call is used to write the entity * (if any) to that {@link OutputStream}. An appropriate {@link MessageBodyWriter} will be found * to write the entity. * * @throws WebApplicationException if {@link MessageBodyWriter} cannot be found for the entity * with a 500 (Internal Server error) response. * @throws java.io.IOException if there is an error writing the entity */ public void write() throws IOException { if (isCommitted) return; if (request.isTracingEnabled()) { configureTrace(responseWriter); } if (entity == null) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); responseWriter.finish(); return; } if (!getHttpHeaders().containsKey(HttpHeaders.VARY)) { final String varyHeader = (String) request.getProperties().get(ContainerRequest.VARY_HEADER); if (varyHeader != null) { getHttpHeaders().add(HttpHeaders.VARY, varyHeader); } } MediaType contentType = getMediaType(); if (contentType == null) { contentType = getMessageBodyWorkers() .getMessageBodyWriterMediaType( entity.getClass(), entityType, annotations, request.getAcceptableMediaTypes()); if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE; getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType); } final MessageBodyWriter p = getMessageBodyWorkers() .getMessageBodyWriter(entity.getClass(), entityType, annotations, contentType); if (p == null) { String message = "A message body writer for Java class " + entity.getClass().getName() + ", and Java type " + entityType + ", and MIME media type " + contentType + " was not found"; LOGGER.severe(message); Map<MediaType, List<MessageBodyWriter>> m = getMessageBodyWorkers().getWriters(contentType); LOGGER.severe( "The registered message body writers compatible with the MIME media type are:\n" + getMessageBodyWorkers().writersToString(m)); if (request.getMethod().equals("HEAD")) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); responseWriter.finish(); return; } else { throw new WebApplicationException(new MessageException(message), 500); } } final long size = p.getSize(entity, entity.getClass(), entityType, annotations, contentType); if (request.getMethod().equals("HEAD")) { if (size != -1) getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size)); isCommitted = true; responseWriter.writeStatusAndHeaders(0, this); } else { if (request.isTracingEnabled()) { request.trace( String.format( "matched message body writer: %s, \"%s\" -> %s", ReflectionHelper.objectToString(entity), contentType, ReflectionHelper.objectToString(p))); } if (out == null) out = new CommittingOutputStream(size); p.writeTo( entity, entity.getClass(), entityType, annotations, contentType, getHttpHeaders(), out); if (!isCommitted) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); } } responseWriter.finish(); }