Class HttpClientSupport
- java.lang.Object
-
- net.shibboleth.utilities.java.support.httpclient.HttpClientSupport
-
public final class HttpClientSupport extends Object
Support class for usingHttpClientand related components.
-
-
Field Summary
Fields Modifier and Type Field Description static StringCONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERSContext key for instances of dynamic context handlers to be invoked before and after the HTTP request.
-
Constructor Summary
Constructors Modifier Constructor Description privateHttpClientSupport()Constructor to prevent instantiation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddDynamicContextHandlerFirst(org.apache.http.client.protocol.HttpClientContext context, HttpClientContextHandler handler)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the first handler list position.static voidaddDynamicContextHandlerFirst(org.apache.http.client.protocol.HttpClientContext context, HttpClientContextHandler handler, boolean uniqueType)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the first handler list position.static voidaddDynamicContextHandlerLast(org.apache.http.client.protocol.HttpClientContext context, HttpClientContextHandler handler)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the last handler list position.static voidaddDynamicContextHandlerLast(org.apache.http.client.protocol.HttpClientContext context, HttpClientContextHandler handler, boolean uniqueType)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the last handler list position.static org.apache.http.conn.socket.LayeredConnectionSocketFactorybuildNoTrustTLSSocketFactory()Build a TLS-capable instance ofLayeredConnectionSocketFactorywhich accepts all peer certificates and performs no hostname verification.static X509TrustManagerbuildNoTrustX509TrustManager()Build an instance ofX509TrustManagerwhich trusts all certificates.static org.apache.http.conn.socket.LayeredConnectionSocketFactorybuildStrictTLSSocketFactory()Build an instance of TLS-capableLayeredConnectionSocketFactorywhich uses the standard JSSE defaultSSLContextand which performs strict hostname verification.static List<HttpClientContextHandler>getDynamicContextHandlerList(org.apache.http.client.protocol.HttpClientContext context)Get the list ofHttpClientContextHandlerfor theHttpClientContext.static StringtoString(org.apache.http.HttpEntity entity, int maxLength)Read the contents of an entity and return it as a String.static StringtoString(org.apache.http.HttpEntity entity, String defaultCharset, int maxLength)Get the entity content as a String, using the provided default character set if none is found in the entity.static StringtoString(org.apache.http.HttpEntity entity, Charset defaultCharset, int maxLength)Get the entity content as a String, using the provided default character set if none is found in the entity.
-
-
-
Field Detail
-
CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS
@Nonnull @NotEmpty public static final String CONTEXT_KEY_DYNAMIC_CONTEXT_HANDLERS
Context key for instances of dynamic context handlers to be invoked before and after the HTTP request. Must be an instance ofList<HttpClientContextHandler>.- See Also:
- Constant Field Values
-
-
Method Detail
-
buildStrictTLSSocketFactory
@Nonnull public static org.apache.http.conn.socket.LayeredConnectionSocketFactory buildStrictTLSSocketFactory()
Build an instance of TLS-capableLayeredConnectionSocketFactorywhich uses the standard JSSE defaultSSLContextand which performs strict hostname verification.- Returns:
- a new instance of HttpClient SSL connection socket factory
-
buildNoTrustTLSSocketFactory
@Nonnull public static org.apache.http.conn.socket.LayeredConnectionSocketFactory buildNoTrustTLSSocketFactory()
Build a TLS-capable instance ofLayeredConnectionSocketFactorywhich accepts all peer certificates and performs no hostname verification.- Returns:
- a new instance of HttpClient SSL connection socket factory
-
buildNoTrustX509TrustManager
@Nonnull public static X509TrustManager buildNoTrustX509TrustManager()
Build an instance ofX509TrustManagerwhich trusts all certificates.- Returns:
- a new trust manager instance
-
getDynamicContextHandlerList
@Nonnull public static List<HttpClientContextHandler> getDynamicContextHandlerList(@Nonnull org.apache.http.client.protocol.HttpClientContext context)
Get the list ofHttpClientContextHandlerfor theHttpClientContext.- Parameters:
context- the client context- Returns:
- the handler list
-
addDynamicContextHandlerFirst
public static void addDynamicContextHandlerFirst(@Nonnull org.apache.http.client.protocol.HttpClientContext context, @Nonnull HttpClientContextHandler handler)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the first handler list position.- Parameters:
context- the client contexthandler- the handler to add
-
addDynamicContextHandlerFirst
public static void addDynamicContextHandlerFirst(@Nonnull org.apache.http.client.protocol.HttpClientContext context, @Nonnull HttpClientContextHandler handler, boolean uniqueType)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the first handler list position.- Parameters:
context- the client contexthandler- the handler to adduniqueType- whether to only add the handler if an instance of its (exact) class is not already present
-
addDynamicContextHandlerLast
public static void addDynamicContextHandlerLast(@Nonnull org.apache.http.client.protocol.HttpClientContext context, @Nonnull HttpClientContextHandler handler)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the last handler list position.- Parameters:
context- the client contexthandler- the handler to add
-
addDynamicContextHandlerLast
public static void addDynamicContextHandlerLast(@Nonnull org.apache.http.client.protocol.HttpClientContext context, @Nonnull HttpClientContextHandler handler, boolean uniqueType)Add the specified instance ofHttpClientContextHandlerto theHttpClientContextin the last handler list position.- Parameters:
context- the client contexthandler- the handler to adduniqueType- whether to only add the handler if an instance of its (exact) class is not already present
-
toString
@Nullable public static String toString(@Nonnull org.apache.http.HttpEntity entity, @Nullable Charset defaultCharset, int maxLength) throws IOException, org.apache.http.ParseException
Get the entity content as a String, using the provided default character set if none is found in the entity.If defaultCharset is null, the default "ISO-8859-1" is used.
- Parameters:
entity- must not be nulldefaultCharset- character set to be applied if none found in the entitymaxLength- limit on size of content- Returns:
- the entity content as a String. May be null if
HttpEntity.getContent()is null. - Throws:
org.apache.http.ParseException- if header elements cannot be parsedIOException- if an error occurs reading the input stream, or the size exceeds limitsUnsupportedCharsetException- when the content's charset is not available
-
toString
@Nullable public static String toString(@Nonnull org.apache.http.HttpEntity entity, @Nullable String defaultCharset, int maxLength) throws IOException, org.apache.http.ParseException
Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.- Parameters:
entity- must not be nulldefaultCharset- character set to be applied if none found in the entitymaxLength- limit on size of content- Returns:
- the entity content as a String. May be null if
HttpEntity.getContent()is null. - Throws:
org.apache.http.ParseException- if header elements cannot be parsedIOException- if an error occurs reading the input stream, or the size exceeds limitsUnsupportedCharsetException- when the content's charset is not available
-
toString
@Nullable public static String toString(@Nonnull org.apache.http.HttpEntity entity, int maxLength) throws IOException, org.apache.http.ParseException
Read the contents of an entity and return it as a String. The content is converted using the character set from the entity (if any), failing that, "ISO-8859-1" is used.- Parameters:
entity- the entity to convert to a string; must not be nullmaxLength- limit on size of content- Returns:
- the entity content as a String. May be null if
HttpEntity.getContent()is null. - Throws:
org.apache.http.ParseException- if header elements cannot be parsedIOException- if an error occurs reading the input stream, or the size exceeds limitsUnsupportedCharsetException- when the content's charset is not available
-
-