Skip to content

Commit

Permalink
add constructor that takes an SSLContext (to allow full control over …
Browse files Browse the repository at this point in the history
…SSL socket creation)
  • Loading branch information
Tim Williamson committed Jan 31, 2012
1 parent 3ada6c3 commit e704dde
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/main/java/com/mogwee/push/ApnsSocketFactory.java
Expand Up @@ -43,7 +43,29 @@ public static enum Type
private final int port;
private final SSLSocketFactory socketFactory;

/**
* Creates a factory for creating sockets to send push notifications.
*
* @param keystore path to keystore file
* @param keystorePassword password for keystore
* @param keystoreType type of keystore, e.g., JKS
* @param type whether socket is for production or development
* @throws GeneralSecurityException if there's an error initializing the keystore or TLS
* @throws IOException if there's an error opening the keystore or creating the socket
*/
public ApnsSocketFactory(String keystore, String keystorePassword, String keystoreType, Type type) throws GeneralSecurityException, IOException
{
this(createContext(new File(keystore), keystorePassword, keystoreType), type);
}

/**
* Creates a factory for creating sockets to send push notifications.
*
* @param context context used to create the underlying socket
* @param type whether socket is for production or development
* @throws IOException if there's an error creating the socket
*/
public ApnsSocketFactory(SSLContext context, Type type) throws IOException
{
this.type = type;

Expand Down Expand Up @@ -77,13 +99,29 @@ public ApnsSocketFactory(String keystore, String keystorePassword, String keysto
throw new IllegalArgumentException("Unknown type: " + type);
}

this.socketFactory = context.getSocketFactory();
}

public Socket createSocket() throws IOException
{
return socketFactory.createSocket(host, port);
}

@Override
public String toString()
{
return "ApnsSocketFactory_" + type;
}

private static SSLContext createContext(File keystore, String keystorePassword, String keystoreType) throws GeneralSecurityException, IOException
{
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("sunx509");
KeyStore appleStore = KeyStore.getInstance("JKS");
InputStream appleStoreInputStream = null;

try {
// created by com.mogwee.push.CreateAppleCertificateKeystore (in tests)
appleStoreInputStream = getClass().getResourceAsStream("/apple.keystore");
appleStoreInputStream = ApnsSocketFactory.class.getResourceAsStream("/apple.keystore");
appleStore.load(appleStoreInputStream, "apple".toCharArray());
}
finally {
Expand All @@ -96,21 +134,11 @@ public ApnsSocketFactory(String keystore, String keystorePassword, String keysto
SSLContext context = SSLContext.getInstance("TLS");
char[] password = keystorePassword.toCharArray();
KeyStore.ProtectionParameter passwordProtection = new KeyStore.PasswordProtection(password);
KeyStore keyStore = KeyStore.Builder.newInstance(keystoreType, null, new File(keystore), passwordProtection).getKeyStore();
KeyStore keyStore = KeyStore.Builder.newInstance(keystoreType, null, keystore, passwordProtection).getKeyStore();

keyManagerFactory.init(keyStore, password);
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
this.socketFactory = context.getSocketFactory();
}

public Socket createSocket() throws IOException
{
return socketFactory.createSocket(host, port);
}

@Override
public String toString()
{
return "ApnsSocketFactory_" + type;
return context;
}
}

0 comments on commit e704dde

Please sign in to comment.