killbill-memoizeit

payment: avoid double dispatching in DefaultPaymentGatewayApi Signed-off-by:

12/1/2015 6:16:14 PM

Details

diff --git a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentGatewayApi.java b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentGatewayApi.java
index 22fcc42..a069b5e 100644
--- a/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentGatewayApi.java
+++ b/payment/src/main/java/org/killbill/billing/payment/api/DefaultPaymentGatewayApi.java
@@ -73,13 +73,7 @@ public class DefaultPaymentGatewayApi extends DefaultApiBase implements PaymentG
 
     @Override
     public HostedPaymentPageFormDescriptor buildFormDescriptor(final Account account, final UUID paymentMethodId, final Iterable<PluginProperty> customFields, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
-        final UUID paymentMethodIdToUse = paymentMethodId != null ? paymentMethodId : account.getPaymentMethodId();
-
-        if (paymentMethodId == null) {
-            throw new PaymentApiException(ErrorCode.PAYMENT_INVALID_PARAMETER, paymentMethodId, "should not be null");
-        }
-
-        return paymentGatewayProcessor.buildFormDescriptor(account, paymentMethodIdToUse, customFields, properties, callContext, internalCallContextFactory.createInternalCallContext(account.getId(), callContext));
+        return buildFormDescriptor(true, account, paymentMethodId, customFields, properties, callContext);
     }
 
     @Override
@@ -87,14 +81,14 @@ public class DefaultPaymentGatewayApi extends DefaultApiBase implements PaymentG
         return executeWithPaymentControl(account, paymentMethodId, properties, paymentOptions, callContext, paymentPluginFormDispatcher, new WithPaymentControlCallback<HostedPaymentPageFormDescriptor>() {
             @Override
             public HostedPaymentPageFormDescriptor doPaymentGatewayApiOperation(final UUID adjustedPaymentMethodId, final Iterable<PluginProperty> adjustedPluginProperties) throws PaymentApiException {
-                return buildFormDescriptor(account, adjustedPaymentMethodId, customFields, adjustedPluginProperties, callContext);
+                return buildFormDescriptor(false, account, adjustedPaymentMethodId, customFields, adjustedPluginProperties, callContext);
             }
         });
     }
 
     @Override
     public GatewayNotification processNotification(final String notification, final String pluginName, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
-        return paymentGatewayProcessor.processNotification(notification, pluginName, properties, callContext);
+        return paymentGatewayProcessor.processNotification(true, notification, pluginName, properties, callContext);
     }
 
     @Override
@@ -103,14 +97,23 @@ public class DefaultPaymentGatewayApi extends DefaultApiBase implements PaymentG
             @Override
             public GatewayNotification doPaymentGatewayApiOperation(final UUID adjustedPaymentMethodId, final Iterable<PluginProperty> adjustedPluginProperties) throws PaymentApiException {
                 if (adjustedPaymentMethodId == null) {
-                    return paymentGatewayProcessor.processNotification(notification, pluginName, properties, callContext);
+                    return paymentGatewayProcessor.processNotification(false, notification, pluginName, properties, callContext);
                 } else {
-                    return paymentGatewayProcessor.processNotification(notification, adjustedPaymentMethodId, properties, callContext);
+                    return paymentGatewayProcessor.processNotification(false, notification, adjustedPaymentMethodId, properties, callContext);
                 }
             }
         });
     }
 
