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
0N/A * published by the Free Software Foundation.
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/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.security.*;
0N/Aimport javax.net.ssl.*;
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/A/**
0N/A * Implements a basic static content HTTP server
0N/A * which understands text/html, text/plain content types
0N/A *
0N/A * Must be given an abs pathname to the document root.
0N/A * Directory listings together with text + html files
0N/A * can be served.
0N/A */
0N/Apublic class FileServerHandler implements HttpHandler {
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A if (args.length != 3) {
0N/A System.out.println ("usage: java FileServerHandler rootDir port logfilename");
0N/A System.exit(1);
0N/A }
0N/A String rootDir = args[0];
0N/A int port = Integer.parseInt (args[1]);
0N/A String logfile = args[2];
0N/A HttpServer server = HttpServer.create (new InetSocketAddress (8000), 0);
0N/A HttpHandler h = new FileServerHandler (rootDir);
0N/A
0N/A HttpContext c = server.createContext ("/", h);
0N/A c.getFilters().add (new LogFilter (new File (logfile)));
0N/A server.setExecutor (Executors.newCachedThreadPool());
0N/A server.start ();
0N/A }
0N/A
0N/A String docroot;
0N/A
0N/A FileServerHandler (String docroot) {
0N/A this.docroot = docroot;
0N/A }
0N/A
0N/A int invocation = 1;
0N/A public void handle (HttpExchange t)
0N/A throws IOException
0N/A {
0N/A InputStream is = t.getRequestBody();
0N/A Headers map = t.getRequestHeaders();
0N/A Headers rmap = t.getResponseHeaders();
0N/A URI uri = t.getRequestURI();
0N/A String path = uri.getPath();
0N/A
0N/A while (is.read () != -1) ;
0N/A is.close();
0N/A File f = new File (docroot, path);
0N/A if (!f.exists()) {
0N/A notfound (t, path);
0N/A return;
0N/A }
0N/A String fixedrequest = map.getFirst ("XFixed");
0N/A
0N/A String method = t.getRequestMethod();
0N/A if (method.equals ("HEAD")) {
0N/A rmap.set ("Content-Length", Long.toString (f.length()));
0N/A t.sendResponseHeaders (200, -1);
0N/A t.close();
0N/A } else if (!method.equals("GET")) {
0N/A t.sendResponseHeaders (405, -1);
0N/A t.close();
0N/A return;
0N/A }
0N/A
0N/A if (path.endsWith (".html") || path.endsWith (".htm")) {
0N/A rmap.set ("Content-Type", "text/html");
0N/A } else {
0N/A rmap.set ("Content-Type", "text/plain");
0N/A }
0N/A if (f.isDirectory()) {
0N/A if (!path.endsWith ("/")) {
0N/A moved (t);
0N/A return;
0N/A }
0N/A rmap.set ("Content-Type", "text/html");
0N/A t.sendResponseHeaders (200, 0);
0N/A String[] list = f.list();
0N/A OutputStream os = t.getResponseBody();
0N/A PrintStream p = new PrintStream (os);
0N/A p.println ("<h2>Directory listing for: " + path+ "</h2>");
0N/A p.println ("<ul>");
0N/A for (int i=0; i<list.length; i++) {
0N/A p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
0N/A }
0N/A p.println ("</ul><p><hr>");
0N/A p.flush();
0N/A p.close();
0N/A } else {
0N/A int clen;
0N/A if (fixedrequest != null) {
0N/A clen = (int) f.length();
0N/A } else {
0N/A clen = 0;
0N/A }
0N/A t.sendResponseHeaders (200, clen);
0N/A OutputStream os = t.getResponseBody();
0N/A FileInputStream fis = new FileInputStream (f);
0N/A int count = 0;
0N/A try {
0N/A byte[] buf = new byte [16 * 1024];
0N/A int len;
0N/A while ((len=fis.read (buf)) != -1) {
0N/A os.write (buf, 0, len);
0N/A count += len;
0N/A }
0N/A } catch (IOException e) {
0N/A e.printStackTrace();
0N/A }
0N/A fis.close();
0N/A os.close();
0N/A }
0N/A }
0N/A
0N/A void moved (HttpExchange t) throws IOException {
0N/A Headers req = t.getRequestHeaders();
0N/A Headers map = t.getResponseHeaders();
0N/A URI uri = t.getRequestURI();
0N/A String host = req.getFirst ("Host");
0N/A String location = "http://"+host+uri.getPath() + "/";
0N/A map.set ("Content-Type", "text/html");
0N/A map.set ("Location", location);
0N/A t.sendResponseHeaders (301, -1);
0N/A t.close();
0N/A }
0N/A
0N/A void notfound (HttpExchange t, String p) throws IOException {
0N/A t.getResponseHeaders().set ("Content-Type", "text/html");
0N/A t.sendResponseHeaders (404, 0);
0N/A OutputStream os = t.getResponseBody();
0N/A String s = "<h2>File not found</h2>";
0N/A s = s + p + "<p>";
0N/A os.write (s.getBytes());
0N/A os.close();
0N/A t.close();
0N/A }
0N/A }