com.tc.simple.apn.quicktests.Test.java Source code

Java tutorial

Introduction

Here is the source code for com.tc.simple.apn.quicktests.Test.java

Source

/*
 *  Copyright Tek Counsel LLC 2013
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package com.tc.simple.apn.quicktests;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.codec.binary.Hex;

import com.tc.simple.apn.APNErrors;

public class Test {

    /**
     * @param args
     */

    public static void main(String[] args) {
        SSLSocket socket = null;

        try {
            String host = "gateway.sandbox.push.apple.com";
            int port = 2195;

            String token = "de7f197546e41a76684f8e2d89f397ed165298d7772f4bd9b0f39c674b185b0f";
            System.out.println(token.toCharArray().length);

            //String token = "8cebc7c08f79fa62f0994eb4298387ff930857ff8d14a50de431559cf476b223";

            KeyStore keyStore = KeyStore.getInstance("PKCS12");

            keyStore.load(Test.class.getResourceAsStream("egram-dev-apn.p12"), "xxxxxxxxx".toCharArray());
            KeyManagerFactory keyMgrFactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyMgrFactory.init(keyStore, "xxxxxxxxx".toCharArray());

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
            SSLSocketFactory socketFactory = sslContext.getSocketFactory();

            socket = (SSLSocket) socketFactory.createSocket(host, port);
            String[] cipherSuites = socket.getSupportedCipherSuites();
            socket.setEnabledCipherSuites(cipherSuites);
            socket.startHandshake();

            char[] t = token.toCharArray();
            byte[] b = Hex.decodeHex(t);

            OutputStream outputstream = socket.getOutputStream();

            String payload = "{\"aps\":{\"alert\":\"yabadabadooo\"}}";

            int expiry = (int) ((System.currentTimeMillis() / 1000L) + 7200);

            ByteArrayOutputStream bout = new ByteArrayOutputStream();

            DataOutputStream dos = new DataOutputStream(bout);

            //command
            dos.writeByte(1);

            //id
            dos.writeInt(900);

            //expiry
            dos.writeInt(expiry);

            //token length.
            dos.writeShort(b.length);

            //token
            dos.write(b);

            //payload length
            dos.writeShort(payload.length());

            //payload.
            dos.write(payload.getBytes());

            byte[] byteMe = bout.toByteArray();

            socket.getOutputStream().write(byteMe);

            socket.setSoTimeout(900);
            InputStream in = socket.getInputStream();

            System.out.println(APNErrors.getError(in.read()));

            in.close();

            outputstream.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}