diff --git a/src/main/java/com/paymill/context/PaymillContext.java b/src/main/java/com/paymill/context/PaymillContext.java index 990b3e6..6de0051 100644 --- a/src/main/java/com/paymill/context/PaymillContext.java +++ b/src/main/java/com/paymill/context/PaymillContext.java @@ -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; @@ -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
@@ -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; @@ -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 ); @@ -148,7 +153,7 @@ public WebhookService getWebhookService() { } private Constructor getPrivateConstructor( final Class clazz ) throws Exception { - Constructor declaredConstructor = clazz.getDeclaredConstructor( Client.class ); + Constructor declaredConstructor = clazz.getDeclaredConstructor( HttpClient.class ); declaredConstructor.setAccessible( true ); return declaredConstructor; } diff --git a/src/main/java/com/paymill/services/AbstractService.java b/src/main/java/com/paymill/services/AbstractService.java index b1a3c07..dfd1742 100644 --- a/src/main/java/com/paymill/services/AbstractService.java +++ b/src/main/java/com/paymill/services/AbstractService.java @@ -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; } diff --git a/src/main/java/com/paymill/services/ClientService.java b/src/main/java/com/paymill/services/ClientService.java index e1cab4b..87c0a42 100644 --- a/src/main/java/com/paymill/services/ClientService.java +++ b/src/main/java/com/paymill/services/ClientService.java @@ -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. @@ -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 ); } @@ -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 params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); if( StringUtils.isNotBlank( email ) ) params.add( "email", email ); if( StringUtils.isNotBlank( description ) ) diff --git a/src/main/java/com/paymill/services/OfferService.java b/src/main/java/com/paymill/services/OfferService.java index 2887f11..712661b 100644 --- a/src/main/java/com/paymill/services/OfferService.java +++ b/src/main/java/com/paymill/services/OfferService.java @@ -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. @@ -17,7 +16,7 @@ */ public class OfferService extends AbstractService { - private OfferService( com.sun.jersey.api.client.Client httpClient ) { + private OfferService( HttpClient httpClient ) { super( httpClient ); } @@ -131,7 +130,7 @@ public Offer create( Integer amount, String currency, Interval.Period interval, ValidationUtils.validatesName( name ); ValidationUtils.validatesTrialPeriodDays( trialPeriodDays ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); params.add( "interval", interval.toString() ); @@ -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 params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "update_subscriptions", String.valueOf( updateSubscriptions ) ); return RestfulUtils.update( OfferService.PATH, offer, params, true, Offer.class, super.httpClient ); } @@ -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 params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "remove_with_subscriptions", String.valueOf( removeWithSubscriptions ) ); RestfulUtils.delete( OfferService.PATH, offer, params, Offer.class, super.httpClient ); } diff --git a/src/main/java/com/paymill/services/PaymentService.java b/src/main/java/com/paymill/services/PaymentService.java index fbf7c7c..460509e 100644 --- a/src/main/java/com/paymill/services/PaymentService.java +++ b/src/main/java/com/paymill/services/PaymentService.java @@ -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. @@ -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 ); } @@ -102,7 +101,7 @@ public Payment get( String paymentId ) { public Payment createWithToken( String token ) { ValidationUtils.validatesToken( token ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); return RestfulUtils.create( PaymentService.PATH, params, Payment.class, super.httpClient ); @@ -136,7 +135,7 @@ public Payment createWithTokenAndClient( String token, String clientId ) { ValidationUtils.validatesToken( token ); ValidationUtils.validatesId( clientId ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "client", clientId ); diff --git a/src/main/java/com/paymill/services/PreauthorizationService.java b/src/main/java/com/paymill/services/PreauthorizationService.java index 6e940d9..86d4833 100644 --- a/src/main/java/com/paymill/services/PreauthorizationService.java +++ b/src/main/java/com/paymill/services/PreauthorizationService.java @@ -2,8 +2,8 @@ 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; @@ -11,7 +11,6 @@ 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. @@ -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 ); } @@ -130,7 +129,7 @@ public Preauthorization createWithToken( final String token, final Integer amoun ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "amount", String.valueOf( amount ) ); @@ -176,7 +175,7 @@ public Preauthorization createWithPayment( final Payment payment, final Integer ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "amount", String.valueOf( amount ) ); diff --git a/src/main/java/com/paymill/services/RefundService.java b/src/main/java/com/paymill/services/RefundService.java index 71adf7f..5348a13 100644 --- a/src/main/java/com/paymill/services/RefundService.java +++ b/src/main/java/com/paymill/services/RefundService.java @@ -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. @@ -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 ); } @@ -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 params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); if( StringUtils.isNotBlank( description ) ) params.add( "description", description ); diff --git a/src/main/java/com/paymill/services/RestfulUtils.java b/src/main/java/com/paymill/services/RestfulUtils.java index cc38769..a65e8db 100644 --- a/src/main/java/com/paymill/services/RestfulUtils.java +++ b/src/main/java/com/paymill/services/RestfulUtils.java @@ -5,8 +5,8 @@ import java.util.ArrayList; import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; @@ -17,17 +17,13 @@ import com.paymill.models.PaymillList; import com.paymill.models.SnakeCase; import com.paymill.models.Updateable; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.core.util.MultivaluedMapImpl; final class RestfulUtils { private final static String ENDPOINT = "https://api.paymill.com/v2.1"; - static PaymillList list( String path, Object filter, Object order, Integer count, Integer offset, Class clazz, Client httpClient ) { - MultivaluedMap params = RestfulUtils.prepareFilterParameters( filter ); + static PaymillList list( String path, Object filter, Object order, Integer count, Integer offset, Class clazz, HttpClient httpClient ) { + ParameterMap params = RestfulUtils.prepareFilterParameters( filter ); String param = RestfulUtils.prepareOrderParameter( order ); if( StringUtils.isNotBlank( param ) && !StringUtils.startsWith( param, "_" ) ) { params.add( "order", param ); @@ -38,44 +34,44 @@ static PaymillList list( String path, Object filter, Object order, Intege if( offset != null && offset >= 0 ) { params.add( "offset", String.valueOf( offset ) ); } - return RestfulUtils.deserializeList( RestfulUtils.get( path, params, httpClient ), clazz ); + return RestfulUtils.deserializeList( httpClient.get( ENDPOINT + path, params ), clazz ); } - static T show( String path, T target, Class clazz, Client httpClient ) { + static T show( String path, T target, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.get( path + "/" + id, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.get( ENDPOINT + path + "/" + id ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T create( String path, MultivaluedMap params, Class clazz, Client httpClient ) { - return RestfulUtils.deserializeObject( RestfulUtils.post( path, params, httpClient ), clazz ); + static T create( String path, ParameterMap params, Class clazz, HttpClient httpClient ) { + return RestfulUtils.deserializeObject( httpClient.post( ENDPOINT + path, params ), clazz ); } - static T update( String path, T target, Class clazz, Client httpClient ) { - MultivaluedMap params = RestfulUtils.prepareEditableParameters( target ); + static T update( String path, T target, Class clazz, HttpClient httpClient ) { + ParameterMap params = RestfulUtils.prepareEditableParameters( target ); String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.put( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.put( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T update( String path, T target, MultivaluedMap params, boolean includeTargetUpdateables, Class clazz, Client httpClient ) { + static T update( String path, T target, ParameterMap params, boolean includeTargetUpdateables, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); if( includeTargetUpdateables ) { params.putAll( RestfulUtils.prepareEditableParameters( target ) ); } - T source = RestfulUtils.deserializeObject( RestfulUtils.put( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.put( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T delete( String path, T target, MultivaluedMap params, Class clazz, Client httpClient ) { + static T delete( String path, T target, ParameterMap params, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.delete( path + "/" + id, params, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.delete( ENDPOINT + path + "/" + id, params ), clazz ); return RestfulUtils.refreshInstance( source, target ); } - static T delete( String path, T target, Class clazz, Client httpClient ) { + static T delete( String path, T target, Class clazz, HttpClient httpClient ) { String id = RestfulUtils.getIdByReflection( target ); - T source = RestfulUtils.deserializeObject( RestfulUtils.delete( path + "/" + id, null, httpClient ), clazz ); + T source = RestfulUtils.deserializeObject( httpClient.delete( ENDPOINT + path + "/" + id, null ), clazz ); return RestfulUtils.refreshInstance( source, target ); } @@ -141,8 +137,9 @@ private static PaymillList deserializeList( String content, Class claz return null; } - private static MultivaluedMap prepareEditableParameters( Object instance ) { - MultivaluedMap params = new MultivaluedMapImpl(); + private static ParameterMap prepareEditableParameters( Object instance ) { + ParameterMap params = new ParameterMap(); + for( Field field : instance.getClass().getDeclaredFields() ) { Updateable updateable = field.getAnnotation( Updateable.class ); if( updateable != null ) { @@ -176,38 +173,9 @@ private static MultivaluedMap prepareEditableParameters( Object return params; } - private static String get( String path, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.get( ClientResponse.class ); - return response.getEntity( String.class ); - } - - private static String get( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ).queryParams( params ); - ClientResponse response = webResource.get( ClientResponse.class ); - return response.getEntity( String.class ); - } - - private static String post( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.post( ClientResponse.class, params ); - return response.getEntity( String.class ); - } - - private static String put( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.put( ClientResponse.class, params ); - return response.getEntity( String.class ); - } - - private static String delete( String path, MultivaluedMap params, Client httpClient ) { - WebResource webResource = httpClient.resource( RestfulUtils.ENDPOINT + path ); - ClientResponse response = webResource.delete( ClientResponse.class, params ); - return response.getEntity( String.class ); - } + private static ParameterMap prepareFilterParameters( Object instance ) { + ParameterMap params = new ParameterMap(); - private static MultivaluedMap prepareFilterParameters( Object instance ) { - MultivaluedMap params = new MultivaluedMapImpl(); if( instance == null ) return params; try { diff --git a/src/main/java/com/paymill/services/SubscriptionService.java b/src/main/java/com/paymill/services/SubscriptionService.java index 1be6435..b600209 100644 --- a/src/main/java/com/paymill/services/SubscriptionService.java +++ b/src/main/java/com/paymill/services/SubscriptionService.java @@ -3,8 +3,6 @@ import java.util.Date; 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; @@ -12,7 +10,8 @@ import com.paymill.models.PaymillList; import com.paymill.models.Subscription; import com.paymill.models.Subscription.Creator; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link SubscriptionService} is used to list, create, edit, delete and update PAYMILL {@link Subscription}s. @@ -23,7 +22,7 @@ public class SubscriptionService extends AbstractService { private final static String PATH = "/subscriptions"; - private SubscriptionService( com.sun.jersey.api.client.Client httpClient ) { + private SubscriptionService( HttpClient httpClient ) { super( httpClient ); } @@ -152,7 +151,7 @@ public Subscription create( Payment payment, Client client, Offer offer, Integer throw new IllegalArgumentException( "Either an offer or amount, currency and interval must be set, when creating a subscription" ); } - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); ValidationUtils.validatesPayment( payment ); params.add( "payment", payment.getId() ); if( client != null ) { @@ -229,7 +228,7 @@ public Subscription create( String paymentId, String clientId, String offerId, I * @return the updated subscription */ public Subscription pause( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "pause", String.valueOf( true ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -260,7 +259,7 @@ public Subscription pause( String subscriptionId ) { * @return the updated subscription */ public Subscription unpause( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "pause", String.valueOf( false ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -371,7 +370,7 @@ public Subscription changeAmountTemporary( String subscriptionId, Integer amount } private Subscription changeAmount( Subscription subscription, Integer amount, Integer type, String currency, Interval.PeriodWithChargeDay interval ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "amount", String.valueOf( amount ) ); params.add( "amount_change_type", String.valueOf( type ) ); if( currency != null ) { @@ -438,7 +437,7 @@ public Subscription changeOfferKeepCaptureDateNoRefund( Subscription subscriptio private Subscription changeOffer( Subscription subscription, Offer offer, Integer type ) { ValidationUtils.validatesOffer( offer ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "offer", offer.getId() ); params.add( "offer_change_type", String.valueOf( type ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -451,7 +450,7 @@ private Subscription changeOffer( Subscription subscription, Offer offer, Intege * @return the updated subscription. */ public Subscription endTrial( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "trial_end", String.valueOf( false ) ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); } @@ -465,7 +464,7 @@ public Subscription endTrial( Subscription subscription ) { * @return the updated subscription. */ public Subscription limitValidity( Subscription subscription, Interval.Period newValidity ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); ValidationUtils.validatesIntervalPeriod( newValidity ); params.add( "period_of_validity", newValidity.toString() ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -490,7 +489,7 @@ public Subscription limitValidity( Subscription subscription, String newValidity * @return the updated subscription. */ public Subscription unlimitValidity( Subscription subscription ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "period_of_validity", "remove" ); return RestfulUtils.update( SubscriptionService.PATH, subscription, params, false, Subscription.class, super.httpClient ); @@ -541,7 +540,7 @@ public Subscription cancel( String subscriptionId ) { } private Subscription delete( Subscription subscription, boolean remove ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "remove", String.valueOf( remove ) ); return RestfulUtils.delete( SubscriptionService.PATH, subscription, params, Subscription.class, super.httpClient ); } diff --git a/src/main/java/com/paymill/services/TransactionService.java b/src/main/java/com/paymill/services/TransactionService.java index eb70af3..e08f7ef 100644 --- a/src/main/java/com/paymill/services/TransactionService.java +++ b/src/main/java/com/paymill/services/TransactionService.java @@ -2,8 +2,8 @@ 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; @@ -13,7 +13,6 @@ import com.paymill.models.PaymillList; import com.paymill.models.Preauthorization; import com.paymill.models.Transaction; -import com.sun.jersey.core.util.MultivaluedMapImpl; /** * The {@link TransactionService} is used to list, create, edit and update PAYMILL {@link Transaction}s. @@ -24,7 +23,7 @@ public class TransactionService extends AbstractService { private final static String PATH = "/transactions"; - private TransactionService( com.sun.jersey.api.client.Client httpClient ) { + private TransactionService( HttpClient httpClient ) { super( httpClient ); } @@ -165,7 +164,7 @@ public Transaction createWithTokenAndFee( String token, Integer amount, String c ValidationUtils.validatesCurrency( currency ); ValidationUtils.validatesFee( fee ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "token", token ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); @@ -216,7 +215,7 @@ public Transaction createWithPayment( Payment payment, Integer amount, String cu ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); @@ -296,7 +295,7 @@ public Transaction createWithPaymentAndClient( Payment payment, Client client, I ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "payment", payment.getId() ); params.add( "client", client.getId() ); params.add( "amount", String.valueOf( amount ) ); @@ -388,7 +387,7 @@ public Transaction createWithPreauthorization( String preauthorizationId, Intege ValidationUtils.validatesAmount( amount ); ValidationUtils.validatesCurrency( currency ); - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "preauthorization", preauthorizationId ); params.add( "amount", String.valueOf( amount ) ); params.add( "currency", currency ); diff --git a/src/main/java/com/paymill/services/WebhookService.java b/src/main/java/com/paymill/services/WebhookService.java index 5cea454..d027325 100644 --- a/src/main/java/com/paymill/services/WebhookService.java +++ b/src/main/java/com/paymill/services/WebhookService.java @@ -2,13 +2,11 @@ import java.util.List; -import javax.ws.rs.core.MultivaluedMap; - import com.paymill.models.PaymillList; import com.paymill.models.Webhook; import com.paymill.models.Webhook.EventType; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.paymill.utils.HttpClient; +import com.paymill.utils.ParameterMap; /** * The {@link WebhookService} is used to list, create, edit and update PAYMILL {@link Webhook}s. @@ -19,7 +17,7 @@ public class WebhookService extends AbstractService { private final static String PATH = "/webhooks"; - private WebhookService( Client httpClient ) { + private WebhookService( HttpClient httpClient ) { super( httpClient ); } @@ -103,7 +101,7 @@ public Webhook get( String webhookId ) { * @return A {@link Webhook} */ public Webhook createUrlWebhook( String url, Webhook.EventType[] eventTypes ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "url", url ); for( Webhook.EventType eventType : eventTypes ) @@ -121,7 +119,7 @@ public Webhook createUrlWebhook( String url, Webhook.EventType[] eventTypes ) { * @return A {@link Webhook} */ public Webhook createEmailWebhook( String email, Webhook.EventType[] eventTypes ) { - MultivaluedMap params = new MultivaluedMapImpl(); + ParameterMap params = new ParameterMap(); params.add( "email", email ); for( Webhook.EventType eventType : eventTypes ) diff --git a/src/main/java/com/paymill/utils/HttpClient.java b/src/main/java/com/paymill/utils/HttpClient.java new file mode 100644 index 0000000..c9a2530 --- /dev/null +++ b/src/main/java/com/paymill/utils/HttpClient.java @@ -0,0 +1,13 @@ +package com.paymill.utils; + +public interface HttpClient { + public String get(String path); + + public String get(String path, ParameterMap params); + + public String post(String path, ParameterMap params); + + public String put(String path, ParameterMap params); + + public String delete(String path, ParameterMap params); +} diff --git a/src/main/java/com/paymill/utils/JerseyClient.java b/src/main/java/com/paymill/utils/JerseyClient.java new file mode 100644 index 0000000..61806cf --- /dev/null +++ b/src/main/java/com/paymill/utils/JerseyClient.java @@ -0,0 +1,69 @@ +package com.paymill.utils; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.util.MultivaluedMapImpl; +import org.apache.commons.lang3.StringUtils; + +import javax.ws.rs.core.MultivaluedMap; + +public final class JerseyClient implements HttpClient { + private final Client httpClient; + + public JerseyClient( String apiKey ) { + this( apiKey, null ); + } + + public JerseyClient( String apiKey, Integer timeout ) { + this.httpClient = new Client(); + this.httpClient.setReadTimeout( timeout ); + this.httpClient.setConnectTimeout( timeout ); + this.httpClient.addFilter( new HTTPBasicAuthFilter( apiKey, StringUtils.EMPTY ) ); + } + + @Override + public String get( String path ) { + WebResource webResource = httpClient.resource( path ); + ClientResponse response = webResource.get( ClientResponse.class ); + return response.getEntity( String.class ); + } + + @Override + public String get( String path, ParameterMap params ) { + WebResource webResource = httpClient.resource( path ).queryParams( convertMap( params ) ); + ClientResponse response = webResource.get( ClientResponse.class ); + return response.getEntity( String.class ); + } + + @Override + public String post( String path, ParameterMap params ) { + WebResource webResource = httpClient.resource( path ); + ClientResponse response = webResource.post( ClientResponse.class, convertMap( params ) ); + return response.getEntity( String.class ); + } + + @Override + public String put( String path, ParameterMap params ) { + WebResource webResource = httpClient.resource( path ); + ClientResponse response = webResource.put( ClientResponse.class, convertMap( params ) ); + return response.getEntity( String.class ); + } + + @Override + public String delete( String path, ParameterMap params ) { + WebResource webResource = httpClient.resource( path ); + ClientResponse response = webResource.delete( ClientResponse.class, convertMap( params ) ); + return response.getEntity( String.class ); + } + + private static MultivaluedMap convertMap(ParameterMap map) { + if( map == null ) { + return null; + } + MultivaluedMap params = new MultivaluedMapImpl(); + params.putAll( map ); + return params; + } +} diff --git a/src/main/java/com/paymill/utils/ParameterMap.java b/src/main/java/com/paymill/utils/ParameterMap.java new file mode 100644 index 0000000..693f68b --- /dev/null +++ b/src/main/java/com/paymill/utils/ParameterMap.java @@ -0,0 +1,87 @@ +package com.paymill.utils; + +import java.util.*; + +public final class ParameterMap implements Map> { + private final HashMap> map; + + public ParameterMap() { + this.map = new HashMap>(); + } + + public void add( K key, V value ) { + List values = this.map.get( key ); + + if(values == null) { + values = new ArrayList(); + } + + values.add(value); + + this.map.put( key, values ); + } + + public V getFirst(K key) { + return this.get( key ) != null ? this.get( key ).get(0) : null; + } + + @Override + public int size() { + return this.map.size(); + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public boolean containsKey(Object o) { + return this.map.containsKey( o ); + } + + @Override + public boolean containsValue(Object o) { + return this.map.containsValue( o ); + } + + @Override + public List get(Object o) { + return this.map.get( o ); + } + + @Override + public List put(K k, List vs) { + return this.map.put( k, vs); + } + + @Override + public List remove(Object o) { + return this.map.remove( o ); + } + + @Override + public void putAll(Map> map) { + this.map.putAll( map ); + } + + @Override + public void clear() { + this.map.clear(); + } + + @Override + public Set keySet() { + return this.map.keySet(); + } + + @Override + public Collection> values() { + return this.map.values(); + } + + @Override + public Set>> entrySet() { + return this.map.entrySet(); + } +}