001/* 002 * Copyright 2010-2013 Ning, Inc. 003 * 004 * Ning licenses this file to you under the Apache License, version 2.0 005 * (the "License"); you may not use this file except in compliance with the 006 * License. You may obtain a copy of the License at: 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations 014 * under the License. 015 */ 016 017package com.ning.billing.recurly.model.push; 018 019import java.io.IOException; 020import java.util.regex.Matcher; 021import java.util.regex.Pattern; 022 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026import com.ning.billing.recurly.model.RecurlyObject; 027 028import com.google.common.base.CaseFormat; 029 030public abstract class Notification extends RecurlyObject { 031 032 private static Logger log = LoggerFactory.getLogger(Notification.class); 033 private static Pattern ROOT_NAME = Pattern.compile("<(.*_notification)>"); 034 035 public static enum Type { 036 BillingInfoUpdatedNotification(com.ning.billing.recurly.model.push.account.BillingInfoUpdatedNotification.class), 037 CanceledAccountNotification(com.ning.billing.recurly.model.push.account.CanceledAccountNotification.class), 038 NewAccountNotification(com.ning.billing.recurly.model.push.account.NewAccountNotification.class), 039 FailedPaymentNotification(com.ning.billing.recurly.model.push.payment.FailedPaymentNotification.class), 040 SuccessfulPaymentNotification(com.ning.billing.recurly.model.push.payment.SuccessfulPaymentNotification.class), 041 SuccessfulRefundNotification(com.ning.billing.recurly.model.push.payment.SuccessfulRefundNotification.class), 042 VoidedPaymentNotification(com.ning.billing.recurly.model.push.payment.VoidedPaymentNotification.class), 043 CanceledSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.CanceledSubscriptionNotification.class), 044 ExpiredSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.ExpiredSubscriptionNotification.class), 045 NewSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.NewSubscriptionNotification.class), 046 ReactivatedAccountNotification(com.ning.billing.recurly.model.push.subscription.ReactivatedAccountNotification.class), 047 RenewedSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.RenewedSubscriptionNotification.class), 048 UpdatedSubscriptionNotification(com.ning.billing.recurly.model.push.subscription.UpdatedSubscriptionNotification.class),; 049 050 private Class<? extends Notification> javaType; 051 052 private Type(final Class<? extends Notification> javaType) { 053 this.javaType = javaType; 054 } 055 056 public Class<? extends Notification> getJavaType() { 057 return javaType; 058 } 059 060 public void setJavaType(final Class<? extends Notification> javaType) { 061 this.javaType = javaType; 062 } 063 } 064 065 public static <T> T read(final String payload, final Class<T> clazz) { 066 try { 067 // TODO Should we cache the mapper? 068 return RecurlyObject.newXmlMapper().readValue(payload, clazz); 069 } catch (IOException e) { 070 log.warn("Enable to read notification, de-serialization failed : {}", e.getMessage()); 071 return null; 072 } 073 } 074 075 /** 076 * Detect notification type based on the xml root name. 077 * 078 * @param payload 079 * @return notification type or null if root name is not found or if there 080 * is no type corresponding to the root name 081 */ 082 public static Type detect(final String payload) { 083 final Matcher m = ROOT_NAME.matcher(payload); 084 if (m.find() && m.groupCount() >= 1) { 085 final String root = m.group(1); 086 try { 087 return Type.valueOf(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, root)); 088 } catch (IllegalArgumentException e) { 089 log.warn("Enable to detect notification type, no type for {}", root); 090 return null; 091 } 092 } 093 log.warn("Enable to detect notification type"); 094 return null; 095 } 096}