Skip to content
Browse files

overhull of changes

  • Loading branch information...
1 parent 0f86474 commit 06f83028dba4bf44ff90295eea7a482953d74f1b @notnoop committed Jun 23, 2011
View
16 pom.xml
@@ -4,7 +4,6 @@
<artifactId>mpns</artifactId>
<name>Java Microsoft Push Notification Service Library</name>
<version>0.0.1-SNAPSHOT</version>
- <description>Library to send Microsoft Push Notification</description>
<!-- Inherit the Sonatype OSS deployment configuration -->
<!--
@@ -117,17 +116,24 @@
<dependencies>
<dependency>
- <groupId>ch.qos.logback</groupId>
- <artifactId>logback-classic</artifactId>
- <version>0.9.24</version>
- <scope>test</scope>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.1.1</version>
+ <scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>0.9.24</version>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>junit</groupId>
View
57 src/main/java/com/notnoop/mpns/MPNS.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2011, 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.mpns;
+
+/**
+ * The main class to interact with the MPNS Service.
+ *
+ * Provides an interface to create the {@link ApnsServiceBuilder} and
+ * {@code ApnsNotification} payload.
+ *
+ */
+public class MPNS {
+
+ private MPNS() { throw new AssertionError("Uninstantiable class"); }
+
+ /**
+ * Returns a new Payload builder
+ */
+ public static MpnsNotificationBuilder newNotification() {
+ return new MpnsNotificationBuilder();
+ }
+
+ /**
+ * Returns a new APNS Service for sending iPhone notifications
+ */
+ public static MpnsServiceBuilder newService() {
+ return new MpnsServiceBuilder();
+ }
+}
View
55 src/main/java/com/notnoop/mpns/MpnsNotification.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2011, 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.mpns;
+
+import java.util.List;
+import java.util.Map.Entry;
+
+/**
+ * Represents an MPNS notification to be sent to Microsoft service.
+ */
+public interface MpnsNotification {
+
+ /**
+ * Returns the binary representation of the payload.
+ *
+ */
+ public String getRequestBody();
+
+ /**
+ * Returns the identifier of the current message. The
+ * identifier is an application generated identifier.
+ *
+ * @return the notification identifier
+ */
+ public List<? extends Entry<String, String>> getHttpHeaders();
+
+}
View
35 src/main/java/com/notnoop/mpns/MpnsNotificationBuilder.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2011, 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.mpns;
+
+public class MpnsNotificationBuilder {
+
+}
View
101 src/main/java/com/notnoop/mpns/MpnsService.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2011, 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.mpns;
+
+import java.util.Collection;
+import java.util.Map;
+
+import com.notnoop.mpns.exceptions.NetworkIOException;
+
+/**
+ * Represents the connection and interface to the Apple APNS servers.
+ *
+ * The service is created by {@link ApnsServiceBuilder} like:
+ *
+ * <pre>
+ * ApnsService = APNS.newService()
+ * .withCert("/path/to/certificate.p12", "MyCertPassword")
+ * .withSandboxDestination()
+ * .build()
+ * </pre>
+ */
+public interface MpnsService {
+
+ /**
+ * Sends a push notification with the provided {@code payload} to the
+ * iPhone of {@code deviceToken}.
+ *
+ * The payload needs to be a valid JSON object, otherwise it may fail
+ * silently. It is recommended to use {@link PayloadBuilder} to create
+ * one.
+ *
+ * @param deviceToken the destination iPhone device token
+ * @param payload The payload message
+ * @throws NetworkIOException if a network error occured while
+ * attempting to send the message
+ */
+ void push(String subscriptionUri, String payload,
+ Collection<? extends Map.Entry<String, String>> headers)
+ throws NetworkIOException;
+
+ /**
+ * Sends the provided notification {@code message} to the desired
+ * destination.
+ * @throws NetworkIOException if a network error occured while
+ * attempting to send the message
+ */
+ void push(String subscriptionUri, MpnsNotification message)
+ throws NetworkIOException;
+
+ /**
+ * Starts the service.
+ *
+ * The underlying implementation may prepare its connections or
+ * datastructures to be able to send the messages.
+ *
+ * This method is a blocking call, even if the service represents
+ * a Non-blocking push service. Once the service is returned, it is ready
+ * to accept push requests.
+ *
+ * @throws NetworkIOException if a network error occurred while
+ * starting the service
+ */
+ void start();
+
+ /**
+ * Stops the service and frees any allocated resources it created for this
+ * service.
+ *
+ * The underlying implementation should close all connections it created,
+ * and possibly stop any threads as well.
+ */
+ void stop();
+}
View
182 src/main/java/com/notnoop/mpns/MpnsServiceBuilder.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2011, 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.mpns;
+
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.http.client.HttpClient;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import com.notnoop.mpns.internal.*;
+import com.notnoop.mpns.exceptions.RuntimeIOException;
+
+/**
+ * The class is used to create instances of {@link ApnsService}.
+ *
+ * Note that this class is not synchronized. If multiple threads access a
+ * {@code ApnsServiceBuilder} instance concurrently, and at least on of the
+ * threads modifies one of the attributes structurally, it must be
+ * synchronized externally.
+ *
+ * Starting a new {@code ApnsService} is easy:
+ *
+ * <pre>
+ * ApnsService = APNS.newService()
+ * .withCert("/path/to/certificate.p12", "MyCertPassword")
+ * .withSandboxDestination()
+ * .build()
+ * </pre>
+ */
+public class MpnsServiceBuilder {
+ private int pooledMax = 1;
+ private ExecutorService executor = null;
+
+ private boolean isQueued = false;
+ private Proxy proxy = null;
+
+ /**
+ * Constructs a new instance of {@code ApnsServiceBuilder}
+ */
+ public MpnsServiceBuilder() { }
+
+ /**
+ * Specify the address of the SOCKS proxy the connection should
+ * use.
+ *
+ * <p>Read the <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html">
+ * Java Networking and Proxies</a> guide to understand the
+ * proxies complexity.
+ *
+ * <p>Be aware that this method only handles SOCKS proxies, not
+ * HTTPS proxies. Use {@link #withProxy(Proxy)} instead.
+ *
+ * @param host the hostname of the SOCKS proxy
+ * @param port the port of the SOCKS proxy server
+ * @return this
+ */
+ public MpnsServiceBuilder withSocksProxy(String host, int port) {
+ Proxy proxy = new Proxy(Proxy.Type.SOCKS,
+ new InetSocketAddress(host, port));
+ return withProxy(proxy);
+ }
+
+ /**
+ * Specify the proxy to be used to establish the connections
+ * to Apple Servers
+ *
+ * <p>Read the <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html">
+ * Java Networking and Proxies</a> guide to understand the
+ * proxies complexity.
+ *
+ * @param proxy the proxy object to be used to create connections
+ * @return this
+ */
+ public MpnsServiceBuilder withProxy(Proxy proxy) {
+ this.proxy = proxy;
+ return this;
+ }
+
+ /**
+ * Constructs a pool of connections to the notification servers.
+ *
+ * Apple servers recommend using a pooled connection up to
+ * 15 concurrent persistent connections to the gateways.
+ *
+ * Note: This option has no effect when using non-blocking
+ * connections.
+ */
+ public MpnsServiceBuilder asPool(int maxConnections) {
+ return asPool(Executors.newFixedThreadPool(maxConnections), maxConnections);
+ }
+
+ /**
+ * Constructs a pool of connections to the notification servers.
+ *
+ * Apple servers recommend using a pooled connection up to
+ * 15 concurrent persistent connections to the gateways.
+ *
+ * Note: This option has no effect when using non-blocking
+ * connections.
+ *
+ * Note: The maxConnections here is used as a hint to how many connections
+ * get created.
+ */
+ public MpnsServiceBuilder asPool(ExecutorService executor, int maxConnections) {
+ this.pooledMax = maxConnections;
+ this.executor = executor;
+ return this;
+ }
+
+ /**
+ * Constructs a new thread with a processing queue to process
+ * notification requests.
+ *
+ * @return this
+ */
+ public MpnsServiceBuilder asQueued() {
+ this.isQueued = true;
+ return this;
+ }
+
+ /**
+ * Returns a fully initialized instance of {@link ApnsService},
+ * according to the requested settings.
+ *
+ * @return a new instance of ApnsService
+ */
+ public MpnsService build() {
+ checkInitialization();
+
+ if (pooledMax == 1) {
+ HttpClient client = new DefaultHttpClient();
+ return new MpnsServiceImpl(client);
+ } else {
+ HttpClient client = new DefaultHttpClient(Utilities.poolManager(pooledMax));
+ return new MpnsPooledService(client, executor);
+ }
+ }
+
+ private void checkInitialization() {
+ if (pooledMax != 1 && executor == null) {
+ throw new IllegalStateException("Executor service is required for pooled connections");
+ }
+ if (this.isQueued) {
+ throw new RuntimeException("Queued Connections not supported yet");
+ }
+ if (this.proxy != null) {
+ throw new RuntimeException("Proxies are not supported yet");
+ }
+ }
+}
View
65 src/main/java/com/notnoop/mpns/exceptions/InvalidSSLConfig.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2010, 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.mpns.exceptions;
+
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+
+/**
+ * Signals that the the provided SSL context settings (e.g.
+ * keystore path, password, encryption type, etc) are invalid
+ *
+ * This Exception can be caused by any of the following:
+ *
+ * <ol>
+ * <li>{@link KeyStoreException}</li>
+ * <li>{@link NoSuchAlgorithmException}</li>
+ * <li>{@link CertificateException}</li>
+ * <li>{@link IOException}</li>
+ * <li>{@link UnrecoverableKeyException}</li>
+ * <li>{@link KeyManagementException}</li>
+ * </ol>
+ *
+ */
+public class InvalidSSLConfig extends MpnsException {
+ private static final long serialVersionUID = -7283168775864517167L;
+
+ public InvalidSSLConfig() { super(); }
+ public InvalidSSLConfig(String message) { super(message); }
+ public InvalidSSLConfig(Throwable cause) { super(cause); }
+ public InvalidSSLConfig(String m, Throwable c) { super(m, c); }
+
+}
View
45 src/main/java/com/notnoop/mpns/exceptions/MpnsException.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2010, 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.mpns.exceptions;
+
+/**
+ * Base class for all the exceptions thrown in Apns Library
+ */
+public abstract class MpnsException extends RuntimeException {
+ private static final long serialVersionUID = -4756693306121825229L;
+
+ public MpnsException() { super(); }
+ public MpnsException(String message) { super(message); }
+ public MpnsException(Throwable cause) { super(cause); }
+ public MpnsException(String m, Throwable c) { super(m, c); }
+
+}
View
49 src/main/java/com/notnoop/mpns/exceptions/NetworkIOException.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010, 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.mpns.exceptions;
+
+import java.io.IOException;
+
+/**
+ * Thrown to indicate that that a network operation has failed:
+ * (e.g. connectivity problems, domain cannot be found, network
+ * dropped).
+ */
+public class NetworkIOException extends MpnsException {
+ private static final long serialVersionUID = 3353516625486306533L;
+
+ public NetworkIOException() { super(); }
+ public NetworkIOException(String message) { super(message); }
+ public NetworkIOException(IOException cause) { super(cause); }
+ public NetworkIOException(String m, IOException c) { super(m, c); }
+
+}
View
51 src/main/java/com/notnoop/mpns/exceptions/RuntimeIOException.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2010, 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.mpns.exceptions;
+
+import java.io.IOException;
+
+/**
+ * Signals that an I/O exception of some sort has occurred. This
+ * class is the general class of exceptions produced by failed or
+ * interrupted I/O operations.
+ *
+ * This is a RuntimeException, unlike the java.io.IOException
+ */
+public class RuntimeIOException extends MpnsException {
+ private static final long serialVersionUID = 8665285084049041306L;
+
+ public RuntimeIOException() { super(); }
+ public RuntimeIOException(String message) { super(message); }
+ public RuntimeIOException(IOException cause) { super(cause); }
+ public RuntimeIOException(String m, IOException c) { super(m, c); }
+
+}
View
89 src/main/java/com/notnoop/mpns/internal/AbstractMpnsService.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2011, 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.mpns.internal;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Collection;
+import java.util.Map.Entry;
+
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.protocol.HTTP;
+
+import com.notnoop.mpns.MpnsNotification;
+import com.notnoop.mpns.MpnsService;
+import com.notnoop.mpns.exceptions.NetworkIOException;
+
+public abstract class AbstractMpnsService implements MpnsService {
+
+ protected final HttpClient httpClient;
+
+ protected AbstractMpnsService(HttpClient httpClient) {
+ this.httpClient = httpClient;
+ }
+
+ protected HttpPost postMessage(String subscriptionUri, String requestBody, Collection<? extends Entry<String, String>> headers) {
+ HttpPost method = new HttpPost(subscriptionUri);
+ try {
+ method.setEntity(new StringEntity(requestBody,
+ "text/xml", HTTP.UTF_8));
+ } catch (UnsupportedEncodingException e) {
+ throw new AssertionError("UTF-8 isn't supported!!!");
+ }
+
+ for (Entry<String, String> header: headers) {
+ method.addHeader(header.getKey(), header.getValue());
+ }
+
+ return method;
+ }
+
+ protected abstract void push(HttpPost request);
+
+ public void push(String subscriptionUri, String payload,
+ Collection<? extends Entry<String, String>> headers)
+ throws NetworkIOException {
+ this.push(postMessage(subscriptionUri, payload, headers));
+ }
+
+ public void push(String subscriptionUri, MpnsNotification message)
+ throws NetworkIOException {
+ this.push(postMessage(subscriptionUri, message.getRequestBody(),
+ message.getHttpHeaders()));
+ }
+
+ public void start() {}
+
+ public void stop() {
+ this.httpClient.getConnectionManager().shutdown();
+ }
+}
View
69 src/main/java/com/notnoop/mpns/internal/MpnsPooledService.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2011, 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.mpns.internal;
+
+import java.util.concurrent.ExecutorService;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+
+import com.notnoop.mpns.MpnsService;
+
+public class MpnsPooledService extends AbstractMpnsService implements MpnsService {
+ private final ExecutorService executor;
+
+ public MpnsPooledService(HttpClient client, ExecutorService executor) {
+ super(client);
+ this.executor = executor;
+ }
+
+ @Override
+ protected void push(final HttpPost request) {
+ executor.execute(new Runnable() {
+ public void run() {
+ try {
+ HttpResponse response = httpClient.execute(request);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ });
+ }
+
+
+
+ @Override
+ public void stop() {
+ this.executor.shutdown();
+ }
+
+}
View
58 src/main/java/com/notnoop/mpns/internal/MpnsServiceImpl.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011, 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.mpns.internal;
+
+import java.io.IOException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+
+import com.notnoop.mpns.MpnsService;
+import com.notnoop.mpns.exceptions.NetworkIOException;
+
+public class MpnsServiceImpl extends AbstractMpnsService implements MpnsService {
+ public MpnsServiceImpl(HttpClient client) {
+ super(client);
+ }
+
+ @Override
+ protected void push(HttpPost request) {
+ try {
+ HttpResponse response = httpClient.execute(request);
+ } catch (ClientProtocolException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new NetworkIOException(e);
+ }
+ }
+}
View
72 src/main/java/com/notnoop/mpns/internal/Utilities.java
@@ -0,0 +1,72 @@
+/*
+* Copyright 2011, 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.mpns.internal;
+
+import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+
+
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+public final class Utilities {
+ private Utilities() { throw new AssertionError("Uninstantiable class"); }
+
+ public static String readFully(InputStream is) {
+ final char[] buffer = new char[0x10000];
+ StringBuilder out = new StringBuilder();
+ try {
+ Reader in = new InputStreamReader(is, "UTF-8");
+ int read;
+ do {
+ read = in.read(buffer, 0, buffer.length);
+ if (read>0) {
+ out.append(buffer, 0, read);
+ }
+ } while (read>=0);
+ return out.toString();
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static ThreadSafeClientConnManager poolManager(int maxConnections) {
+ ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
+ cm.setMaxTotal(maxConnections);
+ cm.setDefaultMaxPerRoute(maxConnections);
+
+ return cm;
+ }
+
+}

0 comments on commit 06f8302

Please sign in to comment.
Something went wrong with that request. Please try again.