0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A Provides a simple high-level Http server API, which can be used to build
0N/A embedded HTTP servers. Both "http" and "https" are supported. The API provides
0N/A a partial implementation of RFC <a href="http://www.ietf.org/rfc/rfc2616.txt">2616</a> (HTTP 1.1)
0N/A and RFC <a href="http://www.ietf.org/rfc/rfc2818.txt">2818</a> (HTTP over TLS).
0N/A Any HTTP functionality not provided by this API can be implemented by application code
0N/A using the API.
0N/A <p>
0N/A Programmers must implement the {@link com.sun.net.httpserver.HttpHandler} interface. This interface
0N/A provides a callback which is invoked to handle incoming requests from clients.
0N/A A HTTP request and its response is known as an exchange. HTTP exchanges are
0N/A represented by the {@link com.sun.net.httpserver.HttpExchange} class.
0N/A The {@link com.sun.net.httpserver.HttpServer} class is used to listen for incoming TCP connections
0N/A and it dispatches requests on these connections to handlers which have been
0N/A registered with the server.
0N/A <p>
0N/A A minimal Http server example is shown below:
0N/A <blockquote><pre>
0N/A class MyHandler implements HttpHandler {
0N/A public void handle(HttpExchange t) throws IOException {
0N/A InputStream is = t.getRequestBody();
0N/A read(is); // .. read the request body
0N/A String response = "This is the response";
0N/A t.sendResponseHeaders(200, response.length());
0N/A OutputStream os = t.getResponseBody();
0N/A os.write(response.getBytes());
0N/A os.close();
0N/A }
0N/A }
0N/A ...
0N/A
0N/A HttpServer server = HttpServer.create(new InetSocketAddress(8000));
0N/A server.createContext("/applications/myapp", new MyHandler());
0N/A server.setExecutor(null); // creates a default executor
0N/A server.start();
0N/A </blockquote></pre>
0N/A <p>The example above creates a simple HttpServer which uses the calling
0N/A application thread to invoke the handle() method for incoming http
0N/A requests directed to port 8000, and to the path /applications/myapp/.
0N/A <p>
0N/A The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to
0N/A process incoming requests and to generate appropriate responses.
0N/A <p>
0N/A Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and
0N/A {@link com.sun.net.httpserver.Filter}
0N/A objects can be added to the returned context. Filters are used to perform automatic pre- and
0N/A post-processing of exchanges before they are passed to the exchange handler.
0N/A <p>
0N/A For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can
0N/A be used to process "https" requests secured by the SSL or TLS protocols.
0N/A A HttpsServer must be provided with a
0N/A {@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an
0N/A initialized {@link javax.net.ssl.SSLContext}.
0N/A HttpsConfigurator can be used to configure the
0N/A cipher suites and other SSL operating parameters.
0N/A A simple example SSLContext could be created as follows:
0N/A <blockquote><pre>
0N/A char[] passphrase = "passphrase".toCharArray();
0N/A KeyStore ks = KeyStore.getInstance("JKS");
0N/A ks.load(new FileInputStream("testkeys"), passphrase);
0N/A
0N/A KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
0N/A kmf.init(ks, passphrase);
0N/A
0N/A TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
0N/A tmf.init(ks);
0N/A
0N/A SSLContext ssl = SSLContext.getInstance("TLS");
0N/A ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
0N/A </blockquote></pre>
0N/A <p>
0N/A In the example above, a keystore file called "testkeys", created with the keytool utility
0N/A is used as a certificate store for client and server certificates.
0N/A The following code shows how the SSLContext is then used in a HttpsConfigurator
0N/A and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.
0N/A <blockquote><pre>
0N/A server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
0N/A public void configure (HttpsParameters params) {
0N/A
0N/A // get the remote address if needed
0N/A InetSocketAddress remote = params.getClientAddress();
0N/A
0N/A SSLContext c = getSSLContext();
0N/A
0N/A // get the default parameters
0N/A SSLParameters sslparams = c.getDefaultSSLParameters();
0N/A if (remote.equals (...) ) {
0N/A // modify the default set for client x
0N/A }
0N/A
0N/A params.setSSLParameters(sslparams);
0N/A // statement above could throw IAE if any params invalid.
0N/A // eg. if app has a UI and parameters supplied by a user.
0N/A
0N/A }
0N/A });
0N/A </blockquote></pre>
0N/A <p>
0N/A @since 1.6
0N/A */
0N/Apackage com.sun.net.httpserver;