From b4b641139343e1d66fecd1c0a88f6a4c645d5e89 Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski <oliver.lazoroski@gmail.com> Date: Thu, 24 Dec 2015 00:49:40 +0100 Subject: [PATCH 1/2] Fixes #148: PayloadBuilder gives no access to ObjectMapper --- src/main/java/com/notnoop/apns/APNS.java | 11 +++++++- .../com/notnoop/apns/DefaultObjectMapper.java | 19 +++++++++++++ .../java/com/notnoop/apns/PayloadBuilder.java | 27 ++++++++++++++----- 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/notnoop/apns/DefaultObjectMapper.java diff --git a/src/main/java/com/notnoop/apns/APNS.java b/src/main/java/com/notnoop/apns/APNS.java index 9b8f68c0..50ab96a8 100644 --- a/src/main/java/com/notnoop/apns/APNS.java +++ b/src/main/java/com/notnoop/apns/APNS.java @@ -30,6 +30,8 @@ */ package com.notnoop.apns; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * The main class to interact with the APNS Service. * @@ -45,7 +47,14 @@ public final class APNS { * Returns a new Payload builder */ public static PayloadBuilder newPayload() { - return new PayloadBuilder(); + return newPayload(DefaultObjectMapper.get()); + } + + /** + * Returns a new Payload builder which uses the given {@link ObjectMapper} to build the payload. + */ + public static PayloadBuilder newPayload(ObjectMapper mapper) { + return new PayloadBuilder(mapper); } /** diff --git a/src/main/java/com/notnoop/apns/DefaultObjectMapper.java b/src/main/java/com/notnoop/apns/DefaultObjectMapper.java new file mode 100644 index 00000000..80cb412f --- /dev/null +++ b/src/main/java/com/notnoop/apns/DefaultObjectMapper.java @@ -0,0 +1,19 @@ +package com.notnoop.apns; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * An {@link ObjectMapper} holder. + */ +class DefaultObjectMapper { + + private static final ObjectMapper defaultMapper = new ObjectMapper(); + + /** + * Returns the default {@link ObjectMapper} instance. + */ + public static ObjectMapper get() { + return defaultMapper; + } + +} diff --git a/src/main/java/com/notnoop/apns/PayloadBuilder.java b/src/main/java/com/notnoop/apns/PayloadBuilder.java index 798c22a0..4008e299 100644 --- a/src/main/java/com/notnoop/apns/PayloadBuilder.java +++ b/src/main/java/com/notnoop/apns/PayloadBuilder.java @@ -42,7 +42,7 @@ * specified by Apple Push Notification Programming Guide. */ public final class PayloadBuilder { - private static final ObjectMapper mapper = new ObjectMapper(); + private final ObjectMapper mapper; private final Map<String, Object> root; private final Map<String, Object> aps; @@ -52,9 +52,18 @@ public final class PayloadBuilder { * Constructs a new instance of {@code PayloadBuilder} */ PayloadBuilder() { + this(DefaultObjectMapper.get()); + } + + /** + * Constructs a new instance of {@code PayloadBuilder} + * which uses the given {@link ObjectMapper} to build the payload. + */ + PayloadBuilder(ObjectMapper mapper) { root = new HashMap<String, Object>(); aps = new HashMap<String, Object>(); customAlert = new HashMap<String, Object>(); + this.mapper = mapper; } /** @@ -251,8 +260,8 @@ public PayloadBuilder forNewsstand() { /** * With iOS7 it is possible to have the application wake up before the user opens the app. - * - * The same key-word can also be used to send 'silent' notifications. With these 'silent' notification + * + * The same key-word can also be used to send 'silent' notifications. With these 'silent' notification * a different app delegate is being invoked, allowing the app to perform background tasks. * * @return this @@ -511,10 +520,12 @@ public String toString() { private PayloadBuilder(final Map<String, Object> root, final Map<String, Object> aps, - final Map<String, Object> customAlert) { + final Map<String, Object> customAlert, + final ObjectMapper mapper) { this.root = new HashMap<String, Object>(root); this.aps = new HashMap<String, Object>(aps); this.customAlert = new HashMap<String, Object>(customAlert); + this.mapper = mapper; } /** @@ -523,13 +534,17 @@ private PayloadBuilder(final Map<String, Object> root, * @return a copy of this builder */ public PayloadBuilder copy() { - return new PayloadBuilder(root, aps, customAlert); + ObjectMapper mapper = DefaultObjectMapper.get() != this.mapper + ? this.mapper.copy() // custom mapper, should be cloned + : this.mapper; // default mapper, no need for cloning + + return new PayloadBuilder(root, aps, customAlert, mapper); } /** * @return a new instance of Payload Builder */ public static PayloadBuilder newPayload() { - return new PayloadBuilder(); + return new PayloadBuilder(DefaultObjectMapper.get()); } } From bfaa442843813d222389bab0d7acd8ffdb35d983 Mon Sep 17 00:00:00 2001 From: Oliver Lazoroski <oliver.lazoroski@gmail.com> Date: Thu, 24 Dec 2015 01:14:37 +0100 Subject: [PATCH 2/2] added missing licence header to DefaultObjectMapper --- .../com/notnoop/apns/DefaultObjectMapper.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/com/notnoop/apns/DefaultObjectMapper.java b/src/main/java/com/notnoop/apns/DefaultObjectMapper.java index 80cb412f..f6ee0e23 100644 --- a/src/main/java/com/notnoop/apns/DefaultObjectMapper.java +++ b/src/main/java/com/notnoop/apns/DefaultObjectMapper.java @@ -1,3 +1,33 @@ +/* + * Copyright 2009, Mahmood Ali. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Mahmood Ali. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package com.notnoop.apns; import com.fasterxml.jackson.databind.ObjectMapper;