Class ContainerResponse
- java.lang.Object
-
- org.glassfish.jersey.server.ContainerResponse
-
- All Implemented Interfaces:
ContainerResponseContext
public class ContainerResponse extends Object implements ContainerResponseContext
Jersey container response context.- Author:
- Marek Potociar
-
-
Constructor Summary
Constructors Constructor Description ContainerResponse(ContainerRequest requestContext, Response response)Create a new Jersey container response context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the response.voidcommitStream()Commit theentity streamunless already committed.voidenableBuffering(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.DategetDate()Get message date.ObjectgetEntity()Get the message entity Java instance.Annotation[]getEntityAnnotations()Get the annotations attached to the entity instance.Class<?>getEntityClass()Get the raw entity type information.OutputStreamgetEntityStream()Get the entity output stream.EntityTaggetEntityTag()Get the entity tag.TypegetEntityType()Get the generic entity type information.MultivaluedMap<String,Object>getHeaders()Get the mutable response headers multivalued map.StringgetHeaderString(String name)Get a message header as a single string value.LocalegetLanguage()Get the language of the entity.DategetLastModified()Get the last modified date.intgetLength()Get Content-Length value.LinkgetLink(String relation)Get the link for the relation.Link.BuildergetLinkBuilder(String relation)Convenience method that returns aLink.Builderfor the relation.Set<Link>getLinks()Get the links attached to the message as header.URIgetLocation()Get the location.MediaTypegetMediaType()Get the media type of the entity.ContainerRequestgetRequestContext()Get the associated container request context paired with this response context.intgetStatus()Get the status code associated with the response.Response.StatusTypegetStatusInfo()Get the complete status information associated with the response.MultivaluedMap<String,String>getStringHeaders()Get a string view of header values associated with the message.OutboundMessageContextgetWrappedMessageContext()Get the wrapped response message context.booleanhasEntity()Check if there is an entity available in the response.booleanhasLink(String relation)Check if link for relation exists.booleanisChunked()Returnstrueif the response entity is aChunkedOutputinstance.booleanisCommitted()Returnstrueif the entity stream has been committed.booleanisMappedFromException()Returns true if the response is result of the exception (for example created duringexception mapping).voidsetEntity(Object entity)Set a new message message entity.voidsetEntity(Object entity, Annotation[] annotations)Set a new message message entity.voidsetEntity(Object entity, Annotation[] annotations, MediaType mediaType)Set a new message entity, including the attached annotations and the media type.voidsetEntity(Object entity, Type type, Annotation[] annotations)Set a new message message entity.voidsetEntityAnnotations(Annotation[] annotations)Set the annotations attached to the entity.voidsetEntityStream(OutputStream outputStream)Set a new entity output stream.voidsetEntityType(Type type)Set the message entity type information.voidsetMappedFromException(boolean mappedFromException)Sets the flag indicating whether the response was created based on the exception.voidsetMediaType(MediaType mediaType)Set the message content media type.voidsetStatus(int code)Set a new response status code.voidsetStatusInfo(Response.StatusType status)Set the complete status information (status code and reason phrase) associated with the response.voidsetStreamProvider(OutboundMessageContext.StreamProvider streamProvider)Set the output stream provider callback.
-
-
-
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 duringexception 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 ofexception mapping).
-
getStatus
public int getStatus()
Description copied from interface:ContainerResponseContextGet the status code associated with the response.- Specified by:
getStatusin interfaceContainerResponseContext- Returns:
- the response status code or -1 if the status was not set.
-
setStatus
public void setStatus(int code)
Description copied from interface:ContainerResponseContextSet a new response status code.- Specified by:
setStatusin interfaceContainerResponseContext- Parameters:
code- new status code.
-
setStatusInfo
public void setStatusInfo(Response.StatusType status)
Description copied from interface:ContainerResponseContextSet the complete status information (status code and reason phrase) associated with the response.- Specified by:
setStatusInfoin interfaceContainerResponseContext- Parameters:
status- the response status information.
-
getStatusInfo
public Response.StatusType getStatusInfo()
Description copied from interface:ContainerResponseContextGet the complete status information associated with the response.- Specified by:
getStatusInfoin interfaceContainerResponseContext- Returns:
- the response status information or
nullif 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:ContainerResponseContextGet any new cookies set on the response message.- Specified by:
getCookiesin interfaceContainerResponseContext- 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:ContainerResponseContextGet a message header as a single string value. Each single header value is converted to String using aRuntimeDelegate.HeaderDelegateif one is available viaRuntimeDelegate.createHeaderDelegate(java.lang.Class)for the header value class or using itstoStringmethod if a header delegate is not available.- Specified by:
getHeaderStringin interfaceContainerResponseContext- Parameters:
name- the message header.- Returns:
- the message header value. If the message header is not present then
nullis 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:ContainerResponseContextGet the mutable response headers multivalued map.- Specified by:
getHeadersin interfaceContainerResponseContext- Returns:
- mutable multivalued map of response headers.
- See Also:
ContainerResponseContext.getStringHeaders(),ContainerResponseContext.getHeaderString(String)
-
getStringHeaders
public MultivaluedMap<String,String> getStringHeaders()
Description copied from interface:ContainerResponseContextGet a string view of header values associated with the message. Changes in the underlyingheaders mapare reflected in this view.The method converts the non-string header values to strings using a
RuntimeDelegate.HeaderDelegateif one is available viaRuntimeDelegate.createHeaderDelegate(java.lang.Class)for the class of the value or using the valuestoStringmethod if a header delegate is not available.- Specified by:
getStringHeadersin interfaceContainerResponseContext- 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:ContainerResponseContextGet message date.- Specified by:
getDatein interfaceContainerResponseContext- Returns:
- the message date, otherwise
nullif not present.
-
getLanguage
public Locale getLanguage()
Description copied from interface:ContainerResponseContextGet the language of the entity.- Specified by:
getLanguagein interfaceContainerResponseContext- Returns:
- the language of the entity or
nullif not specified
-
getMediaType
public MediaType getMediaType()
Description copied from interface:ContainerResponseContextGet the media type of the entity.- Specified by:
getMediaTypein interfaceContainerResponseContext- Returns:
- the media type or
nullif not specified (e.g. there's no response entity).
-
getAllowedMethods
public Set<String> getAllowedMethods()
Description copied from interface:ContainerResponseContextGet the allowed HTTP methods from the Allow HTTP header.- Specified by:
getAllowedMethodsin interfaceContainerResponseContext- Returns:
- the allowed HTTP methods, all methods will returned as upper case strings.
-
getLength
public int getLength()
Description copied from interface:ContainerResponseContextGet Content-Length value.- Specified by:
getLengthin interfaceContainerResponseContext- Returns:
- Content-Length as integer if present and valid number. In other cases returns -1.
-
getEntityTag
public EntityTag getEntityTag()
Description copied from interface:ContainerResponseContextGet the entity tag.- Specified by:
getEntityTagin interfaceContainerResponseContext- Returns:
- the entity tag, otherwise
nullif not present.
-
getLastModified
public Date getLastModified()
Description copied from interface:ContainerResponseContextGet the last modified date.- Specified by:
getLastModifiedin interfaceContainerResponseContext- Returns:
- the last modified date, otherwise
nullif not present.
-
getLocation
public URI getLocation()
Description copied from interface:ContainerResponseContextGet the location.- Specified by:
getLocationin interfaceContainerResponseContext- Returns:
- the location URI, otherwise
nullif not present.
-
getLinks
public Set<Link> getLinks()
Description copied from interface:ContainerResponseContextGet the links attached to the message as header.- Specified by:
getLinksin interfaceContainerResponseContext- Returns:
- links, may return empty
Setif no links are present. Never returnsnull.
-
hasLink
public boolean hasLink(String relation)
Description copied from interface:ContainerResponseContextCheck if link for relation exists.- Specified by:
hasLinkin interfaceContainerResponseContext- Parameters:
relation- link relation.- Returns:
trueif the for the relation link exists,falseotherwise.
-
getLink
public Link getLink(String relation)
Description copied from interface:ContainerResponseContextGet the link for the relation.- Specified by:
getLinkin interfaceContainerResponseContext- Parameters:
relation- link relation.- Returns:
- the link for the relation, otherwise
nullif not present.
-
getLinkBuilder
public Link.Builder getLinkBuilder(String relation)
Description copied from interface:ContainerResponseContextConvenience method that returns aLink.Builderfor the relation.- Specified by:
getLinkBuilderin interfaceContainerResponseContext- Parameters:
relation- link relation.- Returns:
- the link builder for the relation, otherwise
nullif not present.
-
hasEntity
public boolean hasEntity()
Description copied from interface:ContainerResponseContextCheck if there is an entity available in the response. The method returnstrueif the entity is present, returnsfalseotherwise.- Specified by:
hasEntityin interfaceContainerResponseContext- Returns:
trueif there is an entity present in the message,falseotherwise.
-
getEntity
public Object getEntity()
Description copied from interface:ContainerResponseContextGet the message entity Java instance. Returnsnullif the message does not contain an entity.- Specified by:
getEntityin interfaceContainerResponseContext- Returns:
- the message entity or
nullif message does not contain an entity body.
-
setEntity
public void setEntity(Object entity)
Set a new message message entity.- Specified by:
setEntityin interfaceContainerResponseContext- 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:ContainerResponseContextSet a new message entity, including the attached annotations and the media type.It is the callers responsibility to wrap the actual entity with
GenericEntityif preservation of its generic type is required.- Specified by:
setEntityin interfaceContainerResponseContext- 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:ContainerResponseContextGet the raw entity type information.- Specified by:
getEntityClassin interfaceContainerResponseContext- Returns:
- raw entity type.
-
getEntityType
public Type getEntityType()
Description copied from interface:ContainerResponseContextGet the generic entity type information.- Specified by:
getEntityTypein interfaceContainerResponseContext- 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:ContainerResponseContextGet 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 as well as the ones attached to the resource method that has returned the response). The entity instance annotations array does not include annotations declared on the entity implementation class or its ancestors.Note that container response filters invoked earlier in the filter chain may modify the entity annotations value, in which case this getter method would return the last annotations value set by a container response filter invoked earlier in the filter chain.
For example:
@Path("my-resource") public class MyResource { private final Annotations[] extras = ... ; @GET @Custom public String getAnnotatedMe() { return Response.ok().entity("Annotated me", extras).build(); } ... }The container response context for a response returned from the
getMe()method above would contain all the annotations declared on thegetAnnotatedMe()method (@GET,@Custom) as well as all the annotations from theextrasfield, provided this value has not been replaced by any container response filter invoked earlier.Similarly:
@Custom public class AnnotatedMe { ... } @Path("my-resource") public class MyResource { private final Annotations[] extras = ... ; @GET public AnnotatedMe getMe() { return Response.ok().entity(new AnnotatedMe(), extras).build(); } ... }Provided that the value has not been replaced by any container response filter invoked earlier, the container response context for a response returned from the
getMe()method above would contain all the annotations on thegetMe()method (@GET) as well as all the annotations from theextrasfield. It would however not contain any annotations declared on theAnnotatedMeclass.- Specified by:
getEntityAnnotationsin interfaceContainerResponseContext- 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:ContainerResponseContextGet the entity output stream. The JAX-RS runtime is responsible for closing the output stream.- Specified by:
getEntityStreamin interfaceContainerResponseContext- Returns:
- entity output stream.
-
setEntityStream
public void setEntityStream(OutputStream outputStream)
Description copied from interface:ContainerResponseContextSet a new entity output stream. The JAX-RS runtime is responsible for closing the output stream.- Specified by:
setEntityStreamin interfaceContainerResponseContext- 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 theentity stream.- Parameters:
streamProvider- non-nulloutput 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 isCommonProperties.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 theentity stream.- Parameters:
configuration- runtime configuration.
-
commitStream
public void commitStream() throws IOExceptionCommit theentity streamunless already committed.- Throws:
IOException- in case of the IO error.
-
isCommitted
public boolean isCommitted()
Returnstrueif the entity stream has been committed.- Returns:
trueif the entity stream has been committed. Otherwise returnsfalse.
-
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()
Returnstrueif the response entity is aChunkedOutputinstance.- Returns:
trueif the entity is aChunkedOutputinstance,falseotherwise.
-
-