View Javadoc
1   package au.gov.amsa.ais;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   import java.io.OutputStream;
7   import java.net.Socket;
8   import java.net.UnknownHostException;
9   import java.nio.charset.StandardCharsets;
10  import java.security.KeyManagementException;
11  import java.security.NoSuchAlgorithmException;
12  import java.security.cert.CertificateException;
13  import java.security.cert.X509Certificate;
14  import java.util.concurrent.CountDownLatch;
15  import java.util.concurrent.ExecutorService;
16  import java.util.concurrent.Executors;
17  
18  import javax.net.ssl.SSLContext;
19  import javax.net.ssl.SSLEngine;
20  import javax.net.ssl.SSLSocketFactory;
21  import javax.net.ssl.TrustManager;
22  import javax.net.ssl.X509ExtendedTrustManager;
23  
24  import au.gov.amsa.util.nmea.NmeaUtil;
25  
26  public final class MariwebSslConnectMain {
27  
28      public static final String CONTEXT_SSL = "SSL";
29  
30      public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException,
31              UnknownHostException, IOException, InterruptedException {
32  
33          String host = checkNotNull(System.getProperty("host"), "host cannot be null");
34          int port = Integer.parseInt(checkNotNull(System.getProperty("port"), "port cannot be null"));
35          String password = checkNotNull(System.getProperty("password"), "password cannot be null");
36          String username = checkNotNull(System.getProperty("username"), "username cannot be null");
37          if (password == null)
38              throw new IllegalArgumentException("you must set the 'password' system property");
39  
40          SSLContext context = setAlwaysHappyTrustManager(false);
41          SSLSocketFactory factory = context.getSocketFactory();
42          // Socket socket = factory.createSocket("mariweb.amsa.gov.au", 9005);
43  
44          Socket socket = factory.createSocket(host, port);
45  
46          CountDownLatch latch = new CountDownLatch(1);
47          ExecutorService executor = Executors.newSingleThreadExecutor();
48  
49          executor.submit(() -> read(socket, latch));
50  
51          System.out.println("sleeping");
52          Thread.sleep(5000);
53  
54          OutputStream out = socket.getOutputStream();
55          System.out.println("sending command");
56  
57          String command = "$PMWLSS," + System.currentTimeMillis() / 1000 + ",4," + username + "," + password + ",1*";
58          String checksum = NmeaUtil.getChecksum(command);
59          command = command + checksum + "\r\n";
60  
61          out.write(command.getBytes(StandardCharsets.UTF_8));
62          out.flush();
63          System.out.println(command);
64  
65          latch.await();
66  
67      }
68  
69      private static void read(Socket socket, CountDownLatch latch) {
70          try {
71              System.out.println("starting reader");
72              BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
73              String line;
74              while ((line = br.readLine()) != null) {
75                  System.out.println(line);
76              }
77          } catch (Exception e) {
78              e.printStackTrace();
79          }
80          latch.countDown();
81      }
82  
83      private static SSLContext setAlwaysHappyTrustManager(boolean certificateMustBeValid)
84              throws NoSuchAlgorithmException, KeyManagementException {
85          SSLContext sslContext = SSLContext.getInstance(CONTEXT_SSL);
86          TrustManager[] trustManagers = null;
87          if (!certificateMustBeValid) {
88              TrustManager trustManager = new AlwaysHappyTrustManager();
89              trustManagers = new TrustManager[] { trustManager };
90          }
91          sslContext.init(null, trustManagers, new java.security.SecureRandom());
92          return sslContext;
93      }
94  
95      private static class AlwaysHappyTrustManager extends X509ExtendedTrustManager {
96          @Override
97          public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
98              // don't throw exception
99          }
100 
101         @Override
102         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
103             // don't throw exception
104         }
105 
106         @Override
107         public final X509Certificate[] getAcceptedIssuers() {
108             return null;
109         }
110 
111         @Override
112         public void checkClientTrusted(X509Certificate[] arg0, String arg1, Socket arg2) throws CertificateException {
113             // don't throw exception
114         }
115 
116         @Override
117         public void checkClientTrusted(X509Certificate[] arg0, String arg1, SSLEngine arg2)
118                 throws CertificateException {
119             // don't throw exception
120 
121         }
122 
123         @Override
124         public void checkServerTrusted(X509Certificate[] arg0, String arg1, Socket arg2) throws CertificateException {
125             // don't throw exception
126 
127         }
128 
129         @Override
130         public void checkServerTrusted(X509Certificate[] arg0, String arg1, SSLEngine arg2)
131                 throws CertificateException {
132             // don't throw exception
133 
134         }
135     }
136 
137     private static <T> T checkNotNull(T t, String msg) {
138         if (t == null) {
139             throw new NullPointerException(msg);
140         } else {
141             return t;
142         }
143     }
144 
145 }