org.glassfish.jersey.client
Class ClientRequest

java.lang.Object
  extended by org.glassfish.jersey.message.internal.OutboundMessageContext
      extended by org.glassfish.jersey.client.ClientRequest
All Implemented Interfaces:
ClientRequestContext, ServiceLocatorSupplier

public class ClientRequest
extends OutboundMessageContext
implements ClientRequestContext, ServiceLocatorSupplier

Jersey client request context.

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

Nested Class Summary
 
Nested classes/interfaces inherited from class org.glassfish.jersey.message.internal.OutboundMessageContext
OutboundMessageContext.StreamProvider
 
Constructor Summary
  ClientRequest(ClientRequest original)
          Copy constructor.
protected ClientRequest(URI requestUri, ClientConfig clientConfig, PropertiesDelegate propertiesDelegate)
          Create new Jersey client request context.
 
Method Summary
 void abortWith(Response response)
          Abort the filter chain with a response.
 void accept(MediaType... types)
          Add new accepted types to the message headers.
 void accept(String... types)
          Add new accepted types to the message headers.
 void acceptLanguage(Locale... locales)
          Add new accepted languages to the message headers.
 void acceptLanguage(String... locales)
          Add new accepted languages to the message headers.
 void cacheControl(CacheControl cacheControl)
          Add new cache control entry to the message headers.
 void cookie(Cookie cookie)
          Add new cookie to the message headers.
 void enableBuffering()
          Enable a buffering of serialized entity.
 void encoding(String encoding)
          Set message encoding.
 Response getAbortResponse()
          Get the request filter chain aborting response if set, or null otherwise.
 JerseyClient getClient()
          Get the client instance associated with the request.
 Configuration getConfiguration()
          Get the immutable configuration of the request.
 Map<String,Cookie> getCookies()
          Get any cookies that accompanied the request.
 String getMethod()
          Get the request method.
 Object getProperty(String name)
          Returns the property with the given name registered in the current request/response exchange context, or null if there is no property by that name.
 Collection<String> getPropertyNames()
          Returns an immutable collection containing the property names available within the context of the current request/response exchange context.
 Iterable<ReaderInterceptor> getReaderInterceptors()
          Get reader interceptors of this request.
 org.glassfish.hk2.api.ServiceLocator getServiceLocator()
          Get service locator.
 URI getUri()
          Get the request URI.
 MessageBodyWorkers getWorkers()
          Get the message body workers associated with the request.
 Iterable<WriterInterceptor> getWriterInterceptors()
          Get writer interceptors of this request.
 boolean isAsynchronous()
          Returns true if the request is called asynchronously using AsyncInvoker
 void language(Locale language)
          Set message language.
 void language(String language)
          Set message language.
 void removeProperty(String name)
          Removes a property with the given name from the current request/response exchange context.
<T> T
resolveProperty(String name, Class<T> type)
          Resolve a property value for the specified property name.
<T> T
resolveProperty(String name, T defaultValue)
          Resolve a property value for the specified property name.
 void setMethod(String method)
          Set the request method.
 void setProperty(String name, Object object)
          Binds an object to a given property name in the current request/response exchange context.
 void setUri(URI uri)
          Set a new request URI.
 void setWorkers(MessageBodyWorkers workers)
          Set the message body workers associated with the request.
 void type(MediaType type)
          Set message content type.
 void type(String type)
          Set message content type.
 void variant(Variant variant)
          Set message content variant (type, language and encoding).
 void writeEntity()
          Write (serialize) the entity set in this request into the entity stream.
 
Methods inherited from class org.glassfish.jersey.message.internal.OutboundMessageContext
close, commitStream, enableBuffering, getAcceptableLanguages, getAcceptableMediaTypes, getAllowedMethods, getDate, getEntity, getEntityAnnotations, getEntityClass, getEntityStream, getEntityTag, getEntityType, getHeaders, getHeaderString, getLanguage, getLastModified, getLength, getLink, getLinkBuilder, getLinks, getLocation, getMediaType, getRequestCookies, getResponseCookies, getStringHeaders, hasEntity, hasLink, isCommitted, replaceHeaders, setEntity, setEntity, setEntity, setEntity, setEntityAnnotations, setEntityStream, setEntityType, setMediaType, setStreamProvider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.ws.rs.client.ClientRequestContext
getAcceptableLanguages, getAcceptableMediaTypes, getDate, getEntity, getEntityAnnotations, getEntityClass, getEntityStream, getEntityType, getHeaders, getHeaderString, getLanguage, getMediaType, getStringHeaders, hasEntity, setEntity, setEntity, setEntityStream
 

