org.mule.modules.applepush.ApplePushCloudConnector.java Source code

Java tutorial

Introduction

Here is the source code for org.mule.modules.applepush.ApplePushCloudConnector.java

Source

/**
 * (c) 2003-2015 MuleSoft, Inc. The software in this package is
 * published under the terms of the CPAL v1.0 license, a copy of which
 * has been included with this distribution in the LICENSE.md file.
 */

package org.mule.modules.applepush;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.lang.Validate;
import org.mule.api.MuleRuntimeException;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Module;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.param.Default;
import org.mule.api.annotations.param.Optional;
import org.mule.config.i18n.MessageFactory;
import org.mule.util.IOUtils;

import com.notnoop.apns.ApnsService;
import com.notnoop.apns.ApnsServiceBuilder;
import com.notnoop.apns.PayloadBuilder;

/**
 * Mule Connector for <a href=
 * "http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html"
 * > Apple Push</a>
 * <p>
 * Apple Push Notification service (APNs for short) is the centerpiece of the push
 * notifications feature. It is a robust and highly efficient service for propagating
 * information to devices such as iPhone, iPad, and iPod touch devices. Each device
 * establishes an accredited and encrypted IP connection with the service and
 * receives notifications over this persistent connection. If a notification for an
 * application arrives when that application is not running, the device alerts the
 * user that the application has data waiting for it.
 * </p>
 * <p>
 * Software developers (providers?) originate the notifications in their server
 * software. The provider connects with APNs through a persistent and secure channel
 * while monitoring incoming data intended for their client applications. When new
 * data for an application arrives, the provider prepares and sends a notification
 * through the channel to APNs, which pushes the notification to the target device.
 * </p>
 *
 * @author MuleSoft, Inc.
 */
@Module(name = "apple-push", schemaVersion = "3.4", friendlyName = "Apple Push Notifications")
public class ApplePushCloudConnector {
    /**
     * Hostname of Apple's Push Notification Gateway
     */
    @Configurable
    private String host;

    /**
     * Port number of Apple's Push Notification Gateway
     */
    @Configurable
    private int port;

    /**
     * Filename of the keystore which contains the SSL certificate. The file needs be
     * available on the classpath and must be of PKCS #12 format.
     */
    @Configurable
    private String keystore;

    /**
     * Password of the keystore
     */
    @Configurable
    private String keystorePassword;

    /**
     * Sends a push notification with the provided information to the iPhone
     * identified by deviceToken.
     * <p/>
     * {@sample.xml ../../../doc/apple-push.xml.sample apple-push:send}
     *
     * @param deviceToken The device token of the iPhone to which this notification
     *                    is intended
     * @param alert       The alert body of the notification
     * @param sound       The name of a sound file in the application bundle. The sound in
     *                    this file is played as an alert. If the sound file doesnt exist or
     *                    default is specified as the value, the default alert sound is
     *                    played.
     * @param badge       The number to display as the badge of the application icon. If
     *                    this Configurable is absent, any badge number currently shown is
     *                    removed.
     * @param fields      Application-specific custom fields. These values are presented
     *                    to the application and the iOS doesn't display them automatically.
     */
    @Processor
    public void send(final String deviceToken, @Optional final String alert, @Optional final String sound,
            @Default("-1") final int badge, @Optional final Map<String, String> fields) {
        final InputStream certificate = getKeyStoreAsStream();
        Validate.notNull(certificate,
                "Failed to load keystore from classpath or file system with provided path: " + keystore);

        final ApnsServiceBuilder serviceBuilder = new ApnsServiceBuilder();
        final ApnsService service = serviceBuilder.withGatewayDestination(host, port)
                .withCert(certificate, keystorePassword).build();

        final PayloadBuilder payloadBuilder = PayloadBuilder.newPayload();

        if (alert != null) {
            payloadBuilder.alertBody(alert);
        }

        if (sound != null) {
            payloadBuilder.sound(sound);
        }

        if (badge != -1) {
            payloadBuilder.badge(badge);
        }

        if (fields != null) {
            payloadBuilder.customFields(fields);
        }

        final String payload = payloadBuilder.build();

        service.push(deviceToken, payload);
    }

    private InputStream getKeyStoreAsStream() {
        final InputStream keyStore = ClassLoader.getSystemResourceAsStream(keystore);
        if (keyStore != null) {
            return keyStore;
        }

        try {
            return IOUtils.getResourceAsStream(keystore, getClass(), true, false);
        } catch (final IOException ioe) {
            throw new MuleRuntimeException(MessageFactory.createStaticMessage(
                    "Failure when trying to get keystore as stream from path: " + keystore), ioe);
        }
    }

    public String getHost() {
        return host;
    }

    public void setHost(final String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(final int port) {
        this.port = port;
    }

    public String getKeystore() {
        return keystore;
    }

    public void setKeystore(final String keystore) {
        this.keystore = keystore;
    }

    public String getKeystorePassword() {
        return keystorePassword;
    }

    public void setKeystorePassword(final String keystorePassword) {
        this.keystorePassword = keystorePassword;
    }
}