Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate Jersey dependency #51

Merged
merged 2 commits into from
Oct 2, 2014
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
23 changes: 14 additions & 9 deletions src/main/java/com/paymill/context/PaymillContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.util.Date;
import java.util.Properties;

import com.paymill.utils.HttpClient;
import com.paymill.utils.JerseyClient;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.paymill.services.ClientService;
Expand All @@ -19,8 +20,6 @@
import com.paymill.services.SubscriptionService;
import com.paymill.services.TransactionService;
import com.paymill.services.WebhookService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

/**
* PaymillContecxt loads the context of PAYMILL for a single account, by providing a merchants private key<br />
Expand All @@ -44,7 +43,7 @@ public final class PaymillContext {
public final static ObjectMapper PARSER = new ObjectMapper();
private final static Properties PROPERTIES = new Properties();

private Client httpClient;
private final HttpClient httpClient;

private ClientService clientService;
private OfferService offerService;
Expand Down Expand Up @@ -73,14 +72,20 @@ public PaymillContext( final String apiKey ) {
* of infinity is declared.
*/
public PaymillContext( final String apiKey, Integer timeout ) {
this( new JerseyClient( apiKey, timeout ) );
}

/**
* Creates a PAYMILL context with the given HttpClient implementation.
* @param client
* Http client implementation.
*/
private PaymillContext( final HttpClient client ) {
ConvertUtils.register( new DateConverter( null ), Date.class );
InputStream input = null;

try {
this.httpClient = new Client();
this.httpClient.setReadTimeout( timeout );
this.httpClient.setConnectTimeout( timeout );
this.httpClient.addFilter( new HTTPBasicAuthFilter( apiKey, StringUtils.EMPTY ) );
this.httpClient = client;

this.clientService = this.getPrivateConstructor( ClientService.class ).newInstance( this.httpClient );
this.offerService = this.getPrivateConstructor( OfferService.class ).newInstance( this.httpClient );
Expand Down Expand Up @@ -148,7 +153,7 @@ public WebhookService getWebhookService() {
}

private <T> Constructor<T> getPrivateConstructor( final Class<T> clazz ) throws Exception {
Constructor<T> declaredConstructor = clazz.getDeclaredConstructor( Client.class );
Constructor<T> declaredConstructor = clazz.getDeclaredConstructor( HttpClient.class );
declaredConstructor.setAccessible( true );
return declaredConstructor;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/paymill/services/AbstractService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.paymill.services;

import com.sun.jersey.api.client.Client;
import com.paymill.utils.HttpClient;

class AbstractService {

protected Client httpClient;
protected HttpClient httpClient;

protected AbstractService( Client httpClient ) {
protected AbstractService( HttpClient httpClient ) {
this.httpClient = httpClient;
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/paymill/services/ClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.paymill.utils.HttpClient;
import com.paymill.utils.ParameterMap;
import org.apache.commons.lang3.StringUtils;

import com.paymill.models.Client;
import com.paymill.models.PaymillList;
import com.sun.jersey.core.util.MultivaluedMapImpl;

/**
* The {@link ClientService} is used to list, create, edit, delete and update PAYMILL {@link Client}s.
Expand All @@ -17,7 +16,7 @@
*/
public final class ClientService extends AbstractService {

private ClientService( com.sun.jersey.api.client.Client httpClient ) {
private ClientService( HttpClient httpClient ) {
super( httpClient );
}

Expand Down Expand Up @@ -131,7 +130,7 @@ public Client createWithDescription( String description ) {
* @return {@link Client} object, which represents a PAYMILL client.
*/
public Client createWithEmailAndDescription( String email, String description ) {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
if( StringUtils.isNotBlank( email ) )
params.add( "email", email );
if( StringUtils.isNotBlank( description ) )
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/paymill/services/OfferService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.paymill.models.Client;
import com.paymill.models.Interval;
import com.paymill.models.Offer;
import com.paymill.models.PaymillList;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.paymill.utils.HttpClient;
import com.paymill.utils.ParameterMap;

/**
* The {@link OfferService} is used to list, create, edit, delete and update PAYMILL {@link Offer}s.
Expand All @@ -17,7 +16,7 @@
*/
public class OfferService extends AbstractService {

private OfferService( com.sun.jersey.api.client.Client httpClient ) {
private OfferService( HttpClient httpClient ) {
super( httpClient );
}

Expand Down Expand Up @@ -131,7 +130,7 @@ public Offer create( Integer amount, String currency, Interval.Period interval,
ValidationUtils.validatesName( name );
ValidationUtils.validatesTrialPeriodDays( trialPeriodDays );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
params.add( "amount", String.valueOf( amount ) );
params.add( "currency", currency );
params.add( "interval", interval.toString() );
Expand Down Expand Up @@ -194,7 +193,7 @@ public Offer create( Integer amount, String currency, String interval, String na
* @return the updated offer.
*/
public Offer update( Offer offer, boolean updateSubscriptions ) {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
params.add( "update_subscriptions", String.valueOf( updateSubscriptions ) );
return RestfulUtils.update( OfferService.PATH, offer, params, true, Offer.class, super.httpClient );
}
Expand All @@ -207,7 +206,7 @@ public Offer update( Offer offer, boolean updateSubscriptions ) {
* if true, the plan and all subscriptions associated with it will be deleted. If false, only the plan will be deleted.
*/
public void delete( Offer offer, boolean removeWithSubscriptions ) {
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
params.add( "remove_with_subscriptions", String.valueOf( removeWithSubscriptions ) );
RestfulUtils.delete( OfferService.PATH, offer, params, Offer.class, super.httpClient );
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/paymill/services/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.paymill.models.Client;
import com.paymill.models.Payment;
import com.paymill.models.PaymillList;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.paymill.utils.HttpClient;
import com.paymill.utils.ParameterMap;

/**
* The {@link PaymentService} is used to list, create, edit, delete and update PAYMILL {@link Payment}s.
Expand All @@ -18,7 +17,7 @@ public class PaymentService extends AbstractService {

private final static String PATH = "/payments";

private PaymentService( com.sun.jersey.api.client.Client httpClient ) {
private PaymentService( HttpClient httpClient ) {
super( httpClient );
}

Expand Down Expand Up @@ -102,7 +101,7 @@ public Payment get( String paymentId ) {
public Payment createWithToken( String token ) {
ValidationUtils.validatesToken( token );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
params.add( "token", token );

return RestfulUtils.create( PaymentService.PATH, params, Payment.class, super.httpClient );
Expand Down Expand Up @@ -136,7 +135,7 @@ public Payment createWithTokenAndClient( String token, String clientId ) {
ValidationUtils.validatesToken( token );
ValidationUtils.validatesId( clientId );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();
params.add( "token", token );
params.add( "client", clientId );

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/paymill/services/PreauthorizationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.paymill.utils.HttpClient;
import com.paymill.utils.ParameterMap;
import org.apache.commons.lang3.StringUtils;

import com.paymill.context.PaymillContext;
import com.paymill.models.Payment;
import com.paymill.models.PaymillList;
import com.paymill.models.Preauthorization;
import com.paymill.models.Transaction;
import com.sun.jersey.core.util.MultivaluedMapImpl;

/**
* The {@link PreauthorizationService} is used to list, create and delete PAYMILL {@link Preauthorization}s.
Expand All @@ -22,7 +21,7 @@ public class PreauthorizationService extends AbstractService {

private final static String PATH = "/preauthorizations";

private PreauthorizationService( final com.sun.jersey.api.client.Client httpClient ) {
private PreauthorizationService( final HttpClient httpClient ) {
super( httpClient );
}

Expand Down Expand Up @@ -130,7 +129,7 @@ public Preauthorization createWithToken( final String token, final Integer amoun
ValidationUtils.validatesAmount( amount );
ValidationUtils.validatesCurrency( currency );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();

params.add( "token", token );
params.add( "amount", String.valueOf( amount ) );
Expand Down Expand Up @@ -176,7 +175,7 @@ public Preauthorization createWithPayment( final Payment payment, final Integer
ValidationUtils.validatesAmount( amount );
ValidationUtils.validatesCurrency( currency );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap<String, String>();

params.add( "payment", payment.getId() );
params.add( "amount", String.valueOf( amount ) );
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/paymill/services/RefundService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.paymill.services;

import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;

import com.paymill.models.PaymillList;
import com.paymill.models.Refund;
import com.paymill.models.Transaction;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.paymill.utils.HttpClient;
import com.paymill.utils.ParameterMap;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
* The {@link RefundService} is used to list and create PAYMILL {@link Refund}s.
Expand All @@ -20,7 +18,7 @@ public class RefundService extends AbstractService {

private final static String PATH = "/refunds";

private RefundService( com.sun.jersey.api.client.Client httpClient ) {
private RefundService( HttpClient httpClient ) {
super( httpClient );
}

Expand Down Expand Up @@ -182,7 +180,7 @@ public Refund refundTransaction( String transactionId, Integer amount, String de
public Refund refundTransaction( Transaction transaction, Integer amount, String description ) {
ValidationUtils.validatesAmount( amount );

MultivaluedMap<String, String> params = new MultivaluedMapImpl();
ParameterMap<String, String> params = new ParameterMap();
params.add( "amount", String.valueOf( amount ) );
if( StringUtils.isNotBlank( description ) )
params.add( "description", description );
Expand Down
Loading