diff --git a/http-client-java/src/org/expath/httpclient/impl/ApacheHttpConnection.java b/http-client-java/src/org/expath/httpclient/impl/ApacheHttpConnection.java index f22db54..5e29ad2 100644 --- a/http-client-java/src/org/expath/httpclient/impl/ApacheHttpConnection.java +++ b/http-client-java/src/org/expath/httpclient/impl/ApacheHttpConnection.java @@ -9,10 +9,7 @@ package org.expath.httpclient.impl; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.ProxySelector; import java.net.URI; import org.apache.commons.logging.Log; @@ -36,6 +33,7 @@ import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.params.ClientPNames; import org.apache.http.conn.routing.HttpRoutePlanner; +import org.apache.http.entity.AbstractHttpEntity; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentProducer; import org.apache.http.entity.EntityTemplate; @@ -45,6 +43,7 @@ import org.apache.http.impl.conn.ProxySelectorRoutePlanner; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; +import org.apache.http.util.Args; import org.expath.httpclient.HeaderSet; import org.expath.httpclient.HttpClientException; import org.expath.httpclient.HttpConnection; @@ -353,7 +352,7 @@ private void setRequestEntity(HttpRequestBody body) return; } // make the entity from a new producer - HttpEntity entity; + final HttpEntity entity; if ( myVersion == HttpVersion.HTTP_1_1 ) { // Take advantage of HTTP 1.1 chunked encoding to stream the // payload directly to the request. @@ -366,10 +365,14 @@ private void setRequestEntity(HttpRequestBody body) // With HTTP 1.0, chunked encoding is not supported, so first // serialize into memory and use the resulting byte array as the // entity payload. - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - body.serialize(buffer); - entity = new ByteArrayEntity(buffer.toByteArray()); + try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { + body.serialize(buffer); + entity = new ByteArrayEntity(buffer.toByteArray()); + } catch (final IOException e) { + throw new HttpClientException(e.getMessage(), e); + } } + // cast the request HttpEntityEnclosingRequestBase req = null; if ( ! (myRequest instanceof HttpEntityEnclosingRequestBase) ) { diff --git a/http-client-java/src/org/expath/httpclient/impl/BinaryResponseBody.java b/http-client-java/src/org/expath/httpclient/impl/BinaryResponseBody.java index 06f12f4..f274da6 100644 --- a/http-client-java/src/org/expath/httpclient/impl/BinaryResponseBody.java +++ b/http-client-java/src/org/expath/httpclient/impl/BinaryResponseBody.java @@ -28,13 +28,6 @@ public class BinaryResponseBody implements HttpResponseBody { - public BinaryResponseBody(Result result, byte[] value, ContentType type, HeaderSet headers) - throws HttpClientException - { - myContentType = type; - myHeaders = headers; - result.add(value); - } // TODO: Work only for binary response. What if the response is encoded // with base64? @@ -48,19 +41,7 @@ public BinaryResponseBody(Result result, InputStream in, ContentType type, Heade { myContentType = type; myHeaders = headers; - try { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buf = new byte[4096]; - int read = 0; - while ( (read = in.read(buf)) > 0 ) { - out.write(buf, 0, read); - } - byte[] bytes = out.toByteArray(); - result.add(bytes); - } - catch ( IOException ex ) { - throw new HttpClientException("error reading HTTP response", ex); - } + result.add(in); } @Override diff --git a/http-client-java/src/org/expath/httpclient/impl/HrefRequestBody.java b/http-client-java/src/org/expath/httpclient/impl/HrefRequestBody.java index fbcdb5a..34df06f 100644 --- a/http-client-java/src/org/expath/httpclient/impl/HrefRequestBody.java +++ b/http-client-java/src/org/expath/httpclient/impl/HrefRequestBody.java @@ -62,23 +62,18 @@ public void serialize(OutputStream out) { try { String filename = new URI(myHref).getPath(); - InputStream in = new FileInputStream(new File(filename)); - byte[] buf = new byte[4096]; - int l = -1; - while ( (l = in.read(buf)) != -1 ) { - out.write(buf, 0, l); + try (final InputStream in = new FileInputStream(new File(filename))) { + byte[] buf = new byte[4096]; + int l = -1; + while ((l = in.read(buf)) != -1) { + out.write(buf, 0, l); + } + } catch (IOException ex) { + throw new HttpClientException("Error sending the file content", ex); } - in.close(); - } - catch ( URISyntaxException ex ) { + } catch ( URISyntaxException ex ) { throw new HttpClientException("Bad URI: " + myHref, ex); } - catch ( FileNotFoundException ex ) { - throw new HttpClientException("Error sending the file content", ex); - } - catch ( IOException ex ) { - throw new HttpClientException("Error sending the file content", ex); - } } private String myHref; diff --git a/http-client-java/src/org/expath/httpclient/impl/TextResponseBody.java b/http-client-java/src/org/expath/httpclient/impl/TextResponseBody.java index 7c33c4c..b36c499 100644 --- a/http-client-java/src/org/expath/httpclient/impl/TextResponseBody.java +++ b/http-client-java/src/org/expath/httpclient/impl/TextResponseBody.java @@ -9,12 +9,10 @@ package org.expath.httpclient.impl; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.UnsupportedEncodingException; +import java.io.*; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + import org.expath.httpclient.ContentType; import org.expath.httpclient.HeaderSet; import org.expath.httpclient.HttpClientException; @@ -34,45 +32,20 @@ public class TextResponseBody public TextResponseBody(Result result, InputStream in, ContentType type, HeaderSet headers) throws HttpClientException { - // FIXME: ... - String charset = "utf-8"; - try { - Reader reader = new InputStreamReader(in, charset); - init(result, reader, type, headers); - } - catch ( UnsupportedEncodingException ex ) { - String msg = "not supported charset reading HTTP response: " + charset; - throw new HttpClientException(msg, ex); - } + myContentType = type; + myHeaders = headers; + // TODO: ... + final Charset charset = StandardCharsets.UTF_8; + final Reader reader = new InputStreamReader(in, charset); + result.add(reader); } public TextResponseBody(Result result, Reader in, ContentType type, HeaderSet headers) throws HttpClientException - { - init(result, in, type, headers); - } - - private void init(Result result, Reader in, ContentType type, HeaderSet headers) - throws HttpClientException { myContentType = type; myHeaders = headers; - // BufferedReader handles the ends of line (all \n, \r, and \r\n are - // transformed to \n) - try { - StringBuilder builder = new StringBuilder(); - BufferedReader buf_in = new BufferedReader(in); - String buf = null; - while ( (buf = buf_in.readLine()) != null ) { - builder.append(buf); - builder.append('\n'); - } - String value = builder.toString(); - result.add(value); - } - catch ( IOException ex ) { - throw new HttpClientException("error reading HTTP response", ex); - } + result.add(in); } @Override diff --git a/http-client-java/src/org/expath/httpclient/impl/XmlResponseBody.java b/http-client-java/src/org/expath/httpclient/impl/XmlResponseBody.java index 6473876..4a65e16 100644 --- a/http-client-java/src/org/expath/httpclient/impl/XmlResponseBody.java +++ b/http-client-java/src/org/expath/httpclient/impl/XmlResponseBody.java @@ -13,6 +13,8 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import javax.xml.transform.Source; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; @@ -39,15 +41,9 @@ public XmlResponseBody(Result result, InputStream in, ContentType type, HeaderSe throws HttpClientException { // TODO: ... - String charset = "utf-8"; - try { - Reader reader = new InputStreamReader(in, charset); - init(result, reader, type, headers, html); - } - catch ( UnsupportedEncodingException ex ) { - String msg = "not supported charset reading HTTP response: " + charset; - throw new HttpClientException(msg, ex); - } + final Charset charset = StandardCharsets.UTF_8; + final Reader reader = new InputStreamReader(in, charset); + init(result, reader, type, headers, html); } public XmlResponseBody(Result result, Reader in, ContentType type, HeaderSet headers, boolean html) @@ -56,7 +52,7 @@ public XmlResponseBody(Result result, Reader in, ContentType type, HeaderSet hea init(result, in, type, headers, html); } - private void init(Result result, Reader in, ContentType type, HeaderSet headers, boolean html) + private void init(final Result result, final Reader in, final ContentType type, final HeaderSet headers, final boolean html) throws HttpClientException { myContentType = type; @@ -71,13 +67,12 @@ private void init(Result result, Reader in, ContentType type, HeaderSet headers, InputSource input = new InputSource(in); src = new SAXSource(parser, input); src.setSystemId(sys_id); - } - else { + } else { src = new StreamSource(in, sys_id); } result.add(src); } - catch ( SAXException ex ) { + catch (SAXException ex) { throw new HttpClientException("error parsing result HTML", ex); } } diff --git a/http-client-java/src/org/expath/httpclient/model/Result.java b/http-client-java/src/org/expath/httpclient/model/Result.java index c16dc9f..cd1c135 100644 --- a/http-client-java/src/org/expath/httpclient/model/Result.java +++ b/http-client-java/src/org/expath/httpclient/model/Result.java @@ -9,6 +9,8 @@ package org.expath.httpclient.model; +import java.io.Reader; +import java.io.InputStream; import javax.xml.transform.Source; import org.expath.httpclient.HttpClientException; import org.expath.httpclient.HttpResponse; @@ -63,22 +65,22 @@ public Result makeNewResult() throws HttpClientException; /** - * Add an {@code xs:string} to the result sequence. + * Add a string value to the result sequence. * * @param string The string to add to the result sequence. * @throws HttpClientException If any error occurs. */ - public void add(String string) + public void add(Reader string) throws HttpClientException; /** - * Add an {@code xs:base64Binary} to the result sequence. + * Add raw binary to the result sequence. * - * @param bytes The bytes representing the base64 binary item to add to the + * @param bytes The bytes representing the binary item to add to the * result sequence. * @throws HttpClientException If any error occurs. */ - public void add(byte[] bytes) + public void add(InputStream bytes) throws HttpClientException; /** @@ -124,5 +126,5 @@ public void add(HttpResponse response) /* */ /* The Initial Developer of the Original Code is Florent Georges. */ /* */ -/* Contributor(s): none. */ +/* Contributor(s): Adam Retter */ /* ------------------------------------------------------------------------ */ diff --git a/http-client-saxon/src/org/expath/httpclient/saxon/SaxonResult.java b/http-client-saxon/src/org/expath/httpclient/saxon/SaxonResult.java index 14f71a9..34eeb28 100644 --- a/http-client-saxon/src/org/expath/httpclient/saxon/SaxonResult.java +++ b/http-client-saxon/src/org/expath/httpclient/saxon/SaxonResult.java @@ -9,6 +9,7 @@ package org.expath.httpclient.saxon; +import java.io.*; import java.util.ArrayList; import java.util.List; import javax.xml.transform.Source; @@ -50,19 +51,44 @@ public Result makeNewResult() } @Override - public void add(String string) + public void add(Reader reader) throws HttpClientException { - Item item = new StringValue(string); - myItems.add(item); + try(final BufferedReader buf_in = new BufferedReader(reader)) { + final StringBuilder builder = new StringBuilder(); + + String buf = null; + while ( (buf = buf_in.readLine()) != null ) { + builder.append(buf); + builder.append('\n'); + } + final String value = builder.toString(); + + Item item = new StringValue(value); + myItems.add(item); + } + catch ( final IOException ex ) { + throw new HttpClientException("error reading HTTP response", ex); + } } @Override - public void add(byte[] bytes) + public void add(InputStream inputStream) throws HttpClientException { - Item item = new Base64BinaryValue(bytes); - myItems.add(item); + try(final ByteArrayOutputStream out = new ByteArrayOutputStream()) { + final byte[] buf = new byte[4096]; + int read = -1; + while ( (read = inputStream.read(buf)) > 0 ) { + out.write(buf, 0, read); + } + final byte[] bytes = out.toByteArray(); + + Item item = new Base64BinaryValue(bytes); + myItems.add(item); + } catch(final IOException e) { + throw new HttpClientException(e.getMessage(), e); + } } @Override