blob: ddca5ad835ed10be59f475f0b02b531d0a5f1acf [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/**
27 Provides a simple high-level Http server API, which can be used to build
28 embedded HTTP servers. Both "http" and "https" are supported. The API provides
29 a partial implementation of RFC <a href="http://www.ietf.org/rfc/rfc2616.txt">2616</a> (HTTP 1.1)
30 and RFC <a href="http://www.ietf.org/rfc/rfc2818.txt">2818</a> (HTTP over TLS).
31 Any HTTP functionality not provided by this API can be implemented by application code
32 using the API.
33 <p>
34 Programmers must implement the {@link com.sun.net.httpserver.HttpHandler} interface. This interface
35 provides a callback which is invoked to handle incoming requests from clients.
36 A HTTP request and its response is known as an exchange. HTTP exchanges are
37 represented by the {@link com.sun.net.httpserver.HttpExchange} class.
38 The {@link com.sun.net.httpserver.HttpServer} class is used to listen for incoming TCP connections
39 and it dispatches requests on these connections to handlers which have been
40 registered with the server.
41 <p>
42 A minimal Http server example is shown below:
43 <blockquote><pre>
44 class MyHandler implements HttpHandler {
45 public void handle(HttpExchange t) throws IOException {
46 InputStream is = t.getRequestBody();
47 read(is); // .. read the request body
48 String response = "This is the response";
49 t.sendResponseHeaders(200, response.length());
50 OutputStream os = t.getResponseBody();
51 os.write(response.getBytes());
52 os.close();
53 }
54 }
55 ...
56
57 HttpServer server = HttpServer.create(new InetSocketAddress(8000));
58 server.createContext("/applications/myapp", new MyHandler());
59 server.setExecutor(null); // creates a default executor
60 server.start();
61 </blockquote></pre>
62 <p>The example above creates a simple HttpServer which uses the calling
63 application thread to invoke the handle() method for incoming http
64 requests directed to port 8000, and to the path /applications/myapp/.
65 <p>
66 The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to
67 process incoming requests and to generate appropriate responses.
68 <p>
69 Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and
70 {@link com.sun.net.httpserver.Filter}
71 objects can be added to the returned context. Filters are used to perform automatic pre- and
72 post-processing of exchanges before they are passed to the exchange handler.
73 <p>
74 For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can
75 be used to process "https" requests secured by the SSL or TLS protocols.
76 A HttpsServer must be provided with a
77 {@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an
78 initialized {@link javax.net.ssl.SSLContext}.
79 HttpsConfigurator can be used to configure the
80 cipher suites and other SSL operating parameters.
81 A simple example SSLContext could be created as follows:
82 <blockquote><pre>
83 char[] passphrase = "passphrase".toCharArray();
84 KeyStore ks = KeyStore.getInstance("JKS");
85 ks.load(new FileInputStream("testkeys"), passphrase);
86
87 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
88 kmf.init(ks, passphrase);
89
90 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
91 tmf.init(ks);
92
93 SSLContext ssl = SSLContext.getInstance("TLS");
94 ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
95 </blockquote></pre>
96 <p>
97 In the example above, a keystore file called "testkeys", created with the keytool utility
98 is used as a certificate store for client and server certificates.
99 The following code shows how the SSLContext is then used in a HttpsConfigurator
100 and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.
101 <blockquote><pre>
102 server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
103 public void configure (HttpsParameters params) {
104
105 // get the remote address if needed
106 InetSocketAddress remote = params.getClientAddress();
107
108 SSLContext c = getSSLContext();
109
110 // get the default parameters
111 SSLParameters sslparams = c.getDefaultSSLParameters();
112 if (remote.equals (...) ) {
113 // modify the default set for client x
114 }
115
116 params.setSSLParameters(sslparams);
117 // statement above could throw IAE if any params invalid.
118 // eg. if app has a UI and parameters supplied by a user.
119
120 }
121 });
122 </blockquote></pre>
123 <p>
124 @since 1.6
125 */
126package com.sun.net.httpserver;