001/* 002 * Copyright 2010-2014 Ning, Inc. 003 * Copyright 2014-2015 The Billing Project, LLC 004 * 005 * The Billing Project licenses this file to you under the Apache License, version 2.0 006 * (the "License"); you may not use this file except in compliance with the 007 * License. You may obtain a copy of the License at: 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package com.ning.billing.recurly.model.push; 019 020import com.google.common.base.CaseFormat; 021import java.io.IOException; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025import com.ning.billing.recurly.model.RecurlyObject; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029public abstract class Notification extends RecurlyObject { 030 031 private static Logger log = LoggerFactory.getLogger(Notification.class); 032 private static Pattern ROOT_NAME = Pattern.compile("<(\\w+_notification)>"); 033 034 public static enum Type { 035 BillingInfoUpdatedNotification(com.ning.billing.recurly.model.push.account.BillingInfoUpdatedNotification.class), 036 BillingInfoUpdateFailedNotification(com.ning.billing.recurly.model.push.account.BillingInfoUpdateFailedNotification.class), 037 NewShippingAddressNotification(com.ning.billing.recurly.model.push.account.NewShippingAddressNotification.class), 038 UpdatedShippingAddressNotification(com.ning.billing.recurly.model.push.account.UpdatedShippingAddressNotification.class), 039 DeletedShippingAddressNotification(com.ning.billing.recurly.model.push.account.DeletedShippingAddressNotification.class), 040 CanceledAccountNotification(com.ning.billing.recurly.model.push.account.CanceledAccountNotification.class), 041 NewAccountNotification(com.ning.billing.recurly.model.push.account.NewAccountNotification.class), 042 FailedPaymentNotification(com.ning.billing.recurly.model.push.payment.FailedPaymentNotification.class), 043 ScheduledPaymentNotification(com.ning.billing.recurly.model.push.payment.ScheduledPaymentNotification.class), 044 ProcessingPaymentNotification(com.ning.billing.recurly.model.push.payment.ProcessingPaymentNotification.class), 045 SuccessfulPaymentNotification(com.ning.billing.recurly.model.push.payment.SuccessfulPaymentNotification.class), 046 SuccessfulRefundNotification(com.ning.billing.recurly.model.push.payment.SuccessfulRefundNotification.class), 047 VoidPaymentNotification(com.ning.billing.recurly.model.push.payment.VoidPaymentNotification.class), 048 FraudInfoUpdatedNotification(com.ning.billing.recurly.model.push.payment.FraudInfoUpdatedNotification.class), 049 TransactionStatusUpdatedNotification(com.ning.billing.recurly.model.push.payment.TransactionStatusUpdatedNotification.class), 050 TransactionAuthorizedNotification(com.ning.billing.recurly.model.push.payment.TransactionAuthorizedNotification.class), 051 NewCreditPaymentNotification(com.ning.billing.recurly.model.push.creditpayment.NewCreditPaymentNotification.class), 052 VoidedCreditPaymentNotification(com.ning.billing.recurly.model.push.creditpayment.VoidedCreditPaymentNotification.class), 053 CanceledSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.CanceledSubscriptionNotification.class), 054 ExpiredSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.ExpiredSubscriptionNotification.class), 055 SubscriptionPausedNotification(com.ning.billing.recurly.model.push.subscription.SubscriptionPausedNotification.class), 056 SubscriptionResumedNotification(com.ning.billing.recurly.model.push.subscription.SubscriptionResumedNotification.class), 057 ScheduledSubscriptionPauseNotification(com.ning.billing.recurly.model.push.subscription.ScheduledSubscriptionPauseNotification.class), 058 SubscriptionPausedModifiedNotification(com.ning.billing.recurly.model.push.subscription.SubscriptionPausedModifiedNotification.class), 059 PausedSubscriptionRenewalNotification(com.ning.billing.recurly.model.push.subscription.PausedSubscriptionRenewalNotification.class), 060 SubscriptionPausedCanceledNotification(com.ning.billing.recurly.model.push.subscription.SubscriptionPausedCanceledNotification.class), 061 NewSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.NewSubscriptionNotification.class), 062 ReactivatedAccountNotification(com.ning.billing.recurly.model.push.subscription.ReactivatedAccountNotification.class), 063 RenewedSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.RenewedSubscriptionNotification.class), 064 UpdatedSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.UpdatedSubscriptionNotification.class), 065 ClosedInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ClosedInvoiceNotification.class), 066 NewInvoiceNotification(com.ning.billing.recurly.model.push.invoice.NewInvoiceNotification.class), 067 PastDueInvoiceNotification(com.ning.billing.recurly.model.push.invoice.PastDueInvoiceNotification.class), 068 ProcessingInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ProcessingInvoiceNotification.class), 069 UpdatedAccountNotification(com.ning.billing.recurly.model.push.account.UpdatedAccountNotification.class), 070 NewDunningEventNotification(com.ning.billing.recurly.model.push.invoice.NewDunningEventNotification.class), 071 NewChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.NewChargeInvoiceNotification.class), 072 ProcessingChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ProcessingChargeInvoiceNotification.class), 073 PastDueChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.PastDueChargeInvoiceNotification.class), 074 PaidChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.PaidChargeInvoiceNotification.class), 075 FailedChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.FailedChargeInvoiceNotification.class), 076 ReopenedChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ReopenedChargeInvoiceNotification.class), 077 UpdatedChargeInvoiceNotification(com.ning.billing.recurly.model.push.invoice.UpdatedChargeInvoiceNotification.class), 078 NewCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.NewCreditInvoiceNotification.class), 079 ProcessingCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ProcessingCreditInvoiceNotification.class), 080 ClosedCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ClosedCreditInvoiceNotification.class), 081 VoidedCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.VoidedCreditInvoiceNotification.class), 082 ReopenedCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.ReopenedCreditInvoiceNotification.class), 083 OpenCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.OpenCreditInvoiceNotification.class), 084 UpdatedCreditInvoiceNotification(com.ning.billing.recurly.model.push.invoice.UpdatedCreditInvoiceNotification.class), 085 UpdatedInvoiceNotification(com.ning.billing.recurly.model.push.invoice.UpdatedInvoiceNotification.class), 086 NewUsageNotification(com.ning.billing.recurly.model.push.usage.NewUsageNotification.class), 087 PurchasedGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.PurchasedGiftCardNotification.class), 088 CanceledGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.CanceledGiftCardNotification.class), 089 UpdatedGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.UpdatedGiftCardNotification.class), 090 RegeneratedGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.RegeneratedGiftCardNotification.class), 091 RedeemedGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.RedeemedGiftCardNotification.class), 092 UpdatedBalanceGiftCardNotification(com.ning.billing.recurly.model.push.giftcard.UpdatedBalanceGiftCardNotification.class), 093 LowBalanceGiftCardNotification(com.ning.billing.recurly.model.push.subscription.LowBalanceGiftCardNotification.class), 094 NewItemNotification(com.ning.billing.recurly.model.push.item.NewItemNotification.class), 095 UpdatedItemNotification(com.ning.billing.recurly.model.push.item.UpdatedItemNotification.class), 096 DeactivatedItemNotification(com.ning.billing.recurly.model.push.item.DeactivatedItemNotification.class), 097 ReactivatedItemNotification(com.ning.billing.recurly.model.push.item.ReactivatedItemNotification.class); 098 099 private Class<? extends Notification> javaType; 100 101 private Type(final Class<? extends Notification> javaType) { 102 this.javaType = javaType; 103 } 104 105 public Class<? extends Notification> getJavaType() { 106 return javaType; 107 } 108 109 public void setJavaType(final Class<? extends Notification> javaType) { 110 this.javaType = javaType; 111 } 112 } 113 114 public static <T> T read(final String payload, final Class<T> clazz) { 115 try { 116 // TODO Should we cache the mapper? 117 return RecurlyObject.newXmlMapper().readValue(payload, clazz); 118 } catch (IOException e) { 119 log.warn("Enable to read notification, de-serialization failed : {}", e.getMessage()); 120 return null; 121 } 122 } 123 124 /** 125 * Detect notification type based on the xml root name. 126 * 127 * @param payload 128 * @return notification type or null if root name is not found or if there 129 * is no type corresponding to the root name 130 */ 131 public static Type detect(final String payload) { 132 final Matcher m = ROOT_NAME.matcher(payload); 133 if (m.find() && m.groupCount() >= 1) { 134 final String root = m.group(1); 135 try { 136 return Type.valueOf(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, root)); 137 } catch (IllegalArgumentException e) { 138 log.warn("Enable to detect notification type, no type for {}", root); 139 return null; 140 } 141 } 142 log.warn("Enable to detect notification type"); 143 return null; 144 } 145}