org.glassfish.jersey.server
Class ContainerResponse

java.lang.Object
  extended by org.glassfish.jersey.server.ContainerResponse
All Implemented Interfaces:
ContainerResponseContext

public class ContainerResponse
extends Object
implements ContainerResponseContext

Jersey container response context.

Author:
Marek Potociar (marek.potociar at oracle.com)

Constructor Summary
ContainerResponse(ContainerRequest requestContext, Response response)
          Create a new Jersey container response context.
 
Method Summary
 void close()
          Closes the response.
 void commitStream()
          Commit the entity stream unless already committed.
 void enableBuffering(Configuration configuration)
          Enable a buffering of serialized entity.
 Set<String> getAllowedMethods()
          Get the allowed HTTP methods from the Allow HTTP header.
 Map<String,NewCookie> getCookies()
          Get any new cookies set on the response message.
 Date getDate()
          Get message date.
 Object getEntity()
          Get the message entity Java instance.
 Annotation[] getEntityAnnotations()
          Get the annotations attached to the entity instance.
 Class<?> getEntityClass()
          Get the raw entity type information.
 OutputStream getEntityStream()
          Get the entity output stream.
 EntityTag getEntityTag()
          Get the entity tag.
 Type getEntityType()
          Get the generic entity type information.
 MultivaluedMap<String,Object> getHeaders()
          Get the mutable response headers multivalued map.
 String getHeaderString(String name)
          Get a message header as a single string value.
 Locale getLanguage()
          Get the language of the entity.
 Date getLastModified()
          Get the last modified date.
 int getLength()
          Get Content-Length value.
 Link getLink(String relation)
          Get the link for the relation.
 Link.Builder getLinkBuilder(String relation)
          Convenience method that returns a Link.Builder for the relation.
 Set<Link> getLinks()
          Get the links attached to the message as header.
 URI getLocation()
          Get the location.
 MediaType getMediaType()
          Get the media type of the entity.
 ContainerRequest getRequestContext()
          Get the associated container request context paired with this response context.
 int getStatus()
          Get the status code associated with the response.
 Response.StatusType getStatusInfo()
          Get the complete status information associated with the response.
 MultivaluedMap<String,String> getStringHeaders()
          Get a string view of header values associated with the message.
 OutboundMessageContext getWrappedMessageContext()
          Get the wrapped response message context.
 boolean hasEntity()
          Check if there is an entity available in the response.
 boolean hasLink(String relation)
          Check if link for relation exists.
 boolean isChunked()
          Returns true if the response entity is a ChunkedOutput instance.
 boolean isCommitted()
          Returns true if the entity stream has been committed.
 boolean isMappedFromException()
          Returns true if the response is result of the exception (for example created during .ExceptionMapper exception mapping).
 void setEntity(Object entity)
          Set a new message message entity.
 void setEntity(Object entity, Annotation[] annotations)
          Set a new message message entity.
 void setEntity(Object entity, Annotation[] annotations, MediaType mediaType)
          Set a new message entity, including the attached annotations and the media type.
 void setEntity(Object entity, Type type, Annotation[] annotations)
          Set a new message message entity.
 void setEntityAnnotations(Annotation[] annotations)
          Set the annotations attached to the entity.
 void setEntityStream(OutputStream outputStream)
          Set a new entity output stream.
 void setEntityType(Type type)
          Set the message entity type information.
 void setMappedFromException(boolean mappedFromException)
          Sets the flag indicating whether the response was created based on the exception.
 void setMediaType(MediaType mediaType)
          Set the message content media type.
 void setStatus(int code)
          Set a new response status code.
 void setStatusInfo(Response.StatusType status)
          Set the complete status information (status code and reason phrase) associated with the response.
 void setStreamProvider(OutboundMessageContext.StreamProvider streamProvider)
          Set the output stream provider callback.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContainerResponse

public ContainerResponse(ContainerRequest requestContext,
                         Response response)
