001 /*
002 * The contents of this file are subject to the terms
003 * of the Common Development and Distribution License
004 * (the "License"). You may not use this file except
005 * in compliance with the License.
006 *
007 * You can obtain a copy of the license at
008 * http://www.opensource.org/licenses/cddl1.php
009 * See the License for the specific language governing
010 * permissions and limitations under the License.
011 */
012
013 /*
014 * MessageBodyWriter.java
015 *
016 * Created on November 8, 2007, 3:57 PM
017 *
018 */
019
020 package javax.ws.rs.ext;
021
022 import java.io.IOException;
023 import java.io.OutputStream;
024 import java.lang.annotation.Annotation;
025 import java.lang.reflect.Type;
026 import javax.ws.rs.WebApplicationException;
027 import javax.ws.rs.core.MediaType;
028 import javax.ws.rs.core.MultivaluedMap;
029
030 /**
031 * Contract for a provider that supports the conversion of a Java type to a
032 * stream. To add a <code>MessageBodyWriter</code> implementation, annotate the
033 * implementation class with <code>@Provider</code>.
034 *
035 * A <code>MessageBodyWriter</code> implementation may be annotated
036 * with {@link javax.ws.rs.Produces} to restrict the media types for which it will
037 * be considered suitable.
038 *
039 * @param T the type that can be written
040 * @see Provider
041 * @see javax.ws.rs.Produces
042 */
043 public interface MessageBodyWriter<T> {
044
045 /**
046 * Ascertain if the MessageBodyWriter supports a particular type.
047 *
048 * @param type the class of object that is to be written.
049 * @param genericType the type of object to be written, obtained either
050 * by reflection of a resource method return type or via inspection
051 * of the returned instance. {@link javax.ws.rs.core.GenericEntity}
052 * provides a way to specify this information at runtime.
053 * @param annotations an array of the annotations on the resource
054 * method that returns the object.
055 * @param mediaType the media type of the HTTP entity.
056 * @return true if the type is supported, otherwise false.
057 */
058 boolean isWriteable(Class<?> type, Type genericType,
059 Annotation annotations[], MediaType mediaType);
060
061 /**
062 * Called before <code>writeTo</code> to ascertain the length in bytes of
063 * the serialized form of <code>t</code>. A non-negative return value is
064 * used in a HTTP <code>Content-Length</code> header.
065 * @param t the instance to write
066 * @param type the class of object that is to be written.
067 * @param genericType the type of object to be written, obtained either
068 * by reflection of a resource method return type or by inspection
069 * of the returned instance. {@link javax.ws.rs.core.GenericEntity}
070 * provides a way to specify this information at runtime.
071 * @param annotations an array of the annotations on the resource
072 * method that returns the object.
073 * @param mediaType the media type of the HTTP entity.
074 * @return length in bytes or -1 if the length cannot be determined in
075 * advance
076 */
077 long getSize(T t, Class<?> type, Type genericType, Annotation annotations[],
078 MediaType mediaType);
079
080 /**
081 * Write a type to an HTTP response. The response header map is mutable
082 * but any changes must be made before writing to the output stream since
083 * the headers will be flushed prior to writing the response body.
084 *
085 * @param t the instance to write.
086 * @param type the class of object that is to be written.
087 * @param genericType the type of object to be written, obtained either
088 * by reflection of a resource method return type or by inspection
089 * of the returned instance. {@link javax.ws.rs.core.GenericEntity}
090 * provides a way to specify this information at runtime.
091 * @param annotations an array of the annotations on the resource
092 * method that returns the object.
093 * @param mediaType the media type of the HTTP entity.
094 * @param httpHeaders a mutable map of the HTTP response headers.
095 * @param entityStream the {@link OutputStream} for the HTTP entity. The
096 * implementation should not close the output stream.
097 * @throws java.io.IOException if an IO error arises
098 * @throws javax.ws.rs.WebApplicationException if a specific
099 * HTTP error response needs to be produced. Only effective if thrown prior
100 * to the response being committed.
101 */
102 void writeTo(T t, Class<?> type, Type genericType, Annotation annotations[],
103 MediaType mediaType,
104 MultivaluedMap<String, Object> httpHeaders,
105 OutputStream entityStream) throws IOException, WebApplicationException;
106 }