Constructor Detail

ClientRequest

protected ClientRequest(URI requestUri,
                        ClientConfig clientConfig,
                        PropertiesDelegate propertiesDelegate)
Create new Jersey client request context.

Parameters:
requestUri - request Uri.
clientConfig - request configuration.
propertiesDelegate - properties delegate.

ClientRequest

public ClientRequest(ClientRequest original)
Copy constructor.

Parameters:
original - original instance.
Method Detail

resolveProperty

public <T> T resolveProperty(String name,
                             Class<T> type)
Resolve a property value for the specified property name.

The method returns the value of the property registered in the request-specific property bag, if available. If no property for the given property name is found in the request-specific property bag, the method looks at the properties stored in the global client-runtime configuration this request belongs to. If there is a value defined in the client-runtime configuration, it is returned, otherwise the method returns null if no such property is registered neither in the client runtime nor in the request-specific property bag.

Type Parameters:
T - property Java type.
Parameters:
name - property name.
type - expected property class type.
Returns:
resolved property value or null if no such property is registered.

resolveProperty

public <T> T resolveProperty(String name,
                             T defaultValue)
Resolve a property value for the specified property name.

The method returns the value of the property registered in the request-specific property bag, if available. If no property for the given property name is found in the request-specific property bag, the method looks at the properties stored in the global client-runtime configuration this request belongs to. If there is a value defined in the client-runtime configuration, it is returned, otherwise the method returns defaultValue if no such property is registered neither in the client runtime nor in the request-specific property bag.

Type Parameters:
T - property Java type.
Parameters:
name - property name.
defaultValue - default value to return if the property is not registered.
Returns:
resolved property value or defaultValue if no such property is registered.

getProperty

public Object getProperty(String name)
Description copied from interface: ClientRequestContext
Returns the property with the given name registered in the current request/response exchange context, or null if there is no property by that name.

A property allows a JAX-RS filters and interceptors to exchange additional custom information not already provided by this interface.

A list of supported properties can be retrieved using ClientRequestContext.getPropertyNames(). Custom property names should follow the same convention as package names.

Specified by:
getProperty in interface ClientRequestContext
Parameters:
name - a String specifying the name of the property.
Returns:
an Object containing the value of the property, or null if no property exists matching the given name.
See Also:
ClientRequestContext.getPropertyNames()

getPropertyNames

public Collection<String> getPropertyNames()
Description copied from interface: ClientRequestContext
Returns an immutable collection containing the property names available within the context of the current request/response exchange context.

Use the ClientRequestContext.getProperty(java.lang.String) method with a property name to get the value of a property.

Specified by:
getPropertyNames in interface ClientRequestContext
Returns:
an immutable collection of property names.
See Also:
ClientRequestContext.getProperty(java.lang.String)

setProperty

public void setProperty(String name,
                        Object object)
Description copied from interface: ClientRequestContext
Binds an object to a given property name in the current request/response exchange context. If the name specified is already used for a property, this method will replace the value of the property with the new value.

A property allows a JAX-RS filters and interceptors to exchange additional custom information not already provided by this interface.

A list of supported properties can be retrieved using ClientRequestContext.getPropertyNames(). Custom property names should follow the same convention as package names.

If a null value is passed, the effect is the same as calling the ClientRequestContext.removeProperty(String) method.

Specified by:
setProperty in interface ClientRequestContext
Parameters:
name - a String specifying the name of the property.
object - an Object representing the property to be bound.

removeProperty

public void removeProperty(String name)
Description copied from interface: ClientRequestContext
Removes a property with the given name from the current request/response exchange context. After removal, subsequent calls to ClientRequestContext.getProperty(java.lang.String) to retrieve the property value will return null.

Specified by:
removeProperty in interface ClientRequestContext
Parameters:
name - a String specifying the name of the property to be removed.

getUri

public URI getUri()
Description copied from interface: ClientRequestContext
Get the request URI.

Specified by:
getUri in interface ClientRequestContext
Returns:
request URI.

setUri

public void setUri(URI uri)
Description copied from interface: ClientRequestContext
Set a new request URI.

Specified by:
setUri in interface ClientRequestContext
Parameters:
uri - new request URI.

getMethod

public String getMethod()
Description copied from interface: ClientRequestContext
Get the request method.

