Search in sources :

Example 16 with CommunicationException

use of com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException in project connect-sdk-client-android by Ingenico-ePayments.

the class C2sCommunicator method doHTTPPostRequest.

/**
 * Does a POST request with HttpClient
 *
 * @param location, url where the request is sent to
 * @param clientSessionId, used for identification on the GC gateway
 * @param metadata, map filled with metadata, which is added to the request
 * @param postBody, the content of the postbody
 *
 * @return HttpURLConnection, which contains the response of the request
 *
 * @throws CommunicationException
 */
private HttpURLConnection doHTTPPostRequest(String location, String clientSessionId, Map<String, String> metadata, String postBody) throws CommunicationException {
    // Initialize the connection
    OutputStreamWriter writer = null;
    try {
        URL url = new URL(location);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // Set request method to POST
        connection.setRequestMethod("POST");
        // Add json header
        connection.addRequestProperty("Content-Type", "application/json");
        // Add sessionId header
        if (clientSessionId != null) {
            connection.addRequestProperty(HTTP_HEADER_SESSION_ID, "GCS v1Client:" + clientSessionId);
        }
        // Add metadata header
        if (metadata != null) {
            connection.addRequestProperty(HTTP_HEADER_METADATA, GcUtil.getBase64EncodedMetadata(metadata));
        }
        // Log the request
        if (Constants.ENABLE_REQUEST_LOGGING) {
            logRequest(connection, postBody);
        }
        // Add post body
        connection.setDoOutput(true);
        writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(postBody);
        writer.flush();
        // Check if the response code is HTTP_OK
        if (connection.getResponseCode() != 200) {
            throw new CommunicationException("No status 200 received, status is :" + connection.getResponseCode());
        }
        return connection;
    } catch (MalformedURLException e) {
        Log.e(TAG, "doHTTPPostRequest, Unable to parse url " + location);
        throw new CommunicationException("Unable to parse url " + location);
    } catch (IOException e) {
        Log.e(TAG, "doHTTPPostRequest, IOException while opening connection " + e.getMessage());
        throw new CommunicationException("IOException while opening connection " + e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                Log.i(TAG, "doHTTPPostRequest, IOException while closing connection " + e.getMessage());
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) CommunicationException(com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

CommunicationException (com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException)16 IOException (java.io.IOException)13 HttpURLConnection (java.net.HttpURLConnection)13 MalformedURLException (java.net.MalformedURLException)13 InvalidParameterException (java.security.InvalidParameterException)11 Scanner (java.util.Scanner)11 Drawable (android.graphics.drawable.Drawable)2 AssetManager (com.globalcollect.gateway.sdk.client.android.sdk.manager.AssetManager)2 ApiException (com.ingenico.connect.gateway.sdk.java.ApiException)2 URL (java.net.URL)2 ConvertedAmountResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.ConvertedAmountResponse)1 CustomerDetailsRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsRequest)1 CustomerDetailsResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsResponse)1 PaymentRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.PaymentRequest)1 PreparedPaymentRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.PreparedPaymentRequest)1 BasicPaymentProduct (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProduct)1 BasicPaymentProductGroup (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProductGroup)1 BasicPaymentProductGroups (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProductGroups)1 BasicPaymentProducts (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProducts)1 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)1