Create a new Jersey container response context.

Parameters:
requestContext - associated container request context.
response - response instance initializing the response context.
Method Detail

isMappedFromException

public boolean isMappedFromException()
Returns true if the response is result of the exception (for example created during .ExceptionMapper exception mapping).

Returns:
True if this response was created based on the exception, false otherwise.

setMappedFromException

public void setMappedFromException(boolean mappedFromException)
Sets the flag indicating whether the response was created based on the exception.

Parameters:
mappedFromException - True if this exception if result of the exception (for example result of exception mapping).

getStatus

public int getStatus()
Description copied from interface: ContainerResponseContext
Get the status code associated with the response.

Specified by:
getStatus in interface ContainerResponseContext
Returns:
the response status code or -1 if the status was not set.

setStatus

public void setStatus(int code)
Description copied from interface: ContainerResponseContext
Set a new response status code.

Specified by:
setStatus in interface ContainerResponseContext
Parameters:
code - new status code.

setStatusInfo

public void setStatusInfo(Response.StatusType status)
Description copied from interface: ContainerResponseContext
Set the complete status information (status code and reason phrase) associated with the response.

Specified by:
setStatusInfo in interface ContainerResponseContext
Parameters:
status - the response status information.

getStatusInfo

public Response.StatusType getStatusInfo()
Description copied from interface: ContainerResponseContext
Get the complete status information associated with the response.

Specified by:
getStatusInfo in interface ContainerResponseContext
Returns:
the response status information or null if the status was not set.

getRequestContext

public ContainerRequest getRequestContext()
Get the associated container request context paired with this response context.

Returns:
associated container request context.

getCookies

public Map<String,NewCookie> getCookies()
Description copied from interface: ContainerResponseContext
Get any new cookies set on the response message.

Specified by:
getCookies in interface ContainerResponseContext
Returns:
a read-only map of cookie name (String) to a new cookie.

getWrappedMessageContext

public OutboundMessageContext getWrappedMessageContext()
Get the wrapped response message context.

Returns:
wrapped response message context.

getHeaderString

public String getHeaderString(String name)
Description copied from interface: ContainerResponseContext
Get a message header as a single string value. Each single header value is converted to String using a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate.createHeaderDelegate(java.lang.Class) for the header value class or using its toString method if a header delegate is not available.

Specified by:
getHeaderString in interface ContainerResponseContext
Parameters:
name - the message header.
Returns:
the message header value. If the message header is not present then null is returned. If the message header is present but has no value then the empty string is returned. If the message header is present more than once then the values of joined together and separated by a ',' character.
See Also:
ContainerResponseContext.getHeaders(), ContainerResponseContext.getStringHeaders()

getHeaders

public MultivaluedMap<String,Object> getHeaders()
Description copied from interface: ContainerResponseContext
Get the mutable response headers multivalued map.

Specified by:
getHeaders in interface ContainerResponseContext
Returns:
mutable multivalued map of response headers.
See Also:
ContainerResponseContext.getStringHeaders(), ContainerResponseContext.getHeaderString(String)

getStringHeaders

public MultivaluedMap<String,String> getStringHeaders()
Description copied from interface: ContainerResponseContext
Get a string view of header values associated with the message. Changes in the underlying headers map are reflected in this view.

The method converts the non-string header values to strings using a RuntimeDelegate.HeaderDelegate if one is available via RuntimeDelegate.createHeaderDelegate(java.lang.Class) for the class of the value or using the values toString method if a header delegate is not available.

Specified by:
getStringHeaders in interface ContainerResponseContext
Returns:
response headers as a string view of header values.
See Also:
ContainerResponseContext.getHeaders(), ContainerResponseContext.getHeaderString(String)

getDate

public Date getDate()
Description copied from interface: ContainerResponseContext
Get message date.

Specified by:
getDate in interface ContainerResponseContext
Returns:
the message date, otherwise null if not present.

getLanguage

