0

Using the awesome Java APNS by notnoop. https://github.com/notnoop/java-apns

For some reason when I want to pull in my keystore the entire object that houses the APNS just blows up. Here it is below:

object Notification {
 val iosApnsDist = 
        APNS.newService()
        .withCert("/ipush.dist.p12", "password")
        .withSandboxDestination()
        .build()
}

For those that are familiar with Play!, files added to the conf folder are supposed to be available on the classpath. So I was a little confused as to why my reference would crash the app.

Below is a snippet from the APNS java source where the keystore is pulled in. Any thoughts?

public ApnsServiceBuilder withCert(String fileName, String password)
    throws RuntimeIOException, InvalidSSLConfig {
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(fileName);
            return withCert(stream, password);
        } catch (FileNotFoundException e) {
            throw new RuntimeIOException(e);
        } finally {
            Utilities.close(stream);
        }
    }

Update

I was able to extract an error message while running a try/catch during bootup. Basically, it can't find the file:

Caused by: com.notnoop.exceptions.RuntimeIOException: java.io.FileNotFoundException: \ipush.dev.p12 (The system cannot find the file specified)
        at com.notnoop.apns.ApnsServiceBuilder.withCert(ApnsServiceBuilder.java:116)
        at engine.logic.notification.Notification$.<init>(Notification.scala:61)

I can confirm the file is indeed in the /conf folder, so what's the cause?

jcern
  • 7,798
  • 4
  • 39
  • 47
crockpotveggies
  • 12,682
  • 12
  • 70
  • 140

1 Answers1

0

Found the cause of this. Take note that Play! adds files to the classpath but FileInputStream actually does not refer to paths directly on the classpath. Instead, it uses paths on the file system.

This previous StackOverflow will give the basics about getResourceAsStream vs. FileInputStream: getResourceAsStream() vs FileInputStream

So going back to the original problem...it helped to have a look at the source code for the APNS. Notice that it uses FileInputStream but since Play! only adds it to the classpath then the source needs to be modified to use ClassLoader.class.getResourceAsStream(fileName).

I suspect this will also apply to other open-source libraries out there as well.

So why did the object crash? Because without a little error-catching it never would have initialized properly.

Community
  • 1
  • 1
crockpotveggies
  • 12,682
  • 12
  • 70
  • 140