com.sun.ws.rest.spi.container
Class AbstractContainerResponse

java.lang.Object
  extended by com.sun.ws.rest.spi.container.AbstractContainerResponse
All Implemented Interfaces:
HttpResponseContext, ContainerResponse

public abstract class AbstractContainerResponse
extends java.lang.Object
implements ContainerResponse

An abstract implementation of ContainerResponse.

Specific containers may extend this class and instances may be passed to the runtime using the method WebApplication.handleRequest(com.sun.ws.rest.spi.container.ContainerRequest, com.sun.ws.rest.spi.container.ContainerResponse).

When the call to the method WebApplication.handleRequest(com.sun.ws.rest.spi.container.ContainerRequest, com.sun.ws.rest.spi.container.ContainerResponse) returns a container must commit the response, if the response has not already been committed, by committing the status and headers, and writing the entity to the underlying output stream, for example:

   if (!isCommitted()) {
       commitStatusAndHeaders();
       writeEntity(getUnderlyingOutputStream());
   }
 

The runtime may call the method getUnderlyingOutputStream() and before any bytes are written to this stream call the method commitStatusAndHeaders(). When one or more bytes are written to the stream the response is marked as committed. Such behaviour arises when a resource chooses to write an entity directly to an output stream obtained from the method getOutputStream().


Constructor Summary
protected AbstractContainerResponse(MessageBodyContext bodyContext, ContainerRequest request)
           
 
Method Summary
protected abstract  void commitStatusAndHeaders()
          Commit the status code and headers (if any) to the underlying container response.
 java.lang.Object getEntity()
          Get the entity of the response
 java.lang.String getHeaderValue(java.lang.Object headerValue)
           
 javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> getHttpHeaders()
          Get the HTTP response headers.
 java.io.OutputStream getOutputStream()
          Get an OutputStream to which an entity may be written.
 int getStatus()
          Get the status of the response.
protected abstract  java.io.OutputStream getUnderlyingOutputStream()
          Get the OutputStream provided by the underlying container response.
 boolean isCommitted()
          Ascertain if a response has been committed to an underlying container.
 boolean isResponseSet()
          Check if the response has been set using the setReponse methods.
 void setEntity(java.lang.Object entity)
          Set the entity of the response
 void setResponse(javax.ws.rs.core.Response response)
          Set the response state from a Response instance.
 void setResponse(javax.ws.rs.core.Response response, javax.ws.rs.core.MediaType contentType)
          Set the response state from a Response instance.
 void setStatus(int status)
          Set the status of the response.
protected  void writeEntity(java.lang.Object entity, java.io.OutputStream out)
          Write the entity to the output stream
protected  void writeEntity(java.io.OutputStream out)
          Write the entity to the output stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractContainerResponse

protected AbstractContainerResponse(MessageBodyContext bodyContext,
                                    ContainerRequest request)
Parameters:
bodyContext - the message body context.
request - the container request associated with this response.
Method Detail

getUnderlyingOutputStream

protected abstract java.io.OutputStream getUnderlyingOutputStream()
                                                           throws java.io.IOException
Get the OutputStream provided by the underlying container response.

Returns:
the OutputStream of the underlying container response.
Throws:
java.io.IOException

commitStatusAndHeaders

protected abstract void commitStatusAndHeaders()
                                        throws java.io.IOException
Commit the status code and headers (if any) to the underlying container response.

Throws:
java.io.IOException

setResponse

public final void setResponse(javax.ws.rs.core.Response response)
Description copied from interface: HttpResponseContext
Set the response state from a Response instance. This replaces a pre-existing response state.

If an entity is set but there is no MIME media type declared for the Content-Type response header then the MIME media type will be set to "application/octet-stream".

Specified by:
setResponse in interface HttpResponseContext
Parameters:
response - the response.

setResponse

public final void setResponse(javax.ws.rs.core.Response response,
                              javax.ws.rs.core.MediaType contentType)
Description copied from interface: HttpResponseContext
Set the response state from a Response instance. This replaces a pre-existing response state.

Specified by:
setResponse in interface HttpResponseContext
Parameters:
response - the response.
contentType - the MIME media type to use fot the Content-Type response header if the header is not set by the response. If null then "application/octet-stream" will be used.

isResponseSet

public final boolean isResponseSet()
Description copied from interface: HttpResponseContext
Check if the response has been set using the setReponse methods.

Specified by:
isResponseSet in interface HttpResponseContext
Returns:
true if the response has been set.

getStatus

public final int getStatus()
Description copied from interface: HttpResponseContext
Get the status of the response.

Specified by:
getStatus in interface HttpResponseContext

setStatus

public final void setStatus(int status)
Description copied from interface: HttpResponseContext
Set the status of the response.

Specified by:
setStatus in interface HttpResponseContext

getEntity

public final java.lang.Object getEntity()
Description copied from interface: HttpResponseContext
Get the entity of the response

Specified by:
getEntity in interface HttpResponseContext

setEntity

public final void setEntity(java.lang.Object entity)
Description copied from interface: HttpResponseContext
Set the entity of the response

Specified by:
setEntity in interface HttpResponseContext

getHttpHeaders

public final javax.ws.rs.core.MultivaluedMap<java.lang.String,java.lang.Object> getHttpHeaders()
Description copied from interface: HttpResponseContext
Get the HTTP response headers. The returned map is case-insensitive wrt keys. Note that setHttpResponse can change the HTTP response headers and may overwrite headers set previously.

Specified by:
getHttpHeaders in interface HttpResponseContext
Returns:
a mutable map of HTTP header names and values that will be included in the response. Any headers explicitly set will override automatically generated values.

getOutputStream

public final java.io.OutputStream getOutputStream()
                                           throws java.io.IOException
Description copied from interface: HttpResponseContext
Get an OutputStream to which an entity may be written.

The first byte written will cause the status code and headers (if any) to be committed to the underlying container.

Specified by:
getOutputStream in interface HttpResponseContext
Returns:
the output stream
Throws:
java.io.IOException - if an IO error occurs

isCommitted

public final boolean isCommitted()
Description copied from interface: HttpResponseContext
Ascertain if a response has been committed to an underlying container.

A response is committed if the status code, headers (if any) have been committed to the underlying container.

Specified by:
isCommitted in interface HttpResponseContext
Returns:
true if the response has been committed.

writeEntity

protected final void writeEntity(java.io.OutputStream out)
                          throws java.io.IOException
Write the entity to the output stream

Throws:
java.io.IOException

writeEntity

protected final void writeEntity(java.lang.Object entity,
                                 java.io.OutputStream out)
                          throws java.io.IOException
Write the entity to the output stream

Throws:
java.io.IOException

getHeaderValue

public java.lang.String getHeaderValue(java.lang.Object headerValue)