Search in sources :

Example 1 with APIConnectionException

use of com.stripe.exception.APIConnectionException in project stripe-java by stripe.

the class LiveStripeResponseGetter method getMultipartStripeResponse.

private static StripeResponse getMultipartStripeResponse(APIResource.RequestMethod method, String url, Map<String, Object> params, RequestOptions options) throws InvalidRequestException, APIConnectionException, APIException {
    if (method != APIResource.RequestMethod.POST) {
        throw new InvalidRequestException("Multipart requests for HTTP methods other than POST " + "are currently not supported.", null, null, null, 0, null);
    }
    java.net.HttpURLConnection conn = null;
    try {
        conn = createStripeConnection(url, options);
        String boundary = MultipartProcessor.getBoundary();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", String.format("multipart/form-data; boundary=%s", boundary));
        MultipartProcessor multipartProcessor = null;
        try {
            multipartProcessor = new MultipartProcessor(conn, boundary, APIResource.CHARSET);
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof File) {
                    File currentFile = (File) value;
                    if (!currentFile.exists()) {
                        throw new InvalidRequestException("File for key " + key + " must exist.", null, null, null, 0, null);
                    } else if (!currentFile.isFile()) {
                        throw new InvalidRequestException("File for key " + key + " must be a file and not a directory.", null, null, null, 0, null);
                    } else if (!currentFile.canRead()) {
                        throw new InvalidRequestException("Must have read permissions on file for key " + key + ".", null, null, null, 0, null);
                    }
                    multipartProcessor.addFileField(key, currentFile);
                } else {
                    // We only allow a single level of nesting for params
                    // for multipart
                    multipartProcessor.addFormField(key, (String) value);
                }
            }
        } finally {
            if (multipartProcessor != null) {
                multipartProcessor.finish();
            }
        }
        // trigger the request
        int responseCode = conn.getResponseCode();
        String responseBody;
        Map<String, List<String>> headers;
        if (responseCode >= 200 && responseCode < 300) {
            responseBody = getResponseBody(conn.getInputStream());
        } else {
            responseBody = getResponseBody(conn.getErrorStream());
        }
        headers = conn.getHeaderFields();
        return new StripeResponse(responseCode, responseBody, headers);
    } catch (IOException e) {
        throw new APIConnectionException(String.format("IOException during API request to Stripe (%s): %s " + "Please check your internet connection and try again. If this problem persists," + "you should check Stripe's service status at https://twitter.com/stripestatus," + " or let us know at support@stripe.com.", Stripe.getApiBase(), e.getMessage()), e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) APIConnectionException(com.stripe.exception.APIConnectionException) IOException(java.io.IOException) InvalidRequestException(com.stripe.exception.InvalidRequestException) StripeObject(com.stripe.model.StripeObject) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 2 with APIConnectionException

use of com.stripe.exception.APIConnectionException in project stripe-java by stripe.

the class LiveStripeResponseGetter method makeURLConnectionRequest.

private static StripeResponse makeURLConnectionRequest(APIResource.RequestMethod method, String url, String query, RequestOptions options) throws APIConnectionException {
    java.net.HttpURLConnection conn = null;
    try {
        switch(method) {
            case GET:
                conn = createGetConnection(url, query, options);
                break;
            case POST:
                conn = createPostConnection(url, query, options);
                break;
            case DELETE:
                conn = createDeleteConnection(url, query, options);
                break;
            default:
                throw new APIConnectionException(String.format("Unrecognized HTTP method %s. " + "This indicates a bug in the Stripe bindings. Please contact " + "support@stripe.com for assistance.", method));
        }
        // trigger the request
        int responseCode = conn.getResponseCode();
        String responseBody;
        Map<String, List<String>> headers;
        if (responseCode >= 200 && responseCode < 300) {
            responseBody = getResponseBody(conn.getInputStream());
        } else {
            responseBody = getResponseBody(conn.getErrorStream());
        }
        headers = conn.getHeaderFields();
        return new StripeResponse(responseCode, responseBody, headers);
    } catch (IOException e) {
        throw new APIConnectionException(String.format("IOException during API request to Stripe (%s): %s " + "Please check your internet connection and try again. If this problem persists," + "you should check Stripe's service status at https://twitter.com/stripestatus," + " or let us know at support@stripe.com.", Stripe.getApiBase(), e.getMessage()), e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) APIConnectionException(com.stripe.exception.APIConnectionException) LinkedList(java.util.LinkedList) List(java.util.List) IOException(java.io.IOException)

Aggregations

APIConnectionException (com.stripe.exception.APIConnectionException)2 IOException (java.io.IOException)2 HttpURLConnection (java.net.HttpURLConnection)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 InvalidRequestException (com.stripe.exception.InvalidRequestException)1 StripeObject (com.stripe.model.StripeObject)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1