Package org.restlet

Class Response

Direct Known Subclasses:
HttpResponse, WrapperResponse

public class Response extends Message
Generic response sent by server connectors. It is then received by client connectors. Responses are uniform across all types of connectors, protocols and components.
Author:
Jerome Louvel
See Also:
  • Constructor Details

    • Response

      public Response(Request request)
      Constructor.
      Parameters:
      request - The request associated to this response.
  • Method Details

    • getCurrent

      public static Response getCurrent()
      Returns the response associated to the current thread. Warning: this method should only be used under duress. You should by default prefer obtaining the current context using methods such as Resource.getResponse(). This variable is stored internally as a thread local variable and updated each time a call is handled by a Restlet via the Restlet.handle(org.restlet.Request, org.restlet.Response) method.
      Returns:
      The current context.
    • setCurrent

      public static void setCurrent(Response response)
      Sets the response associated with the current thread.
      Parameters:
      response - The thread's response.
    • abort

      public void abort()
      Ask the connector to abort the related network connection, for example immediately closing the socket.
    • commit

      public void commit()
      Asks the server connector to immediately commit the given response, making it ready to be sent back to the client. Note that all server connectors don't necessarily support this feature.

      When the response is in autoCommit mode (see related property), then calling this method isn't necessary. Also, be aware that committing the response doesn't necessarily means that is will be immediately be written on the network as some buffering can occurs. If you want to ensure that response buffers are flushed,

      Note that this calls back Request.commit(Response) on the parent request which holds the link with the underlying network connection.
    • flushBuffers

      public void flushBuffers() throws IOException
      Asks the server connector to immediately flush the network buffers. Note that this calls back Message.flushBuffers() on the parent request which holds the link with the underlying network connection.
      Overrides:
      flushBuffers in class Message
      Throws:
      IOException
    • getAccessControlAllowCredentials

      public Boolean getAccessControlAllowCredentials()
      When used as part of a response to a preflight CORS request, this indicates whether or not the actual request can be made using credentials.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Credentials" header.
      Returns:
      True if the requested resource allows credential.
    • getAccessControlAllowHeaders

      public Set<String> getAccessControlAllowHeaders()
      Returns the modifiable set of headers allowed by the actual request on the current resource when used as part of a response to a preflight CORS request.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Headers" header.
      Returns:
      The set of headers allowed by the actual request on the current resource.
    • getAccessControlAllowMethods

      public Set<Method> getAccessControlAllowMethods()
      Returns the modifiable set of methods allowed by the actual request on the current resource when used as part of a response to a preflight CORS request
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Methods" header.
      Returns:
      The set of methods allowed by the actual request on the current resource.
    • getAccessControlAllowOrigin

      public String getAccessControlAllowOrigin()
      When used in the context of CORS support, it returns the URI an origin server allows for the requested resource. Use "*" as a wildcard character.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Origin" header.
      Returns:
      The origin allowed by the requested resource.
    • getAccessControlExposeHeaders

      public Set<String> getAccessControlExposeHeaders()
      Returns a modifiable whitelist of headers an origin server allows for the requested resource.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Expose-Headers" header.
      Returns:
      The set of headers an origin server allows for the requested resource.
    • getAccessControlMaxAge

      public int getAccessControlMaxAge()
      Indicates how long the results of a preflight CORS request can be cached in a preflight result cache.
      In case of a negative value, the results of a preflight request is not meant to be cached.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Max-Age" header.
      Returns:
      Indicates how long the results of a preflight request can be cached in a preflight result cache.
    • getAge

      public int getAge()
      Returns the estimated amount of time since a response was generated or revalidated by the origin server. Origin servers should leave the 0 default value. Only caches are expected to set this property.

      Note that when used with HTTP connectors, this property maps to the "Age" header.
      Returns:
      The response age.
    • getAllowedMethods

      public Set<Method> getAllowedMethods()
      Returns the modifiable set of methods allowed on the requested resource. This property only has to be updated when a status CLIENT_ERROR_METHOD_NOT_ALLOWED is set. Creates a new instance if no one has been set.

      Note that when used with HTTP connectors, this property maps to the "Allow" header.
      Returns:
      The set of allowed methods.
    • getAuthenticationInfo

      public AuthenticationInfo getAuthenticationInfo()
      Returns information sent by an origin server related to an successful authentication attempt. If none is available, null is returned.

      Note that when used with HTTP connectors, this property maps to the "Authentication-Info" header.
      Returns:
      The authentication information provided by the server.
    • getChallengeRequests

      public List<ChallengeRequest> getChallengeRequests()
      Returns the list of authentication requests sent by an origin server to a client. If none is available, an empty list is returned.

      Note that when used with HTTP connectors, this property maps to the "WWW-Authenticate" header.
      Returns:
      The list of authentication requests.
    • getCookieSettings

      public Series<CookieSetting> getCookieSettings()
      Returns the modifiable series of cookie settings provided by the server. Creates a new instance if no one has been set.

      Note that when used with HTTP connectors, this property maps to the "Set-Cookie" and "Set-Cookie2" headers.
      Returns:
      The cookie settings provided by the server.
    • getDimensions

      public Set<Dimension> getDimensions()
      Returns the modifiable set of selecting dimensions on which the response entity may vary. If some server-side content negotiation is done, this set should be properly updated, other it can be left empty. Creates a new instance if no one has been set.

      Note that when used with HTTP connectors, this property maps to the "Vary" header.
      Returns:
      The set of dimensions on which the response entity may vary.
    • getLocationRef

      public Reference getLocationRef()
      Returns the location reference. This is the reference that the client should follow for redirections or resource creations.

      Note that when used with HTTP connectors, this property maps to the "Location" header.
      Returns:
      The redirection reference.
    • getProxyChallengeRequests

      public List<ChallengeRequest> getProxyChallengeRequests()
      Returns the list of authentication requests sent by an origin server to a client. If none is available, an empty list is returned.

      Note that when used with HTTP connectors, this property maps to the "Proxy-Authenticate" header.
      Returns:
      The list of authentication requests.
    • getRequest

      public Request getRequest()
      Returns the associated request
      Returns:
      The associated request
    • getRetryAfter

      public Date getRetryAfter()
      Indicates how long the service is expected to be unavailable to the requesting client. Default value is null.

      Note that when used with HTTP connectors, this property maps to the "Retry-After" header.
      Returns:
      Date after with a retry attempt could occur.
    • getServerInfo

      public ServerInfo getServerInfo()
      Returns the server-specific information. Creates a new instance if no one has been set.
      Returns:
      The server-specific information.
    • getStatus

      public Status getStatus()
      Returns the status.
      Returns:
      The status.
    • isAutoCommitting

      public boolean isAutoCommitting()
      Indicates if the response should be automatically committed. When processing a request on the server-side, setting this property to 'false' let you ask to the server connector to wait before sending the response back to the client when the initial calling thread returns. This will let you do further updates to the response and manually calling commit() later on, using another thread.
      Returns:
      True if the response should be automatically committed.
    • isCommitted

      public boolean isCommitted()
      Indicates if the response has already been committed.
      Returns:
      True if the response has already been committed.
    • isConfidential

      public boolean isConfidential()
      Description copied from class: Message
      Indicates if the message was or will be exchanged confidentially, for example via a SSL-secured connection.
      Specified by:
      isConfidential in class Message
      Returns:
      True if the message is confidential.
    • isFinal

      public boolean isFinal()
      Indicates if the response is final or provisional. It relies on the Status.isInformational() method.
      Returns:
      True if the response is final.
    • isProvisional

      public boolean isProvisional()
      Indicates if the response is provisional or final. It relies on the Status.isInformational() method.
      Returns:
      True if the response is provisional.
    • redirectPermanent

      public void redirectPermanent(Reference targetRef)
      Permanently redirects the client to a target URI. The client is expected to reuse the same method for the new request.
      Parameters:
      targetRef - The target URI reference.
    • redirectPermanent

      public void redirectPermanent(String targetUri)
      Permanently redirects the client to a target URI. The client is expected to reuse the same method for the new request.

      If you pass a relative target URI, it will be resolved with the current base reference of the request's resource reference (see Request.getResourceRef() and Reference.getBaseRef().
      Parameters:
      targetUri - The target URI.
    • redirectSeeOther

      public void redirectSeeOther(Reference targetRef)
      Redirects the client to a different URI that SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource.
      Parameters:
      targetRef - The target reference.
    • redirectSeeOther

      public void redirectSeeOther(String targetUri)
      Redirects the client to a different URI that SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource.

      If you pass a relative target URI, it will be resolved with the current base reference of the request's resource reference (see Request.getResourceRef() and Reference.getBaseRef().
      Parameters:
      targetUri - The target URI.
    • redirectTemporary

      public void redirectTemporary(Reference targetRef)
      Temporarily redirects the client to a target URI. The client is expected to reuse the same method for the new request.
      Parameters:
      targetRef - The target reference.
    • redirectTemporary

      public void redirectTemporary(String targetUri)
      Temporarily redirects the client to a target URI. The client is expected to reuse the same method for the new request.

      If you pass a relative target URI, it will be resolved with the current base reference of the request's resource reference (see Request.getResourceRef() and Reference.getBaseRef().
      Parameters:
      targetUri - The target URI.
    • setAccessControlAllowCredentials

      public void setAccessControlAllowCredentials(Boolean accessControlAllowCredentials)
      When used as part of a response to a preflight CORS request, indicates whether or not the actual request can be made using credentials.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Credentials" header.
      Parameters:
      accessControlAllowCredentials - True if the requested resource allows credential.
    • setAccessControlMaxAge

      public void setAccessControlMaxAge(int accessControlMaxAge)
      When used as part of a response to a preflight CORS request, indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Max-Age" header.
      In case of negative value, the header is not set.
      Parameters:
      accessControlMaxAge - How long the results of a preflight request can be cached in a preflight result cache.
    • setAccessControlAllowHeaders

      public void setAccessControlAllowHeaders(Set<String> accessControlAllowHeaders)
      Sets the set of headers allowed by the actual request on the current resource when used as part of a response to a preflight CORS request.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Headers" header.
      Parameters:
      accessControlAllowHeaders - The set of headers allowed by the actual request on the current resource.
    • setAccessControlAllowMethods

      public void setAccessControlAllowMethods(Set<Method> accessControlAllowMethods)
      Sets the set of methods allowed by the actual request on the current resource when used as part of a response to a preflight CORS request.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Methods" header.
      Parameters:
      accessControlAllowMethods - The set of methods allowed by the actual request on the current resource.
    • setAccessControlAllowOrigin

      public void setAccessControlAllowOrigin(String accessControlAllowOrigin)
      When used in the context of CORS support, it sets the URI an origin server allows for the requested resource. Use "*" as a wildcard character.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Origin" header.
      Parameters:
      accessControlAllowOrigin - The origin allowed by the requested resource.
    • setAccessControlExposeHeaders

      public void setAccessControlExposeHeaders(Set<String> accessControlExposeHeaders)
      Sets the list of headers an origin server allows for the requested resource.
      Note that when used with HTTP connectors, this property maps to the "Access-Control-Expose-Headers" header.
      Parameters:
      accessControlExposeHeaders - The set of headers an origin server allows for the requested resource.
    • setAge

      public void setAge(int age)
      Sets the estimated amount of time since a response was generated or revalidated by the origin server. Origin servers should leave the 0 default value. Only caches are expected to set this property.

      Note that when used with HTTP connectors, this property maps to the "Age" header.
      Parameters:
      age - The response age.
    • setAllowedMethods

      public void setAllowedMethods(Set<Method> allowedMethods)
      Sets the set of methods allowed on the requested resource. The set instance set must be thread-safe (use CopyOnWriteArraySet for example.

      Note that when used with HTTP connectors, this property maps to the "Allow" header.
      Parameters:
      allowedMethods - The set of methods allowed on the requested resource.
    • setAuthenticationInfo

      public void setAuthenticationInfo(AuthenticationInfo authenticationInfo)
      Sets the authentication information sent by an origin server to a client after a successful authentication attempt.

      Note that when used with HTTP connectors, this property maps to the "Authentication-Info" header.
      Parameters:
      authenticationInfo - The data returned by the server in response to successful authentication.
    • setAutoCommitting

      public void setAutoCommitting(boolean autoCommitting)
      Indicates if the response should be automatically committed.
      Parameters:
      autoCommitting - True if the response should be automatically committed
    • setChallengeRequests

      public void setChallengeRequests(List<ChallengeRequest> challengeRequests)
      Sets the list of authentication requests sent by an origin server to a client. Note that when used with HTTP connectors, this property maps to the "WWW-Authenticate" header. This method clears the current list and adds all entries in the parameter list.
      Parameters:
      challengeRequests - A list of authentication requests sent by an origin server to a client.
    • setCommitted

      public void setCommitted(boolean committed)
      Indicates if the response has already been committed.
      Parameters:
      committed - True if the response has already been committed.
    • setCookieSettings

      public void setCookieSettings(Series<CookieSetting> cookieSettings)
      Sets the modifiable series of cookie settings provided by the server. Note that when used with HTTP connectors, this property maps to the "Set-Cookie" and "Set-Cookie2" headers. This method clears the current series and adds all entries in the parameter series.
      Parameters:
      cookieSettings - A series of cookie settings provided by the server.
    • setDimensions

      public void setDimensions(Set<Dimension> dimensions)
      Sets the set of dimensions on which the response entity may vary. Note that when used with HTTP connectors, this property maps to the "Vary" header. This method clears the current set and adds all entries in the parameter set.
      Parameters:
      dimensions - The set of dimensions on which the response entity may vary.
    • setLocationRef

      public void setLocationRef(Reference locationRef)
      Sets the reference that the client should follow for redirections or resource creations. Note that when used with HTTP connectors, this property maps to the "Location" header.
      Parameters:
      locationRef - The reference to set.
    • setLocationRef

      public void setLocationRef(String locationUri)
      Sets the reference that the client should follow for redirections or resource creations. If you pass a relative location URI, it will be resolved with the current base reference of the request's resource reference (see Request.getResourceRef() and Reference.getBaseRef().

      Note that when used with HTTP connectors, this property maps to the "Location" header.
      Parameters:
      locationUri - The URI to set.
      See Also:
    • setProxyChallengeRequests

      public void setProxyChallengeRequests(List<ChallengeRequest> proxyChallengeRequests)
      Sets the modifiable list of authentication requests sent by a proxy to a client. The list instance set must be thread-safe (use CopyOnWriteArrayList for example. Note that when used with HTTP connectors, this property maps to the "Proxy-Authenticate" header. This method clears the current list and adds all entries in the parameter list.
      Parameters:
      proxyChallengeRequests - A list of authentication requests sent by a proxy to a client.
    • setRequest

      public void setRequest(Request request)
      Sets the associated request.
      Parameters:
      request - The associated request
    • setRetryAfter

      public void setRetryAfter(Date retryAfter)
      Indicates how long the service is expected to be unavailable to the requesting client. Default value is null.

      Note that when used with HTTP connectors, this property maps to the "Retry-After" header.
      Parameters:
      retryAfter - Date after with a retry attempt could occur.
    • setServerInfo

      public void setServerInfo(ServerInfo serverInfo)
      Sets the server-specific information.
      Parameters:
      serverInfo - The server-specific information.
    • setStatus

      public void setStatus(Status status)
      Sets the status.
      Parameters:
      status - The status to set.
    • setStatus

      public void setStatus(Status status, String description)
      Sets the status.
      Parameters:
      status - The status to set (code and reason phrase).
      description - The longer status description.
    • setStatus

      public void setStatus(Status status, Throwable throwable)
      Sets the status.
      Parameters:
      status - The status to set.
      throwable - The related error or exception.
    • setStatus

      public void setStatus(Status status, Throwable throwable, String message)
      Sets the status.
      Parameters:
      status - The status to set.
      throwable - The related error or exception.
      message - The status message.
    • toString

      public String toString()
      Displays a synthesis of the response like an HTTP status line.
      Overrides:
      toString in class Object
      Returns:
      A synthesis of the response like an HTTP status line.