PageRenderTime 45ms CodeModel.GetById 18ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/notnoop/apns/internal/ApnsFeedbackConnection.java

http://github.com/notnoop/java-apns
Java | 121 lines | 81 code | 10 blank | 30 comment | 8 complexity | 4d374564716efafa244e263a0792b097 MD5 | raw file
  1/*
  2 *  Copyright 2009, Mahmood Ali.
  3 *  All rights reserved.
  4 *
  5 *  Redistribution and use in source and binary forms, with or without
  6 *  modification, are permitted provided that the following conditions are
  7 *  met:
  8 *
  9 *    * Redistributions of source code must retain the above copyright
 10 *      notice, this list of conditions and the following disclaimer.
 11 *    * Redistributions in binary form must reproduce the above
 12 *      copyright notice, this list of conditions and the following disclaimer
 13 *      in the documentation and/or other materials provided with the
 14 *      distribution.
 15 *    * Neither the name of Mahmood Ali. nor the names of its
 16 *      contributors may be used to endorse or promote products derived from
 17 *      this software without specific prior written permission.
 18 *
 19 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 20 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 21 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 22 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 23 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 24 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 25 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 26 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 27 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 28 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 29 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 30 */
 31package com.notnoop.apns.internal;
 32
 33import java.io.IOException;
 34import java.io.InputStream;
 35import java.net.InetSocketAddress;
 36import java.net.Proxy;
 37import java.net.Socket;
 38import java.util.Date;
 39import java.util.Map;
 40import javax.net.SocketFactory;
 41import javax.net.ssl.SSLSocketFactory;
 42import org.slf4j.Logger;
 43import org.slf4j.LoggerFactory;
 44import com.notnoop.exceptions.NetworkIOException;
 45
 46public class ApnsFeedbackConnection {
 47    private static final Logger logger = LoggerFactory.getLogger(ApnsFeedbackConnection.class);
 48
 49    private final SocketFactory factory;
 50    private final String host;
 51    private final int port;
 52    private final Proxy proxy;
 53    private final int readTimeout;
 54    private final int connectTimeout;
 55    private final String proxyUsername;
 56    private final String proxyPassword;
 57
 58    public ApnsFeedbackConnection(final SocketFactory factory, final String host, final int port) {
 59        this(factory, host, port, null, 0, 0, null, null);
 60    }
 61
 62    public ApnsFeedbackConnection(final SocketFactory factory, final String host, final int port,
 63            final Proxy proxy, int readTimeout, int connectTimeout, final String proxyUsername, final String proxyPassword) {
 64        this.factory = factory;
 65        this.host = host;
 66        this.port = port;
 67        this.proxy = proxy;
 68        this.readTimeout = readTimeout;
 69        this.connectTimeout = connectTimeout;
 70        this.proxyUsername = proxyUsername;
 71        this.proxyPassword = proxyPassword;
 72    }
 73
 74    int DELAY_IN_MS = 1000;
 75    private static final int RETRIES = 3;
 76
 77    public Map<String, Date> getInactiveDevices() throws NetworkIOException {
 78        int attempts = 0;
 79        while (true) {
 80            try {
 81                attempts++;
 82                final Map<String, Date> result = getInactiveDevicesImpl();
 83
 84                attempts = 0;
 85                return result;
 86            } catch (final Exception e) {
 87                logger.warn("Failed to retrieve invalid devices", e);
 88                if (attempts >= RETRIES) {
 89                    logger.error("Couldn't get feedback connection", e);
 90                    Utilities.wrapAndThrowAsRuntimeException(e);
 91                }
 92                Utilities.sleep(DELAY_IN_MS);
 93            }
 94        }
 95    }
 96
 97    public Map<String, Date> getInactiveDevicesImpl() throws IOException {
 98        Socket proxySocket = null;
 99        Socket socket = null;
100        try {
101            if (proxy == null) {
102                socket = factory.createSocket(host, port);
103            } else if (proxy.type() == Proxy.Type.HTTP) {
104                TlsTunnelBuilder tunnelBuilder = new TlsTunnelBuilder();
105                socket = tunnelBuilder.build((SSLSocketFactory) factory, proxy, proxyUsername, proxyPassword, host, port);
106            } else {
107                proxySocket = new Socket(proxy);
108                proxySocket.connect(new InetSocketAddress(host, port), connectTimeout);
109                socket = ((SSLSocketFactory) factory).createSocket(proxySocket, host, port, false);
110            }
111            socket.setSoTimeout(readTimeout);
112            socket.setKeepAlive(true);
113            final InputStream stream = socket.getInputStream();
114            return Utilities.parseFeedbackStream(stream);
115        } finally {
116            Utilities.close(socket);
117            Utilities.close(proxySocket);
118        }
119    }
120
121}