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..f6ee0e23 --- /dev/null +++ b/src/main/java/com/notnoop/apns/DefaultObjectMapper.java @@ -0,0 +1,49 @@ +/* + * 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; + +/** + * 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 root; private final Map 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(); aps = new HashMap(); customAlert = new HashMap(); + 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 root, final Map aps, - final Map customAlert) { + final Map customAlert, + final ObjectMapper mapper) { this.root = new HashMap(root); this.aps = new HashMap(aps); this.customAlert = new HashMap(customAlert); + this.mapper = mapper; } /** @@ -523,13 +534,17 @@ private PayloadBuilder(final Map 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()); } }