Skip to content

Commit

Permalink
initial support for proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
notnoop committed Feb 12, 2010
1 parent 40f1f42 commit 2fedaa1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/main/java/com/notnoop/apns/ApnsServiceBuilder.java
Expand Up @@ -33,6 +33,9 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -77,6 +80,7 @@ public class ApnsServiceBuilder {
private boolean isQueued = false;
private boolean isNonBlocking = false;
private ApnsDelegate delegate = ApnsDelegate.EMPTY;
private Socket proxySocket = null;

/**
* Constructs a new instance of {@code ApnsServiceBuilder}
Expand Down Expand Up @@ -240,6 +244,54 @@ public ApnsServiceBuilder withReconnectPolicy(ReconnectPolicy.Provided rp) {
return this;
}

/**
* 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 ApnsServiceBuilder 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 ApnsServiceBuilder withProxy(Proxy proxy) {
return withProxySocket(new Socket(proxy));
}

/**
* Specify the socket to be used as unlying socket to connect
* to the APN service.
*
* @param proxySocket the underlying socket for connections
* @return this
*/
public ApnsServiceBuilder withProxySocket(Socket proxySocket) {
this.proxySocket = proxySocket;
return this;
}

/**
* Constructs a pool of connections to the notification servers.
*
Expand Down Expand Up @@ -304,7 +356,7 @@ public ApnsService build() {
if (isNonBlocking) {
service = new MinaAdaptor(sslContext, gatewayHost, gatewaPort, feedback);
} else {
ApnsConnection conn = new ApnsConnectionImpl(sslFactory, gatewayHost, gatewaPort, reconnectPolicy, delegate);
ApnsConnection conn = new ApnsConnectionImpl(sslFactory, gatewayHost, gatewaPort, proxySocket, reconnectPolicy, delegate);
if (pooledMax != 1) {
conn = new ApnsPooledConnection(conn, pooledMax);
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/notnoop/apns/internal/ApnsConnectionImpl.java
Expand Up @@ -34,6 +34,7 @@
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,21 +50,32 @@ public class ApnsConnectionImpl implements ApnsConnection {
private final SocketFactory factory;
private final String host;
private final int port;
private final Socket underlyingSocket;
private final ReconnectPolicy reconnectPolicy;
private final ApnsDelegate delegate;

public ApnsConnectionImpl(SocketFactory factory, String host, int port) {
this(factory, host, port, new ReconnectPolicies.Never(), ApnsDelegate.EMPTY);
}

public ApnsConnectionImpl(SocketFactory factory, String host, int port, ReconnectPolicy reconnectPolicy, ApnsDelegate delegate) {
public ApnsConnectionImpl(SocketFactory factory, String host,
int port, ReconnectPolicy reconnectPolicy,
ApnsDelegate delegate) {
this(factory, host, port, null, reconnectPolicy, delegate);
}

public ApnsConnectionImpl(SocketFactory factory, String host,
int port, Socket underlyingSocket,
ReconnectPolicy reconnectPolicy, ApnsDelegate delegate) {
this.factory = factory;
this.host = host;
this.port = port;
this.reconnectPolicy = reconnectPolicy;
this.delegate = delegate == null ? ApnsDelegate.EMPTY : delegate;
this.underlyingSocket = underlyingSocket;
}


public synchronized void close() {
try {
if (socket != null)
Expand All @@ -85,7 +97,10 @@ private synchronized Socket socket() throws NetworkIOException {

if (socket == null || socket.isClosed()) {
try {
socket = factory.createSocket(host, port);
if (underlyingSocket == null)
socket = factory.createSocket(host, port);
else
socket = ((SSLSocketFactory)factory).createSocket(underlyingSocket, host, port, true);
reconnectPolicy.reconnected();
logger.debug("Made a new connection to APNS");
} catch (IOException e) {
Expand Down

0 comments on commit 2fedaa1

Please sign in to comment.