Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add constructor that takes an SSLContext (to allow full control over …

…SSL socket creation)
  • Loading branch information...
commit e704dde5db9e0fefe3ee63efd99a84cd76d8f24e 1 parent 3ada6c3
Tim Williamson authored
Showing with 41 additions and 13 deletions.
  1. +41 −13 src/main/java/com/mogwee/push/ApnsSocketFactory.java
View
54 src/main/java/com/mogwee/push/ApnsSocketFactory.java
@@ -43,8 +43,30 @@
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;
switch (type) {
@@ -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 {
@@ -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;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.