Specified by:
getMethod in interface ClientRequestContext
Returns:
the request method.
See Also:
HttpMethod

setMethod

public void setMethod(String method)
Description copied from interface: ClientRequestContext
Set the request method.

Specified by:
setMethod in interface ClientRequestContext
Parameters:
method - new request method.
See Also:
HttpMethod

getClient

public JerseyClient getClient()
Description copied from interface: ClientRequestContext
Get the client instance associated with the request.

Specified by:
getClient in interface ClientRequestContext
Returns:
client instance associated with the request.

abortWith

public void abortWith(Response response)
Description copied from interface: ClientRequestContext
Abort the filter chain with a response. This method breaks the filter chain processing and returns the provided response back to the client. The provided response goes through the chain of applicable response filters.

Specified by:
abortWith in interface ClientRequestContext
Parameters:
response - response to be sent back to the client.

getAbortResponse

public Response getAbortResponse()
Get the request filter chain aborting response if set, or null otherwise.

Returns:
request filter chain aborting response if set, or null otherwise.

getConfiguration

public Configuration getConfiguration()
Description copied from interface: ClientRequestContext
Get the immutable configuration of the request.

Specified by:
getConfiguration in interface ClientRequestContext
Returns:
immutable request configuration.

getCookies

public Map<String,Cookie> getCookies()
Description copied from interface: ClientRequestContext
Get any cookies that accompanied the request.

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

getWorkers

public MessageBodyWorkers getWorkers()
Get the message body workers associated with the request.

Returns:
message body workers.

setWorkers

public void setWorkers(MessageBodyWorkers workers)
Set the message body workers associated with the request.

Parameters:
workers - message body workers.

accept

public void accept(MediaType... types)
Add new accepted types to the message headers.

Parameters:
types - accepted types to be added.

accept

public void accept(String... types)
Add new accepted types to the message headers.

Parameters:
types - accepted types to be added.

acceptLanguage

public void acceptLanguage(Locale... locales)
Add new accepted languages to the message headers.

Parameters:
locales - accepted languages to be added.

acceptLanguage

public void acceptLanguage(String... locales)
Add new accepted languages to the message headers.

Parameters:
locales - accepted languages to be added.

cookie

public void cookie(Cookie cookie)
Add new cookie to the message headers.

Parameters:
cookie - cookie to be added.

cacheControl

public void cacheControl(CacheControl cacheControl)
Add new cache control entry to the message headers.

Parameters:
cacheControl - cache control entry to be added.

encoding

public void encoding(String encoding)
Set message encoding.

Parameters:
encoding - message encoding to be set.

language

public void language(String language)
Set message language.

Parameters:
language - message language to be set.

language

public void language(Locale language)
Set message language.

Parameters:
language - message language to be set.

type

public void type(MediaType type)
Set message content type.

Parameters:
type - message content type to be set.

type

public void type(String type)
Set message content type.

Parameters:
type - message content type to be set.

variant

public void variant(Variant variant)
Set message content variant (type, language and encoding).

Parameters:
variant - message content content variant (type, language and encoding) to be set.

isAsynchronous

public boolean isAsynchronous()
Returns true if the request is called asynchronously using AsyncInvoker

Returns:
True if the request is asynchronous; false otherwise.

enableBuffering

public void enableBuffering()
Enable a buffering of serialized entity. The buffering will be configured from runtime configuration associated with this request. 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.


writeEntity

public void writeEntity()
                 throws IOException
Write (serialize) the entity set in this request into the entity stream. The method use writer interceptors and message body writer.

This method modifies the state of this request and therefore it can be called only once per request life cycle otherwise IllegalStateException is thrown.

Note that OutboundMessageContext.setStreamProvider(org.glassfish.jersey.message.internal.OutboundMessageContext.StreamProvider) and optionally enableBuffering() must be called before calling this method.

Throws:
IOException - In the case of IO error.

getWriterInterceptors

public Iterable<WriterInterceptor> getWriterInterceptors()
Get writer interceptors of this request.

Returns:
Writer interceptors in the interceptor execution order.

getReaderInterceptors

public Iterable<ReaderInterceptor> getReaderInterceptors()
Get reader interceptors of this request.

Returns:
Reader interceptors in the interceptor execution order.

getServiceLocator

public org.glassfish.hk2.api.ServiceLocator getServiceLocator()
Description copied from interface: ServiceLocatorSupplier
Get service locator.

Specified by:
getServiceLocator in interface ServiceLocatorSupplier
Returns:
Service locator;


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