jdk/test/com/sun/net/httpserver/FileServerHandler.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 36131 379db4b2f95d
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Implements a basic static content HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * which understands text/html, text/plain content types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Must be given an abs pathname to the document root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Directory listings together with text + html files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * can be served.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class FileServerHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            if (args.length != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                System.out.println ("usage: java FileServerHandler rootDir port logfilename");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            String rootDir = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            int port = Integer.parseInt (args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            String logfile = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            HttpServer server = HttpServer.create (new InetSocketAddress (8000), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            HttpHandler h = new FileServerHandler (rootDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            HttpContext c = server.createContext ("/", h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            c.getFilters().add (new LogFilter (new File (logfile)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            server.setExecutor (Executors.newCachedThreadPool());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        String docroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        FileServerHandler (String docroot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.docroot = docroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int invocation = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            URI uri = t.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            while (is.read () != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            File f = new File (docroot, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                notfound (t, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            String fixedrequest = map.getFirst ("XFixed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            String method = t.getRequestMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (method.equals ("HEAD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                rmap.set ("Content-Length", Long.toString (f.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            } else if (!method.equals("GET")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                t.sendResponseHeaders (405, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (path.endsWith (".html") || path.endsWith (".htm")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                rmap.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                rmap.set ("Content-Type", "text/plain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (!path.endsWith ("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    moved (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                rmap.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                t.sendResponseHeaders (200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                String[] list = f.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                PrintStream p = new PrintStream (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                p.println ("<h2>Directory listing for: " + path+ "</h2>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                p.println ("<ul>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                for (int i=0; i<list.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                p.println ("</ul><p><hr>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                p.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                int clen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (fixedrequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    clen = (int) f.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    clen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                t.sendResponseHeaders (200, clen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                FileInputStream fis = new FileInputStream (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                byte[] buf = new byte [16 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                while ((len=fis.read (buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    os.write (buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        void moved (HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Headers req = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Headers map = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            URI uri = t.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            String host = req.getFirst ("Host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            String location = "http://"+host+uri.getPath() + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            map.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            map.set ("Location", location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            t.sendResponseHeaders (301, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        void notfound (HttpExchange t, String p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            t.getResponseHeaders().set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            t.sendResponseHeaders (404, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            String s = "<h2>File not found</h2>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            s = s + p + "<p>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }