[java-support] branch master updated: Add copy of EntityUtils.toString with a maxLength option.
Scott Cantor
cantor.2 at osu.edu
Mon May 22 22:21:07 EDT 2017
This is an automated email from the git hooks/post-receive script.
scantor pushed a commit to branch master
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=3b8c76587e6eaa81d51a009af48a086ec7731188
The following commit(s) were added to refs/heads/master by this push:
new 3b8c765 Add copy of EntityUtils.toString with a maxLength option.
3b8c765 is described below
commit 3b8c76587e6eaa81d51a009af48a086ec7731188
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon May 22 22:21:05 2017 -0400
Add copy of EntityUtils.toString with a maxLength option.
---
.../java/support/httpclient/HttpClientSupport.java | 128 ++++++++++++++++++++-
1 file changed, 122 insertions(+), 6 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
index 2d948a5..b91e1c2 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/HttpClientSupport.java
@@ -17,21 +17,35 @@
package net.shibboleth.utilities.java.support.httpclient;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Collections;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
+import org.apache.http.HttpEntity;
+import org.apache.http.ParseException;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
+import org.apache.http.entity.ContentType;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.CharArrayBuffer;
/**
* Support class for using {@link org.apache.http.client.HttpClient} and related components.
@@ -48,7 +62,7 @@ public final class HttpClientSupport {
*
* @return a new instance of HttpClient SSL connection socket factory
*/
- public static LayeredConnectionSocketFactory buildStrictTLSSocketFactory() {
+ @Nonnull public static LayeredConnectionSocketFactory buildStrictTLSSocketFactory() {
return new TLSSocketFactoryBuilder()
.setHostnameVerifier(new StrictHostnameVerifier())
.build();
@@ -60,7 +74,7 @@ public final class HttpClientSupport {
*
* @return a new instance of HttpClient SSL connection socket factory
*/
- public static LayeredConnectionSocketFactory buildNoTrustTLSSocketFactory() {
+ @Nonnull public static LayeredConnectionSocketFactory buildNoTrustTLSSocketFactory() {
return new TLSSocketFactoryBuilder()
.setTrustManagers(Collections.<TrustManager>singletonList(buildNoTrustX509TrustManager()))
.setHostnameVerifier(new AllowAllHostnameVerifier())
@@ -77,7 +91,7 @@ public final class HttpClientSupport {
* @deprecated use instead {@link #buildStrictTLSSocketFactory()}
*/
@Deprecated
- public static SSLConnectionSocketFactory buildStrictSSLConnectionSocketFactory() {
+ @Nonnull public static SSLConnectionSocketFactory buildStrictSSLConnectionSocketFactory() {
return new SSLConnectionSocketFactory(
SSLContexts.createDefault(),
SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
@@ -92,7 +106,7 @@ public final class HttpClientSupport {
* @deprecated use instead {@link #buildNoTrustTLSSocketFactory()}
*/
@Deprecated
- public static SSLConnectionSocketFactory buildNoTrustSSLConnectionSocketFactory() {
+ @Nonnull public static SSLConnectionSocketFactory buildNoTrustSSLConnectionSocketFactory() {
final X509TrustManager noTrustManager = buildNoTrustX509TrustManager();
try {
@@ -112,7 +126,7 @@ public final class HttpClientSupport {
*
* @return a new trust manager instance
*/
- public static X509TrustManager buildNoTrustX509TrustManager() {
+ @Nonnull public static X509TrustManager buildNoTrustX509TrustManager() {
return new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
@@ -132,4 +146,106 @@ public final class HttpClientSupport {
}
-}
+// Checkstyle: CyclomaticComplexity OFF
+ /**
+ * Get the entity content as a String, using the provided default character set
+ * if none is found in the entity.
+ *
+ * <p>If defaultCharset is null, the default "ISO-8859-1" is used.</p>
+ *
+ * @param entity must not be null
+ * @param defaultCharset character set to be applied if none found in the entity
+ * @param maxLength limit on size of content
+ *
+ * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
+ *
+ * @throws ParseException if header elements cannot be parsed
+ * @throws IOException if an error occurs reading the input stream, or the size exceeds limits
+ * @throws UnsupportedCharsetException when the content's charset is not available
+ */
+ @Nullable public static String toString(@Nonnull final HttpEntity entity, @Nullable final Charset defaultCharset,
+ final int maxLength) throws IOException, ParseException {
+ try (final InputStream instream = entity.getContent()) {
+ if (instream == null) {
+ return null;
+ }
+ if (entity.getContentLength() > maxLength || entity.getContentLength() > Integer.MAX_VALUE) {
+ throw new IOException("HTTP entity size exceeded limit");
+ }
+ int i = (int) entity.getContentLength();
+ if (i < 0) {
+ i = 4096;
+ }
+ Charset charset = null;
+ try {
+ final ContentType contentType = ContentType.get(entity);
+ if (contentType != null) {
+ charset = contentType.getCharset();
+ }
+ } catch (final UnsupportedCharsetException ex) {
+ throw new UnsupportedEncodingException(ex.getMessage());
+ }
+ if (charset == null) {
+ charset = defaultCharset;
+ }
+ if (charset == null) {
+ charset = HTTP.DEF_CONTENT_CHARSET;
+ }
+ try (final Reader reader = new InputStreamReader(instream, charset)) {
+ final CharArrayBuffer buffer = new CharArrayBuffer(i);
+ final char[] tmp = new char[1024];
+ int size = 0;
+ int l;
+ while((l = reader.read(tmp)) != -1) {
+ size += l;
+ if (size > maxLength) {
+ throw new IOException("HTTP entity size exceeded limit");
+ }
+ buffer.append(tmp, 0, l);
+ }
+ return buffer.toString();
+ }
+ }
+ }
+// Checkstyle: CyclomaticComplexity ON
+
+ /**
+ * 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.
+ *
+ * @param entity must not be null
+ * @param defaultCharset character set to be applied if none found in the entity
+ * @param maxLength limit on size of content
+ *
+ * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
+ *
+ * @throws ParseException if header elements cannot be parsed
+ * @throws IOException if an error occurs reading the input stream, or the size exceeds limits
+ * @throws UnsupportedCharsetException when the content's charset is not available
+ */
+ @Nullable public static String toString(@Nonnull final HttpEntity entity, @Nullable final String defaultCharset,
+ final int maxLength) throws IOException, ParseException {
+ return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null, maxLength);
+ }
+
+ /**
+ * 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.
+ *
+ * @param entity the entity to convert to a string; must not be null
+ * @param maxLength limit on size of content
+ *
+ * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
+ *
+ * @throws ParseException if header elements cannot be parsed
+ * @throws IOException if an error occurs reading the input stream, or the size exceeds limits
+ * @throws UnsupportedCharsetException when the content's charset is not available
+ */
+ @Nullable public static String toString(@Nonnull final HttpEntity entity, final int maxLength)
+ throws IOException, ParseException {
+ return toString(entity, (Charset) null, maxLength);
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list