From a2f2e0e84248a34b7cce3092545b13838eab8974 Mon Sep 17 00:00:00 2001 From: David Kocher Date: Thu, 2 Jun 2016 21:27:03 +0200 Subject: [PATCH] Fix MIME in POST request. (cherry picked from commit ea42bc9) --- .../b2/request/BaseB2Request.java | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/src/main/java/synapticloop/b2/request/BaseB2Request.java b/src/main/java/synapticloop/b2/request/BaseB2Request.java index 6c4c7c1..2f5ff19 100644 --- a/src/main/java/synapticloop/b2/request/BaseB2Request.java +++ b/src/main/java/synapticloop/b2/request/BaseB2Request.java @@ -2,17 +2,17 @@ /* * Copyright (c) 2016 Synapticloop. - * + * * All rights reserved. - * - * This code may contain contributions from other parties which, where - * applicable, will be listed in the default build file for the project + * + * This code may contain contributions from other parties which, where + * applicable, will be listed in the default build file for the project * ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. - * - * This source code and any derived binaries are covered by the terms and - * conditions of the Licence agreement ("the Licence"). You may not use this - * source code or any derived binaries except in compliance with the Licence. - * A copy of the Licence is available in the file named LICENSE.txt shipped with + * + * This source code and any derived binaries are covered by the terms and + * conditions of the Licence agreement ("the Licence"). You may not use this + * source code or any derived binaries except in compliance with the Licence. + * A copy of the Licence is available in the file named LICENSE.txt shipped with * this source code or binaries. */ @@ -44,6 +44,8 @@ import synapticloop.b2.exception.B2ApiException; import synapticloop.b2.response.B2AuthorizeAccountResponse; +import static org.apache.http.entity.ContentType.APPLICATION_JSON; + public abstract class BaseB2Request { private static final Logger LOGGER = LoggerFactory.getLogger(BaseB2Request.class); @@ -85,7 +87,7 @@ protected BaseB2Request(CloseableHttpClient client, final String url) { /** * Instantiate the base B2 request which adds headers with the authorization * token. - * + * * @param client Shared HTTP client * @param b2AuthorizeAccountResponse the authorize account response * @param url Fully qualified request URI @@ -97,7 +99,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b /** * Instantiate the base B2 request which adds headers with the authorization * token. - * + * * @param client Shared HTTP client * @param b2AuthorizeAccountResponse the authorize account response * @param url Fully qualified request URI @@ -113,7 +115,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b /** * Add header to request replacing previous if any - * + * * @param key the key to add * @param value the value to add */ @@ -123,7 +125,7 @@ protected void addHeader(String key, String value) { /** * Add query parameter to request replacing previous if any - * + * * @param key the key to add * @param value the value to add */ @@ -133,7 +135,7 @@ protected void addParameter(String key, String value) { /** * Add property to JSON request body - * + * * @param key the key to add * @param value the value to add */ @@ -143,9 +145,9 @@ protected void addProperty(String key, Object value) { /** * Execute an HTTP HEAD request and return the response for further parsing - * + * * @return the response object - * + * * @throws B2ApiException if something went wrong with the call * @throws IOException if there was an error communicating with the API service */ @@ -173,7 +175,7 @@ protected CloseableHttpResponse executeHead() throws B2ApiException, IOException /** * Execute a GET request, returning the data stream from the response. - * + * * @return The response from the GET request * * @throws B2ApiException if there was an error with the request @@ -203,9 +205,9 @@ protected CloseableHttpResponse executeGet() throws B2ApiException, IOException /** * Execute a POST request returning the response data as a String - * + * * @return the response data as a string - * + * * @throws B2ApiException if there was an error with the call, most notably * a non OK status code (i.e. not 200) * @throws IOException if there was an error communicating with the API service @@ -216,7 +218,7 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException String postData = convertPostData(); HttpPost httpPost = new HttpPost(uri); - httpPost.setEntity(new StringEntity(postData)); + httpPost.setEntity(new StringEntity(postData, APPLICATION_JSON)); CloseableHttpResponse httpResponse = this.execute(httpPost); switch(httpResponse.getStatusLine().getStatusCode()) { @@ -234,11 +236,11 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException /** * Execute a POST request with the contents of a file. - * + * * @param entity Content to write - * + * * @return the string representation of the response - * + * * @throws B2ApiException if there was an error with the call, most notably * a non OK status code (i.e. not 200) * @throws IOException if there was an error communicating with the API service @@ -263,9 +265,9 @@ protected CloseableHttpResponse executePost(HttpEntity entity) throws B2ApiExcep /** * Convert the stringData and integerData Maps to JSON format, to be included * in the POST body of the request. - * + * * @return the JSON string of the data - * + * * @throws IOException if there was an error converting the data. */ protected String convertPostData() throws IOException { @@ -283,11 +285,11 @@ protected String convertPostData() throws IOException { } /** - * Return the URI for this request, which adds any parameters found in the + * Return the URI for this request, which adds any parameters found in the * 'parameters' data structure - * + * * @return The URI for this request, with properly encoded parameters - * + * * @throws IOException If there was an error building the URI */ protected URI buildUri() throws IOException { @@ -306,15 +308,15 @@ protected URI buildUri() throws IOException { } /** - * Set the headers safely, go through the headers Map and add them to the http - * request with properly encode values. If they already exist on the http + * Set the headers safely, go through the headers Map and add them to the http + * request with properly encode values. If they already exist on the http * request, it will be ignored. - * + * * To override what headers are set, this should be done in the constructor * of the base request object. - * + * * @param request The HTTP request to set the headers on - * + * * @throws B2ApiException if there was an error setting the headers */ protected void setHeaders(HttpUriRequest request) throws B2ApiException { @@ -342,9 +344,9 @@ protected CloseableHttpResponse execute(final HttpUriRequest request) throws IOE /** * Obfuscate the data by removing the accountId and replacing it with the * string "[redacted]" - * + * * @param data the data to obfuscate - * + * * @return the obfuscated data */ private Object obfuscateData(String key, Object data) {