public Locale getLanguage()
Description copied from interface: ContainerResponseContext
Get the language of the entity.

Specified by:
getLanguage in interface ContainerResponseContext
Returns:
the language of the entity or null if not specified

getMediaType

public MediaType getMediaType()
Description copied from interface: ContainerResponseContext
Get the media type of the entity.

Specified by:
getMediaType in interface ContainerResponseContext
Returns:
the media type or null if not specified (e.g. there's no response entity).

getAllowedMethods

public Set<String> getAllowedMethods()
Description copied from interface: ContainerResponseContext
Get the allowed HTTP methods from the Allow HTTP header.

Specified by:
getAllowedMethods in interface ContainerResponseContext
Returns:
the allowed HTTP methods, all methods will returned as upper case strings.

getLength

public int getLength()
Description copied from interface: ContainerResponseContext
Get Content-Length value.

Specified by:
getLength in interface ContainerResponseContext
Returns:
Content-Length as integer if present and valid number. In other cases returns -1.

getEntityTag

public EntityTag getEntityTag()
Description copied from interface: ContainerResponseContext
Get the entity tag.

Specified by:
getEntityTag in interface ContainerResponseContext
Returns:
the entity tag, otherwise null if not present.

getLastModified

public Date getLastModified()
Description copied from interface: ContainerResponseContext
Get the last modified date.

Specified by:
getLastModified in interface ContainerResponseContext
Returns:
the last modified date, otherwise null if not present.

getLocation

public URI getLocation()
Description copied from interface: ContainerResponseContext
Get the location.

Specified by:
getLocation in interface ContainerResponseContext
Returns:
the location URI, otherwise null if not present.

getLinks

public Set<Link> getLinks()
Description copied from interface: ContainerResponseContext
Get the links attached to the message as header.

Specified by:
getLinks in interface ContainerResponseContext
Returns:
links, may return empty Set if no links are present. Never returns null.

hasLink

public boolean hasLink(String relation)
Description copied from interface: ContainerResponseContext
Check if link for relation exists.

Specified by:
hasLink in interface ContainerResponseContext
Parameters:
relation - link relation.
Returns:
true if the for the relation link exists, false otherwise.

getLink

public Link getLink(String relation)
Description copied from interface: ContainerResponseContext
Get the link for the relation.

Specified by:
getLink in interface ContainerResponseContext
Parameters:
relation - link relation.
Returns:
the link for the relation, otherwise null if not present.

getLinkBuilder

public Link.Builder getLinkBuilder(String relation)
Description copied from interface: ContainerResponseContext
Convenience method that returns a Link.Builder for the relation.

Specified by:
getLinkBuilder in interface ContainerResponseContext
Parameters:
relation - link relation.
Returns:
the link builder for the relation, otherwise null if not present.

hasEntity

public boolean hasEntity()
Description copied from interface: ContainerResponseContext
Check if there is an entity available in the response. The method returns true if the entity is present, returns false otherwise.

Specified by:
hasEntity in interface ContainerResponseContext
Returns:
true if there is an entity present in the message, false otherwise.

getEntity

public Object getEntity()
Description copied from interface: ContainerResponseContext
Get the message entity Java instance. Returns null if the message does not contain an entity.

Specified by:
getEntity in interface ContainerResponseContext
Returns:
the message entity or null if message does not contain an entity body.

setEntity

public void setEntity(Object entity)
Set a new message message entity.

Specified by:
setEntity in interface ContainerResponseContext
Parameters:
entity - entity object.
See Also:
MessageBodyWriter

setEntity

public void setEntity(Object entity,
                      Annotation[] annotations)
Set a new message message entity.

Parameters:
entity - entity object.
annotations - annotations attached to the entity.
See Also:
MessageBodyWriter

setEntity

public void setEntity(Object entity,
                      Type type,
                      Annotation[] annotations)
Set a new message message entity.

Parameters:
entity - entity object.
type - declared entity class.
annotations - annotations attached to the entity.
See Also:
MessageBodyWriter

setEntity

public void setEntity(Object entity,
                      Annotation[] annotations,
                      MediaType mediaType)
Description copied from interface: ContainerResponseContext
Set a new message entity, including the attached annotations and the media type.

It is the callers responsibility to wrap the actual entity with GenericEntity if preservation of its generic type is required.

Specified by:
setEntity in interface ContainerResponseContext
Parameters:
entity - entity object.
annotations - annotations attached to the entity instance.
mediaType - entity media type.
See Also:
ContainerResponseContext.setEntity(Object), MessageBodyWriter

setMediaType

public void setMediaType(MediaType mediaType)
Set the message content media type.

Parameters:
mediaType - message content media type.

getEntityClass

public Class<?> getEntityClass()
Description copied from interface: ContainerResponseContext
Get the raw entity type information.

Specified by:
getEntityClass in interface ContainerResponseContext
Returns:
raw entity type.

getEntityType

public Type getEntityType()
Description copied from interface: ContainerResponseContext
Get the generic entity type information.

Specified by:
getEntityType in interface ContainerResponseContext
Returns:
declared generic entity type.

setEntityType

public void setEntityType(Type type)
Set the message entity type information. This method overrides any computed or previously set entity type information.

Parameters:
type - overriding message entity type.

getEntityAnnotations

public Annotation[] getEntityAnnotations()
Description copied from interface: ContainerResponseContext
Get the annotations attached to the entity instance.

Note that the returned annotations array contains only those annotations explicitly attached to entity instance (such as the ones attached using Response.ResponseBuilder.entity(Object, java.lang.annotation.Annotation[]) method or the ones attached to the resource method that returned the response). The entity instance annotations array does not include annotations declared on the entity implementation class or its ancestors.

Specified by:
getEntityAnnotations in interface ContainerResponseContext
Returns:
annotations attached to the entity instance.

setEntityAnnotations

public void setEntityAnnotations(Annotation[] annotations)
Set the annotations attached to the entity.

Parameters:
annotations - entity annotations.

getEntityStream

public OutputStream getEntityStream()
Description copied from interface: ContainerResponseContext
Get the entity output stream. The JAX-RS runtime is responsible for closing the output stream.

Specified by:
getEntityStream in interface ContainerResponseContext
Returns:
entity output stream.

setEntityStream

public void setEntityStream(OutputStream outputStream)
Description copied from interface: ContainerResponseContext
Set a new entity output stream. The JAX-RS runtime is responsible for closing the output stream.

Specified by:
setEntityStream in interface ContainerResponseContext
Parameters:
outputStream - new entity output stream.

setStreamProvider

public void setStreamProvider(OutboundMessageContext.StreamProvider streamProvider)
Set the output stream provider callback.

This method must be called before first bytes are written to the entity stream.

Parameters:
streamProvider - non-null output stream provider.

enableBuffering

public void enableBuffering(Configuration configuration)
Enable a buffering of serialized entity. The buffering will be configured from configuration. The property determining the size of the buffer is CommonProperties.OUTBOUND_CONTENT_LENGTH_BUFFER.

The buffering functionality is by default disabled and could be enabled by calling this method. In this case this method must be called before first bytes are written to the entity stream.

Parameters:
configuration - runtime configuration.

commitStream

public void commitStream()
                  throws IOException
Commit the entity stream unless already committed.

Throws:
IOException - in case of the IO error.

isCommitted

public boolean isCommitted()
Returns true if the entity stream has been committed.

Returns:
true if the entity stream has been committed. Otherwise returns false.

close

public void close()
Closes the response. Flushes and closes the entity stream, frees up container resources associated with the corresponding request.


isChunked

public boolean isChunked()
Returns true if the response entity is a ChunkedOutput instance.

Returns:
true if the entity is a ChunkedOutput instance, false otherwise.


Copyright © 2007-2014, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.