Search in sources :

Example 1 with Response

use of cc.protea.util.http.Response in project java-sdk by hyperwallet.

the class MultipartRequest method putResource.

public Response putResource(boolean usesProxy, Proxy proxy, final String proxyUsername, final String proxyPassword) throws IOException {
    Response response = new Response();
    URL url = new URL(requestURL);
    final String pair = username + ":" + password;
    final String base64 = DatatypeConverter.printBase64Binary(pair.getBytes());
    // If there is proxy authentication provided, it will be set here
    if (proxyUsername != null && proxyPassword != null) {
        // NOTE: Removing Basic Auth from tunneling disabledSchemas is required in order
        // for Proxy Authorization to work. To prevent overriding client System Settings,
        // the client should set this System property themselves inside their JVM Options (1)
        // or their own code (2). Approaches listed below:
        // 1. jdk.http.auth.tunneling.disabledSchemes=
        // 2. System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "false");
        Authenticator authenticator = new Request.DefaultPasswordAuthenticator(proxyUsername, proxyPassword);
        Authenticator.setDefault(authenticator);
    }
    connection = usesProxy ? (HttpURLConnection) url.openConnection(proxy) : (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("authorization", "Basic " + base64);
    connection.setRequestProperty("accept", "application/json");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
    outStream = new DataOutputStream(this.connection.getOutputStream());
    writeMultipartBody();
    outStream.flush();
    outStream.close();
    // checks server's status code first
    int status = this.connection.getResponseCode();
    InputStream responseStream;
    if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
        responseStream = new BufferedInputStream(connection.getInputStream());
    } else {
        responseStream = new BufferedInputStream(connection.getErrorStream());
    }
    BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
    String line = "";
    StringBuilder stringBuilder = new StringBuilder();
    while ((line = responseStreamReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }
    responseStreamReader.close();
    response.setResponseCode(status);
    response.setBody(stringBuilder.toString());
    response.setHeaders(this.connection.getHeaderFields());
    this.connection.disconnect();
    return response;
}
Also used : Response(cc.protea.util.http.Response)

Example 2 with Response

use of cc.protea.util.http.Response in project java-sdk by hyperwallet.

the class HyperwalletApiClient method post.

public <T> T post(final String url, final Object bodyObject, final Class<T> type, HashMap<String, String> header) {
    Response response = null;
    try {
        String body = convert(bodyObject);
        Request request = getService(url, false).setBody(encrypt(body));
        if (header != null) {
            for (String key : header.keySet()) {
                request = request.addHeader(key, header.get(key));
            }
        }
        response = request.postResource();
        return processResponse(response, type);
    } catch (IOException | JOSEException | ParseException e) {
        throw new HyperwalletException(e);
    }
}
Also used : Response(cc.protea.util.http.Response) HyperwalletException(com.hyperwallet.clientsdk.HyperwalletException) IOException(java.io.IOException) ParseException(java.text.ParseException) JOSEException(com.nimbusds.jose.JOSEException)

Example 3 with Response

use of cc.protea.util.http.Response in project java-sdk by hyperwallet.

the class HyperwalletApiClient method put.

public <T> T put(final String url, final Object bodyObject, final Class<T> type) {
    Response response = null;
    try {
        String body = convert(bodyObject);
        response = getService(url, false).setBody(encrypt(body)).putResource();
        return processResponse(response, type);
    } catch (IOException | JOSEException | ParseException e) {
        throw new HyperwalletException(e);
    }
}
Also used : Response(cc.protea.util.http.Response) HyperwalletException(com.hyperwallet.clientsdk.HyperwalletException) IOException(java.io.IOException) ParseException(java.text.ParseException) JOSEException(com.nimbusds.jose.JOSEException)

Example 4 with Response

use of cc.protea.util.http.Response in project java-sdk by hyperwallet.

the class HyperwalletApiClientTest method checkErrorResponse.

@Test
public void checkErrorResponse() throws Exception {
    Response errorResponse = new Response();
    int responseCode = 500;
    errorResponse.setResponseCode(responseCode);
    String responseMessage = "something went wrong";
    errorResponse.setResponseMessage(responseMessage);
    errorResponse.setBody(getErrorResponseBodyString());
    try {
        hyperwalletApiClient.checkErrorResponse(errorResponse);
        fail("Expect HyperwalletException");
    } catch (HyperwalletException e) {
        assertThat(e.getErrorCode(), not(String.valueOf(responseCode)));
        assertThat(e.getErrorCode(), is("ERROR_CODE"));
        assertThat(e.getErrorMessage(), not(equalTo("responseMessage")));
        assertThat(e.getErrorMessage(), is(equalTo("ERROR_MESSAGE")));
        assertThat(e.getHyperwalletErrors(), is(notNullValue()));
        assertThat(e.getHyperwalletErrors().get(0).getRelatedResources(), is(notNullValue()));
        assertThat(e.getHyperwalletErrors().get(0).getRelatedResources().get(0), is("relatedResource1"));
        assertThat(e.getHyperwalletErrors().get(0).getRelatedResources().get(1), is("relatedResource2"));
    }
}
Also used : HttpResponse(org.mockserver.model.HttpResponse) Response(cc.protea.util.http.Response) HyperwalletException(com.hyperwallet.clientsdk.HyperwalletException) Test(org.testng.annotations.Test)

Example 5 with Response

use of cc.protea.util.http.Response in project java-sdk by hyperwallet.

the class Request method readResponse.

/**
 * A private method that handles reading the Responses from the server.
 *
 * @return a {@link Response} from the server.
 * @throws IOException
 */
private Response readResponse() throws IOException {
    Response response = new Response();
    response.setResponseCode(connection.getResponseCode());
    response.setResponseMessage(connection.getResponseMessage());
    response.setHeaders(connection.getHeaderFields());
    try {
        response.setBody(getStringFromStream(connection.getInputStream()));
    } catch (IOException e) {
        response.setBody(getStringFromStream(connection.getErrorStream()));
    }
    return response;
}
Also used : BinaryResponse(cc.protea.util.http.BinaryResponse) Response(cc.protea.util.http.Response) IOException(java.io.IOException)

Aggregations

Response (cc.protea.util.http.Response)6 HyperwalletException (com.hyperwallet.clientsdk.HyperwalletException)4 IOException (java.io.IOException)4 JOSEException (com.nimbusds.jose.JOSEException)3 ParseException (java.text.ParseException)3 BinaryResponse (cc.protea.util.http.BinaryResponse)1 HttpResponse (org.mockserver.model.HttpResponse)1 Test (org.testng.annotations.Test)1