+    private HostedPaymentPageFormDescriptor buildFormDescriptor(final boolean shouldDispatch, final Account account, final UUID paymentMethodId, final Iterable<PluginProperty> customFields, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
+        final UUID paymentMethodIdToUse = paymentMethodId != null ? paymentMethodId : account.getPaymentMethodId();
+        if (paymentMethodIdToUse == null) {
+            throw new PaymentApiException(ErrorCode.PAYMENT_INVALID_PARAMETER, paymentMethodId, "should not be null");
+        }
+
+        return paymentGatewayProcessor.buildFormDescriptor(shouldDispatch, account, paymentMethodIdToUse, customFields, properties, callContext, internalCallContextFactory.createInternalCallContext(account.getId(), callContext));
+    }
+
     private interface WithPaymentControlCallback<T> {
 
         T doPaymentGatewayApiOperation(final UUID adjustedPaymentMethodId, final Iterable<PluginProperty> adjustedPluginProperties) throws PaymentApiException;
diff --git a/payment/src/main/java/org/killbill/billing/payment/core/PaymentGatewayProcessor.java b/payment/src/main/java/org/killbill/billing/payment/core/PaymentGatewayProcessor.java
index 2b3b010..3ca5b2e 100644
--- a/payment/src/main/java/org/killbill/billing/payment/core/PaymentGatewayProcessor.java
+++ b/payment/src/main/java/org/killbill/billing/payment/core/PaymentGatewayProcessor.java
@@ -74,48 +74,66 @@ public class PaymentGatewayProcessor extends ProcessorBase {
         this.paymentPluginNotificationDispatcher = new PluginDispatcher<GatewayNotification>(paymentPluginTimeoutSec, executors);
     }
 
-    public GatewayNotification processNotification(final String notification, final UUID paymentMethodId, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
+    public GatewayNotification processNotification(final boolean shouldDispatch, final String notification, final UUID paymentMethodId, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
         final String pluginName = getPaymentProviderPluginName(paymentMethodId, internalCallContextFactory.createInternalCallContext(paymentMethodId, ObjectType.PAYMENT_METHOD, callContext));
-        return processNotification(notification, pluginName, properties, callContext);
+        return processNotification(shouldDispatch, notification, pluginName, properties, callContext);
     }
 
-    public GatewayNotification processNotification(final String notification, final String pluginName, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
-        return dispatchWithExceptionHandling(null,
-                                             pluginName,
-                                             new Callable<PluginDispatcherReturnType<GatewayNotification>>() {
-                                                 @Override
-                                                 public PluginDispatcherReturnType<GatewayNotification> call() throws PaymentApiException {
-                                                     final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
+    public GatewayNotification processNotification(final boolean shouldDispatch, final String notification, final String pluginName, final Iterable<PluginProperty> properties, final CallContext callContext) throws PaymentApiException {
+        if (shouldDispatch) {
+            return dispatchWithExceptionHandling(null,
+                                                 pluginName,
+                                                 new Callable<PluginDispatcherReturnType<GatewayNotification>>() {
+                                                     @Override
+                                                     public PluginDispatcherReturnType<GatewayNotification> call() throws PaymentApiException {
+                                                         final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
 
-                                                     try {
-                                                         final GatewayNotification result = plugin.processNotification(notification, properties, callContext);
-                                                         return PluginDispatcher.createPluginDispatcherReturnType(result == null ? new DefaultNoOpGatewayNotification() : result);
-                                                     } catch (final PaymentPluginApiException e) {
-                                                         throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+                                                         try {
+                                                             final GatewayNotification result = plugin.processNotification(notification, properties, callContext);
+                                                             return PluginDispatcher.createPluginDispatcherReturnType(result == null ? new DefaultNoOpGatewayNotification() : result);
+                                                         } catch (final PaymentPluginApiException e) {
+                                                             throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+                                                         }
                                                      }
-                                                 }
-                                             }, paymentPluginNotificationDispatcher);
+                                                 }, paymentPluginNotificationDispatcher);
+        } else {
+            final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
+            try {
+                return plugin.processNotification(notification, properties, callContext);
+            } catch (final PaymentPluginApiException e) {
+                throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+            }
+        }
     }
 
-    public HostedPaymentPageFormDescriptor buildFormDescriptor(final Account account, final UUID paymentMethodId, final Iterable<PluginProperty> customFields, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext internalCallContext) throws PaymentApiException {
+    public HostedPaymentPageFormDescriptor buildFormDescriptor(final boolean shouldDispatch, final Account account, final UUID paymentMethodId, final Iterable<PluginProperty> customFields, final Iterable<PluginProperty> properties, final CallContext callContext, final InternalCallContext internalCallContext) throws PaymentApiException {
         final String pluginName = getPaymentProviderPluginName(paymentMethodId, internalCallContext);
 
-        return dispatchWithExceptionHandling(account,
-                                             pluginName,
-                                             new Callable<PluginDispatcherReturnType<HostedPaymentPageFormDescriptor>>() {
-                                                 @Override
-                                                 public PluginDispatcherReturnType<HostedPaymentPageFormDescriptor> call() throws PaymentApiException {
-                                                     final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
+        if (shouldDispatch) {
+            return dispatchWithExceptionHandling(account,
+                                                 pluginName,
+                                                 new Callable<PluginDispatcherReturnType<HostedPaymentPageFormDescriptor>>() {
+                                                     @Override
+                                                     public PluginDispatcherReturnType<HostedPaymentPageFormDescriptor> call() throws PaymentApiException {
+                                                         final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
 
-                                                     try {
-                                                         final HostedPaymentPageFormDescriptor result = plugin.buildFormDescriptor(account.getId(), customFields, properties, callContext);
-                                                         return PluginDispatcher.createPluginDispatcherReturnType(result == null ? new DefaultNoOpHostedPaymentPageFormDescriptor(account.getId()) : result);
-                                                     } catch (final RuntimeException e) {
-                                                         throw new PaymentApiException(e, ErrorCode.PAYMENT_INTERNAL_ERROR, Objects.firstNonNull(e.getMessage(), ""));
-                                                     } catch (final PaymentPluginApiException e) {
-                                                         throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+                                                         try {
+                                                             final HostedPaymentPageFormDescriptor result = plugin.buildFormDescriptor(account.getId(), customFields, properties, callContext);
+                                                             return PluginDispatcher.createPluginDispatcherReturnType(result == null ? new DefaultNoOpHostedPaymentPageFormDescriptor(account.getId()) : result);
+                                                         } catch (final RuntimeException e) {
+                                                             throw new PaymentApiException(e, ErrorCode.PAYMENT_INTERNAL_ERROR, Objects.firstNonNull(e.getMessage(), ""));
+                                                         } catch (final PaymentPluginApiException e) {
+                                                             throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+                                                         }
                                                      }
-                                                 }
-                                             }, paymentPluginFormDispatcher);
+                                                 }, paymentPluginFormDispatcher);
+        } else {
+            final PaymentPluginApi plugin = getPaymentPluginApi(pluginName);
+            try {
+                return plugin.buildFormDescriptor(account.getId(), customFields, properties, callContext);
+            } catch (final PaymentPluginApiException e) {
+                throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, e.getErrorMessage());
+            }
+        }
     }
 }