Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

61
LINES

< > BotCompany Repo | #1002344 // Test makeSecure in NanoHTTPD (enabling HTTPS)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2685L/21K/54K).

1  
!759
2  
3  
!include #1001651 // class NanoHTTP
4  
5  
//static int port = 443; // standard https
6  
static int port = 8765;
7  
  
8  
  static class MyHTTPD extends NanoHTTPD {
9  
    public MyHTTPD(int port) {
10  
      super(port);
11  
    }
12  
  
13  
    public Response serve(String uri, Method method,
14  
      Map<String, String> header, Map<String, String> parms,
15  
      Map<String, String> files) {
16  
      print("Serving URI: " + quote(uri));
17  
      try {
18  
        Response response = cast callOpt(getMainClass(), "serve", uri, method, header, parms, files);
19  
        if (response != null) ret response;
20  
        S html = callHtmlMethod(getMainClass(), uri);
21  
        if (html != null)
22  
          ret serveHTML(html);
23  
        ret serve404();
24  
      } catch (Throwable e) {
25  
        e.printStackTrace();
26  
        ret serveHTML("ERROR.");
27  
      }
28  
    }
29  
  }
30  
31  
  static MyHTTPD serveHttp_server;
32  
33  
static SSLServerSocketFactory makeSSLSocketFactory(File keystoreFile, char[] passphrase) throws IOException {
34  
        SSLServerSocketFactory res = null;
35  
        try {
36  
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
37  
            InputStream keystoreStream = new FileInputStream(keystoreFile);
38  
            keystore.load(keystoreStream, passphrase);
39  
            // TODO: close keystoreStream??
40  
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
41  
            trustManagerFactory.init(keystore);
42  
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
43  
            keyManagerFactory.init(keystore, passphrase);
44  
            SSLContext ctx = SSLContext.getInstance("TLS");
45  
            ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
46  
            res = ctx.getServerSocketFactory();
47  
        } catch (Exception e) {
48  
            throw new IOException(e.getMessage());
49  
        }
50  
        return res;
51  
    }
52  
53  
p {
54  
  File keystoreFile = new File("/home/stefan/dev/keystore-ssl/keystore.jks");
55  
  SSLServerSocketFactory factory = makeSSLSocketFactory(keystoreFile, args[0].toCharArray());
56  
  
57  
  serveHttp_server = new MyHTTPD(port);
58  
  serveHttp_server.makeSecure(factory);
59  
  serveHttp_server.start();
60  
  System.out.println("HTTPS server started (listening on port " + port + "!)");
61  
}

Author comment

Began life as a copy of #760

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002344
Snippet name: Test makeSecure in NanoHTTPD (enabling HTTPS)
Eternal ID of this version: #1002344/1
Text MD5: f20f780088c346c8e41d6312e872c2e1
Transpilation MD5: 7cccdd831543749ee827405e643b95d1
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-07 20:07:57
Source code size: 2388 bytes / 61 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1715 / 1500
Referenced in: [show references]