Class HttpUrlConnectorProvider
- java.lang.Object
-
- org.glassfish.jersey.client.HttpUrlConnectorProvider
-
- All Implemented Interfaces:
ConnectorProvider
public class HttpUrlConnectorProvider extends java.lang.Object implements ConnectorProvider
Default Jersey clientconnector
provider that provides connector instances which delegate HTTP requests toHttpURLConnection
for processing.The provided connector instances override default behaviour of the property
ClientProperties.REQUEST_ENTITY_PROCESSING
and useRequestEntityProcessing.BUFFERED
request entity processing by default.Due to a bug in the chunked transport coding support of
HttpURLConnection
that causes requests to fail unpredictably, this connector provider allows to configure the provided connector instances to useHttpURLConnection
's fixed-length streaming mode as a workaround. This workaround can be enabled viauseFixedLengthStreaming()
method or viaUSE_FIXED_LENGTH_STREAMING
Jersey client configuration property.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
HttpUrlConnectorProvider.ConnectionFactory
A factory forHttpURLConnection
instances.private static class
HttpUrlConnectorProvider.DefaultConnectionFactory
-
Field Summary
Fields Modifier and Type Field Description private int
chunkSize
private HttpUrlConnectorProvider.ConnectionFactory
connectionFactory
private static HttpUrlConnectorProvider.ConnectionFactory
DEFAULT_CONNECTION_FACTORY
Default connection factory to be used.private static java.util.logging.Logger
LOGGER
static java.lang.String
SET_METHOD_WORKAROUND
A value oftrue
declares that the client will try to set unsupported HTTP method toHttpURLConnection
via reflection.static java.lang.String
USE_FIXED_LENGTH_STREAMING
Iftrue
, theHttpUrlConnector
(if used) will assume the content length from the value of "Content-Length" request header (if present).private boolean
useFixedLengthStreaming
private boolean
useSetMethodWorkaround
-
Constructor Summary
Constructors Constructor Description HttpUrlConnectorProvider()
Create newHttpURLConnection
-based Jersey client connector provider.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpUrlConnectorProvider
chunkSize(int chunkSize)
Set chunk size for requests transferred using a HTTP chunked transfer coding.HttpUrlConnectorProvider
connectionFactory(HttpUrlConnectorProvider.ConnectionFactory connectionFactory)
Set a customHttpURLConnection
factory.protected Connector
createHttpUrlConnector(javax.ws.rs.client.Client client, HttpUrlConnectorProvider.ConnectionFactory connectionFactory, int chunkSize, boolean fixLengthStreaming, boolean setMethodWorkaround)
CreateHttpUrlConnector
.boolean
equals(java.lang.Object o)
Connector
getConnector(javax.ws.rs.client.Client client, javax.ws.rs.core.Configuration config)
Get a Jersey client connector instance for a givenclient
instance and Jersey client runtimeconfiguration
.int
hashCode()
HttpUrlConnectorProvider
useFixedLengthStreaming()
Instruct the provided connectors to use thefixed-length streaming mode
on the underlying HTTP URL connection instance when sending requests.HttpUrlConnectorProvider
useSetMethodWorkaround()
Instruct the provided connectors to use reflection when setting the HTTP method value.SeeSET_METHOD_WORKAROUND
property documentation for more details.
-
-
-
Field Detail
-
USE_FIXED_LENGTH_STREAMING
public static final java.lang.String USE_FIXED_LENGTH_STREAMING
Iftrue
, theHttpUrlConnector
(if used) will assume the content length from the value of "Content-Length" request header (if present).When this property is enabled and the request has a valid non-zero content length value specified in its "Content-Length" request header, that this value will be used as an input to the
HttpURLConnection.setFixedLengthStreamingMode(int)
method call invoked on the underlyingconnection
. This will also suppress the entity buffering in the @{code HttpURLConnection}, which is undesirable in certain scenarios, e.g. when streaming large entities.Note that the content length value defined in the request header must exactly match the real size of the entity. If the
HttpHeaders.CONTENT_LENGTH
header is explicitly specified in a request, this property will be ignored and the request entity will be still buffered by the underlying @{code HttpURLConnection} infrastructure.This property also overrides the behaviour enabled by the
ClientProperties.CHUNKED_ENCODING_SIZE
property. Chunked encoding will only be used, if the size is not specified in the header of the request.Note that this property only applies to client run-times that are configured to use the default
HttpUrlConnector
as the client connector. The property is ignored by other connectors.The default value is
false
.The name of the configuration property is "jersey.config.client.httpUrlConnector.useFixedLengthStreaming".
- Since:
- 2.5
- See Also:
- Constant Field Values
-
SET_METHOD_WORKAROUND
public static final java.lang.String SET_METHOD_WORKAROUND
A value oftrue
declares that the client will try to set unsupported HTTP method toHttpURLConnection
via reflection.NOTE: Enabling this property may cause security related warnings/errors and it may break when other JDK implementation is used. Use only when you know what you are doing.
The value MUST be an instance of
Boolean
.The default value is
false
.The name of the configuration property is "jersey.config.client.httpUrlConnection.setMethodWorkaround".
- See Also:
- Constant Field Values
-
DEFAULT_CONNECTION_FACTORY
private static final HttpUrlConnectorProvider.ConnectionFactory DEFAULT_CONNECTION_FACTORY
Default connection factory to be used.
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
connectionFactory
private HttpUrlConnectorProvider.ConnectionFactory connectionFactory
-
chunkSize
private int chunkSize
-
useFixedLengthStreaming
private boolean useFixedLengthStreaming
-
useSetMethodWorkaround
private boolean useSetMethodWorkaround
-
-
Method Detail
-
connectionFactory
public HttpUrlConnectorProvider connectionFactory(HttpUrlConnectorProvider.ConnectionFactory connectionFactory)
Set a customHttpURLConnection
factory.- Parameters:
connectionFactory
- custom HTTP URL connection factory. Must not benull
.- Returns:
- updated connector provider instance.
- Throws:
java.lang.NullPointerException
- in case the supplied connectionFactory isnull
.
-
chunkSize
public HttpUrlConnectorProvider chunkSize(int chunkSize)
Set chunk size for requests transferred using a HTTP chunked transfer coding. If no value is set, the default chunk size of 4096 bytes will be used.Note that this programmatically set value can be overridden by setting the
ClientProperties.CHUNKED_ENCODING_SIZE
property specified in the Jersey client instance configuration.- Parameters:
chunkSize
- chunked transfer coding chunk size to be used.- Returns:
- updated connector provider instance.
- Throws:
java.lang.IllegalArgumentException
- in case the specified chunk size is negative.
-
useFixedLengthStreaming
public HttpUrlConnectorProvider useFixedLengthStreaming()
Instruct the provided connectors to use thefixed-length streaming mode
on the underlying HTTP URL connection instance when sending requests. SeeUSE_FIXED_LENGTH_STREAMING
property documentation for more details.Note that this programmatically set value can be overridden by setting the
USE_FIXED_LENGTH_STREAMING
property specified in the Jersey client instance configuration.- Returns:
- updated connector provider instance.
-
useSetMethodWorkaround
public HttpUrlConnectorProvider useSetMethodWorkaround()
Instruct the provided connectors to use reflection when setting the HTTP method value.SeeSET_METHOD_WORKAROUND
property documentation for more details.Note that this programmatically set value can be overridden by setting the
SET_METHOD_WORKAROUND
property specified in the Jersey client instance configuration or in the request properties.- Returns:
- updated connector provider instance.
-
getConnector
public Connector getConnector(javax.ws.rs.client.Client client, javax.ws.rs.core.Configuration config)
Description copied from interface:ConnectorProvider
Get a Jersey client connector instance for a givenclient
instance and Jersey client runtimeconfiguration
.Note that the supplied runtime configuration can be different from the client instance configuration as a single client can be used to serve multiple differently configured runtimes. While the
SSL context
orhostname verifier
are shared, other configuration properties may change in each runtime.Based on the supplied client and runtime configuration data, it is up to each connector provider implementation to decide whether a new dedicated connector instance is required or if the existing, previously create connector instance can be reused.
- Specified by:
getConnector
in interfaceConnectorProvider
- Parameters:
client
- Jersey client instance.config
- Jersey client runtime configuration.- Returns:
- configured
Connector
instance to be used by the client.
-
createHttpUrlConnector
protected Connector createHttpUrlConnector(javax.ws.rs.client.Client client, HttpUrlConnectorProvider.ConnectionFactory connectionFactory, int chunkSize, boolean fixLengthStreaming, boolean setMethodWorkaround)
CreateHttpUrlConnector
.- Parameters:
client
- JAX-RS client instance for which the connector is being created.connectionFactory
-HttpsURLConnection
factory to be used when creating connections.chunkSize
- chunk size to use when using HTTP chunked transfer coding.fixLengthStreaming
- specify if the thefixed-length streaming mode
on the underlying HTTP URL connection instances should be used when sending requests.setMethodWorkaround
- specify if the reflection workaround should be used to set HTTP URL connection method name. SeeSET_METHOD_WORKAROUND
for details.- Returns:
- created
HttpUrlConnector
instance.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-