Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/package-info.java
41159 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26Provides a simple high-level Http server API, which can be used to build27embedded HTTP servers. Both "http" and "https" are supported. The API provides28a partial implementation of RFC <a href="https://www.ietf.org/rfc/rfc2616.txt">2616</a> (HTTP 1.1)29and RFC <a href="https://www.ietf.org/rfc/rfc2818.txt">2818</a> (HTTP over TLS).30Any HTTP functionality not provided by this API can be implemented by application code31using the API.32<p>33Programmers must implement the {@link com.sun.net.httpserver.HttpHandler} interface. This interface34provides a callback which is invoked to handle incoming requests from clients.35A HTTP request and its response is known as an exchange. HTTP exchanges are36represented by the {@link com.sun.net.httpserver.HttpExchange} class.37The {@link com.sun.net.httpserver.HttpServer} class is used to listen for incoming TCP connections38and it dispatches requests on these connections to handlers which have been39registered with the server.40<p>41A minimal Http server example is shown below:42<blockquote><pre>43class MyHandler implements HttpHandler {44public void handle(HttpExchange t) throws IOException {45InputStream is = t.getRequestBody();46read(is); // .. read the request body47String response = "This is the response";48t.sendResponseHeaders(200, response.length());49OutputStream os = t.getResponseBody();50os.write(response.getBytes());51os.close();52}53}54...5556HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);57server.createContext("/applications/myapp", new MyHandler());58server.setExecutor(null); // creates a default executor59server.start();60</pre></blockquote>61<p>The example above creates a simple HttpServer which uses the calling62application thread to invoke the handle() method for incoming http63requests directed to port 8000, and to the path /applications/myapp/.64<p>65The {@link com.sun.net.httpserver.HttpExchange} class encapsulates everything an application needs to66process incoming requests and to generate appropriate responses.67<p>68Registering a handler with a HttpServer creates a {@link com.sun.net.httpserver.HttpContext} object and69{@link com.sun.net.httpserver.Filter}70objects can be added to the returned context. Filters are used to perform automatic pre- and71post-processing of exchanges before they are passed to the exchange handler.72<p>73For sensitive information, a {@link com.sun.net.httpserver.HttpsServer} can74be used to process "https" requests secured by the SSL or TLS protocols.75A HttpsServer must be provided with a76{@link com.sun.net.httpserver.HttpsConfigurator} object, which contains an77initialized {@link javax.net.ssl.SSLContext}.78HttpsConfigurator can be used to configure the79cipher suites and other SSL operating parameters.80A simple example SSLContext could be created as follows:81<blockquote><pre>82char[] passphrase = "passphrase".toCharArray();83KeyStore ks = KeyStore.getInstance("JKS");84ks.load(new FileInputStream("testkeys"), passphrase);8586KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");87kmf.init(ks, passphrase);8889TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");90tmf.init(ks);9192SSLContext ssl = SSLContext.getInstance("TLS");93ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);94</pre></blockquote>95<p>96In the example above, a keystore file called "testkeys", created with the keytool utility97is used as a certificate store for client and server certificates.98The following code shows how the SSLContext is then used in a HttpsConfigurator99and how the SSLContext and HttpsConfigurator are linked to the HttpsServer.100<blockquote><pre>101server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {102public void configure (HttpsParameters params) {103104// get the remote address if needed105InetSocketAddress remote = params.getClientAddress();106107SSLContext c = getSSLContext();108109// get the default parameters110SSLParameters sslparams = c.getDefaultSSLParameters();111if (remote.equals (...) ) {112// modify the default set for client x113}114115params.setSSLParameters(sslparams);116// statement above could throw IAE if any params invalid.117// eg. if app has a UI and parameters supplied by a user.118119}120});121</pre></blockquote>122123@since 1.6124*/125package com.sun.net.httpserver;126127128