Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api30.sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>br.cielo.cieloecommerce</groupId>
<artifactId>api30.sdk</artifactId>
<version>0.0.4-SNAPSHOT</version>
<version>0.0.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>api30.sdk</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cieloecommerce.sdk.Merchant;
import cieloecommerce.sdk.ecommerce.request.*;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;

Expand All @@ -12,7 +12,7 @@
public class CieloEcommerce {
private final Merchant merchant;
private final Environment environment;
private HttpClient httpClient;
private CloseableHttpClient httpClient;

/**
* Create an instance of CieloEcommerce choosing the environment where the
Expand All @@ -39,7 +39,7 @@ public CieloEcommerce(Merchant merchant) {
this(merchant, Environment.PRODUCTION);
}

public void setHttpClient(HttpClient httpClient) {
public void setHttpClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public Sale createSale(Sale sale) throws IOException, CieloRequestException {

/**
* Create a card token to be stored on store
*
*
* @param cardToken
* The credit card data
* @return The card token
Expand Down Expand Up @@ -112,7 +112,7 @@ public Sale querySale(String paymentId) throws IOException, CieloRequestExceptio

return sale;
}

/**
* Query a Sale on Cielo by paymentId
*
Expand All @@ -136,7 +136,7 @@ public QueryMerchantOrderResponse queryMerchantOrder(String merchantOrderId) thr
return merchantOrder;
}



/**
* Query a RecurrentSale on Cielo by recurrentPaymentId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package cieloecommerce.sdk.ecommerce.request;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.UUID;
import java.util.zip.GZIPInputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import com.google.gson.Gson;
import cieloecommerce.sdk.Environment;
import cieloecommerce.sdk.Merchant;
import org.apache.http.util.EntityUtils;

/**
* Abstraction to reuse most of the code that send and receive the HTTP
Expand All @@ -25,7 +21,7 @@
public abstract class AbstractSaleRequest<Request, Response> {
final Environment environment;
private final Merchant merchant;
private HttpClient httpClient;
private CloseableHttpClient httpClient;

AbstractSaleRequest(Merchant merchant, Environment environment) {
this.merchant = merchant;
Expand All @@ -34,7 +30,7 @@ public abstract class AbstractSaleRequest<Request, Response> {

public abstract Response execute(Request param) throws IOException, CieloRequestException;

public void setHttpClient(HttpClient httpClient) {
public void setHttpClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}

Expand All @@ -48,7 +44,7 @@ public void setHttpClient(HttpClient httpClient) {
* @throws IOException
* yeah, deal with it
*/
HttpResponse sendRequest(HttpUriRequest request) throws IOException {
CloseableHttpResponse sendRequest(HttpUriRequest request) throws IOException {
if (httpClient == null) {
httpClient = HttpClientBuilder.create().build();
}
Expand All @@ -67,33 +63,22 @@ HttpResponse sendRequest(HttpUriRequest request) throws IOException {
/**
* Read the response body sent by Cielo
*
* @param response
* HttpResponse by Cielo, with headers, status code, etc.
* @param response HttpResponse by Cielo, with headers, status code, etc.
* @return An instance of Sale with the response entity sent by Cielo.
* @throws IOException
* yeah, deal with it
* @throws IOException yeah, deal with it
* @throws CieloRequestException
*/
Response readResponse(HttpResponse response, Class<Response> responseClassOf)
throws IOException, CieloRequestException {
HttpEntity responseEntity = response.getEntity();
InputStream responseEntityContent = responseEntity.getContent();

Header contentEncoding = response.getFirstHeader("Content-Encoding");

if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
responseEntityContent = new GZIPInputStream(responseEntityContent);
Response readResponse(CloseableHttpResponse response, Class<Response> responseClassOf)
throws IOException, CieloRequestException {
try {
HttpEntity responseEntity = response.getEntity();
final String responseBody = EntityUtils.toString(responseEntity);
EntityUtils.consume(responseEntity);
return parseResponse(response.getStatusLine().getStatusCode(), responseBody,
responseClassOf);
} finally {
response.close();
}

BufferedReader responseReader = new BufferedReader(new InputStreamReader(responseEntityContent));
StringBuilder responseBuilder = new StringBuilder();
String line;

while ((line = responseReader.readLine()) != null) {
responseBuilder.append(line);
}

return parseResponse(response.getStatusLine().getStatusCode(), responseBuilder.toString(), responseClassOf);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

Expand All @@ -24,7 +24,7 @@ public CardToken execute(CardToken param) throws IOException, CieloRequestExcept

request.setEntity(new StringEntity(new GsonBuilder().create().toJson(param)));

HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, CardToken.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;

import org.apache.commons.codec.Charsets;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
Expand All @@ -29,7 +29,7 @@ public Sale execute(Sale param) throws IOException, CieloRequestException {

request.setEntity(new StringEntity(new GsonBuilder().create().toJson(param),
ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), Charsets.UTF_8.name())));
HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, Sale.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cieloecommerce.sdk.Environment;
import cieloecommerce.sdk.Merchant;
import cieloecommerce.sdk.ecommerce.RecurrentSale;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;

import java.io.IOException;
Expand All @@ -18,7 +18,7 @@ public RecurrentSale execute(String recurrentPaymentId) throws IOException, Ciel
String url = environment.getApiUrl() + "1/RecurrentPayment/" + recurrentPaymentId + "/Deactivate";

HttpPut request = new HttpPut(url);
HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, RecurrentSale.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

import cieloecommerce.sdk.Environment;
Expand All @@ -22,9 +22,9 @@ public QueryMerchantOrderResponse execute(String merchnatOrderId) throws IOExcep
String url = environment.getApiQueryURL() + "1/sales?merchantOrderId=" + merchnatOrderId;

HttpGet request = new HttpGet(url);
HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, QueryMerchantOrderResponse.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

import cieloecommerce.sdk.Environment;
Expand All @@ -19,7 +19,7 @@ public RecurrentSale execute(String recurrentPaymentId) throws IOException, Ciel
String url = environment.getApiQueryURL() + "1/RecurrentPayment/" + recurrentPaymentId;

HttpGet request = new HttpGet(url);
HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, RecurrentSale.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

import cieloecommerce.sdk.Environment;
Expand All @@ -22,7 +22,7 @@ public Sale execute(String paymentId) throws IOException, CieloRequestException
String url = environment.getApiQueryURL() + "1/sales/" + paymentId;

HttpGet request = new HttpGet(url);
HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

return readResponse(response, Sale.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cieloecommerce.sdk.Environment;
import cieloecommerce.sdk.Merchant;
import cieloecommerce.sdk.ecommerce.SaleResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;

Expand Down Expand Up @@ -43,7 +43,7 @@ public SaleResponse execute(String paymentId) throws IOException, CieloRequestEx

request = new HttpPut(builder.build().toString());

HttpResponse response = sendRequest(request);
CloseableHttpResponse response = sendRequest(request);

sale = readResponse(response, SaleResponse.class);
} catch (URISyntaxException e) {
Expand Down