Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #148: PayloadBuilder gives no access to ObjectMapper #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/com/notnoop/apns/APNS.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
package com.notnoop.apns;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* The main class to interact with the APNS Service.
*
Expand All @@ -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);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/notnoop/apns/DefaultObjectMapper.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
27 changes: 21 additions & 6 deletions src/main/java/com/notnoop/apns/PayloadBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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());
}
}