Search in sources :

Example 1 with NetworkIOException

use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.

the class ApnsConnectionImpl method getOrCreateSocket.

private synchronized Socket getOrCreateSocket(boolean resend) throws NetworkIOException {
    if (reconnectPolicy.shouldReconnect()) {
        logger.debug("Reconnecting due to reconnectPolicy dictating it");
        Utilities.close(socket);
        socket = null;
    }
    if (socket == null || socket.isClosed()) {
        try {
            if (proxy == null) {
                socket = factory.createSocket(host, port);
                logger.debug("Connected new socket {}", socket);
            } else if (proxy.type() == Proxy.Type.HTTP) {
                TlsTunnelBuilder tunnelBuilder = new TlsTunnelBuilder();
                socket = tunnelBuilder.build((SSLSocketFactory) factory, proxy, proxyUsername, proxyPassword, host, port);
                logger.debug("Connected new socket through http tunnel {}", socket);
            } else {
                boolean success = false;
                Socket proxySocket = null;
                try {
                    proxySocket = new Socket(proxy);
                    proxySocket.connect(new InetSocketAddress(host, port), connectTimeout);
                    socket = ((SSLSocketFactory) factory).createSocket(proxySocket, host, port, false);
                    success = true;
                } finally {
                    if (!success) {
                        Utilities.close(proxySocket);
                    }
                }
                logger.debug("Connected new socket through socks tunnel {}", socket);
            }
            socket.setSoTimeout(readTimeout);
            socket.setKeepAlive(true);
            if (errorDetection) {
                monitorSocket(socket);
            }
            reconnectPolicy.reconnected();
            logger.debug("Made a new connection to APNS");
        } catch (IOException e) {
            logger.error("Couldn't connect to APNS server", e);
            // indicate to clients whether this is a resend or initial send
            throw new NetworkIOException(e, resend);
        }
    }
    return socket;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NetworkIOException(com.notnoop.exceptions.NetworkIOException) IOException(java.io.IOException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Socket(java.net.Socket) NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 2 with NetworkIOException

use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.

the class ApnsConnectionImpl method sendMessage.

private synchronized void sendMessage(ApnsNotification m, boolean fromBuffer) throws NetworkIOException {
    logger.debug("sendMessage {} fromBuffer: {}", m, fromBuffer);
    if (delegate instanceof StartSendingApnsDelegate) {
        ((StartSendingApnsDelegate) delegate).startSending(m, fromBuffer);
    }
    int attempts = 0;
    while (true) {
        try {
            attempts++;
            Socket socket = getOrCreateSocket(fromBuffer);
            socket.getOutputStream().write(m.marshall());
            socket.getOutputStream().flush();
            cacheNotification(m);
            delegate.messageSent(m, fromBuffer);
            //logger.debug("Message \"{}\" sent", m);
            attempts = 0;
            break;
        } catch (SSLHandshakeException e) {
            // No use retrying this, it's dead Jim
            throw new NetworkIOException(e);
        } catch (IOException e) {
            Utilities.close(socket);
            if (attempts >= RETRIES) {
                logger.error("Couldn't send message after " + RETRIES + " retries." + m, e);
                delegate.messageSendFailed(m, e);
                Utilities.wrapAndThrowAsRuntimeException(e);
            }
            if (attempts != 1) {
                logger.info("Failed to send message " + m + "... trying again after delay", e);
                Utilities.sleep(DELAY_IN_MS);
            }
        }
    }
}
Also used : StartSendingApnsDelegate(com.notnoop.apns.StartSendingApnsDelegate) NetworkIOException(com.notnoop.exceptions.NetworkIOException) IOException(java.io.IOException) Socket(java.net.Socket) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 3 with NetworkIOException

use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.

the class ApnsConnectionResendTest method assertNetworkIoExForRedelivery.

private void assertNetworkIoExForRedelivery(ApnsNotification notification, MessageSentFailedRecord failed) {
    failed.assertRecord(notification, new NetworkIOException());
    final NetworkIOException found = failed.getException();
    assertTrue(found.isResend());
}
Also used : NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 4 with NetworkIOException

use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.

the class QueuedApnsService method start.

public void start() {
    if (started.getAndSet(true)) {
        // So it is returning immediately here
        return;
    }
    service.start();
    shouldContinue = true;
    thread = threadFactory.newThread(new Runnable() {

        public void run() {
            while (shouldContinue) {
                try {
                    ApnsNotification msg = queue.take();
                    service.push(msg);
                } catch (InterruptedException e) {
                // ignore
                } catch (NetworkIOException e) {
                // ignore: failed connect...
                } catch (Exception e) {
                    // weird if we reached here - something wrong is happening, but we shouldn't stop the service anyway!
                    logger.warn("Unexpected message caught... Shouldn't be here", e);
                }
            }
        }
    });
    thread.start();
}
Also used : ApnsNotification(com.notnoop.apns.ApnsNotification) NetworkIOException(com.notnoop.exceptions.NetworkIOException) NetworkIOException(com.notnoop.exceptions.NetworkIOException)

Example 5 with NetworkIOException

use of com.notnoop.exceptions.NetworkIOException in project java-apns by notnoop.

the class ApnsConnectionTest method sendOneSimpleClientCertFail.

@Test(timeout = 2000)
public void sendOneSimpleClientCertFail() throws InterruptedException {
    ApnsService service = APNS.newService().withSSLContext(clientMultiKeyContext("notused")).withGatewayDestination(LOCALHOST, gatewayPort).build();
    server.stopAt(msg1.length());
    try {
        service.push(msg1);
        fail();
    } catch (NetworkIOException e) {
        assertTrue("Expected bad_certifcate exception", e.getMessage().contains("bad_certificate"));
    }
}
Also used : ApnsService(com.notnoop.apns.ApnsService) NetworkIOException(com.notnoop.exceptions.NetworkIOException) Test(org.junit.Test)

Aggregations

NetworkIOException (com.notnoop.exceptions.NetworkIOException)5 IOException (java.io.IOException)2 Socket (java.net.Socket)2 ApnsNotification (com.notnoop.apns.ApnsNotification)1 ApnsService (com.notnoop.apns.ApnsService)1 StartSendingApnsDelegate (com.notnoop.apns.StartSendingApnsDelegate)1 InetSocketAddress (java.net.InetSocketAddress)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Test (org.junit.Test)1