From 7b6b9056d36a0987df04df86f88715b40386a1e2 Mon Sep 17 00:00:00 2001 From: Pablo Fernandez Date: Sat, 7 May 2011 13:50:10 -0300 Subject: [PATCH] multipart request support --- src/main/java/org/scribe/model/Request.java | 55 ++++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/scribe/model/Request.java b/src/main/java/org/scribe/model/Request.java index d49aa3e..50c0f89 100644 --- a/src/main/java/org/scribe/model/Request.java +++ b/src/main/java/org/scribe/model/Request.java @@ -26,6 +26,7 @@ class Request private String payload = null; private HttpURLConnection connection; private String charset; + private byte[] bytePayload = null; /** * Creates a new Http Request @@ -77,7 +78,7 @@ class Request addHeaders(connection); if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) { - addBody(connection, getBodyContents()); + addBody(connection, getByteBodyContents()); } return new Response(connection); } @@ -88,13 +89,11 @@ class Request conn.setRequestProperty(key, headers.get(key)); } - void addBody(HttpURLConnection conn, String content) throws IOException + void addBody(HttpURLConnection conn, byte[] content) throws IOException { - if (this.charset == null) - this.charset = Charset.defaultCharset().name(); - conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.getBytes(charset).length)); + conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length)); conn.setDoOutput(true); - conn.getOutputStream().write(content.getBytes(charset)); + conn.getOutputStream().write(content); } /** @@ -145,6 +144,16 @@ class Request this.payload = payload; } + /** + * Overloaded version for byte arrays + * + * @param payload + */ + public void addPayload(byte[] payload) + { + this.bytePayload = payload; + } + /** * Get a {@link Map} of the query string parameters. * @@ -201,10 +210,32 @@ class Request * Returns the body of the request * * @return form encoded string + * @throws OAuthException if the charset chosen is not supported */ public String getBodyContents() { - return (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams); + try + { + return new String(getByteBodyContents(),getCharset()); + } + catch(UnsupportedEncodingException uee) + { + throw new OAuthException("Unsupported Charset: "+charset, uee); + } + } + + byte[] getByteBodyContents() + { + if (bytePayload != null) return bytePayload; + String body = (payload != null) ? payload : URLUtils.formURLEncodeMap(bodyParams); + try + { + return body.getBytes(getCharset()); + } + catch(UnsupportedEncodingException uee) + { + throw new OAuthException("Unsupported Charset: "+getCharset(), uee); + } } /** @@ -227,6 +258,16 @@ class Request return headers; } + /** + * Returns the connection charset. Defaults to {@link Charset} defaultCharset if not set + * + * @return charset + */ + public String getCharset() + { + return charset == null ? Charset.defaultCharset().name() : charset; + } + /** * Sets the connect timeout for the underlying {@link HttpURLConnection} *