jaxp/test/javax/xml/jaxp/libs/jaxp/library/SimpleHttpServer.java
author joehw
Wed, 11 Jan 2017 13:06:04 -0800
changeset 43121 e73af7b6ce47
permissions -rw-r--r--
8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar Reviewed-by: rriggs, dfuchs, lancea, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43121
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     1
/*
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     4
 *
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     8
 *
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    13
 * accompanied this code).
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    14
 *
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    18
 *
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    21
 * questions.
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    22
 */
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    23
package jaxp.library;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    24
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    25
import com.sun.net.httpserver.Headers;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    26
import com.sun.net.httpserver.HttpContext;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    27
import com.sun.net.httpserver.HttpExchange;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    28
import com.sun.net.httpserver.HttpHandler;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    29
import com.sun.net.httpserver.HttpServer;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    30
import java.io.File;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    31
import java.io.FileInputStream;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    32
import java.io.IOException;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    33
import java.io.InputStream;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    34
import java.io.OutputStream;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    35
import java.net.InetSocketAddress;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    36
import java.net.URI;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    37
import java.util.concurrent.ExecutorService;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    38
import java.util.concurrent.Executors;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    39
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    40
/**
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    41
 * A simple HTTP Server
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    42
 */
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    43
public class SimpleHttpServer {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    44
    HttpServer _httpserver;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    45
    ExecutorService _executor;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    46
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    47
    String _address;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    48
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    49
    String _context, _docroot;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    50
    int _port;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    51
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    52
    public SimpleHttpServer(String context, String docroot) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    53
        //let the system pick up an ephemeral port in a bind operation
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    54
        this(0, context, docroot);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    55
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    56
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    57
    public SimpleHttpServer(int port, String context, String docroot) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    58
        _port = port;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    59
        _context = context;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    60
        _docroot = docroot;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    61
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    62
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    63
    public void start() {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    64
        MyHttpHandler handler = new MyHttpHandler(_docroot);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    65
        InetSocketAddress addr = new InetSocketAddress(_port);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    66
        try {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    67
            _httpserver = HttpServer.create(addr, 0);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    68
        } catch (IOException ex) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    69
            throw new RuntimeException("cannot create httpserver", ex);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    70
        }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    71
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    72
        //TestHandler is mapped to /test
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    73
        HttpContext ctx = _httpserver.createContext(_context, handler);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    74
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    75
        _executor = Executors.newCachedThreadPool();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    76
        _httpserver.setExecutor(_executor);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    77
        _httpserver.start();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    78
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    79
        _address = "http://localhost:" + _httpserver.getAddress().getPort();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    80
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    81
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    82
    public void stop() {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    83
        _httpserver.stop(2);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    84
        _executor.shutdown();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    85
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    86
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    87
    public String getAddress() {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    88
        return _address;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    89
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    90
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    91
    static class MyHttpHandler implements HttpHandler {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    92
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    93
        String _docroot;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    94
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    95
        public MyHttpHandler(String docroot) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    96
            _docroot = docroot;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    97
        }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    98
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
    99
        public void handle(HttpExchange t)
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   100
                throws IOException {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   101
            InputStream is = t.getRequestBody();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   102
            Headers map = t.getRequestHeaders();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   103
            Headers rmap = t.getResponseHeaders();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   104
            OutputStream os = t.getResponseBody();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   105
            URI uri = t.getRequestURI();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   106
            String path = uri.getPath();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   107
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   108
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   109
            while (is.read() != -1) ;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   110
            is.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   111
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   112
            File f = new File(_docroot, path);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   113
            if (!f.exists()) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   114
                notfound(t, path);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   115
                return;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   116
            }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   117
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   118
            String method = t.getRequestMethod();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   119
            if (method.equals("HEAD")) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   120
                rmap.set("Content-Length", Long.toString(f.length()));
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   121
                t.sendResponseHeaders(200, -1);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   122
                t.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   123
            } else if (!method.equals("GET")) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   124
                t.sendResponseHeaders(405, -1);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   125
                t.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   126
                return;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   127
            }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   128
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   129
            if (path.endsWith(".html") || path.endsWith(".htm")) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   130
                rmap.set("Content-Type", "text/html");
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   131
            } else {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   132
                rmap.set("Content-Type", "text/plain");
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   133
            }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   134
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   135
            t.sendResponseHeaders (200, f.length());
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   136
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   137
            FileInputStream fis = new FileInputStream(f);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   138
            int count = 0;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   139
            try {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   140
                byte[] buf = new byte[16 * 1024];
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   141
                int len;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   142
                while ((len = fis.read(buf)) != -1) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   143
                    os.write(buf, 0, len);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   144
                    count += len;
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   145
                }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   146
            } catch (IOException e) {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   147
                e.printStackTrace();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   148
            }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   149
            fis.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   150
            os.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   151
        }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   152
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   153
        void moved(HttpExchange t) throws IOException {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   154
            Headers req = t.getRequestHeaders();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   155
            Headers map = t.getResponseHeaders();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   156
            URI uri = t.getRequestURI();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   157
            String host = req.getFirst("Host");
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   158
            String location = "http://" + host + uri.getPath() + "/";
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   159
            map.set("Content-Type", "text/html");
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   160
            map.set("Location", location);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   161
            t.sendResponseHeaders(301, -1);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   162
            t.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   163
        }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   164
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   165
        void notfound(HttpExchange t, String p) throws IOException {
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   166
            t.getResponseHeaders().set("Content-Type", "text/html");
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   167
            t.sendResponseHeaders(404, 0);
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   168
            OutputStream os = t.getResponseBody();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   169
            String s = "<h2>File not found</h2>";
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   170
            s = s + p + "<p>";
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   171
            os.write(s.getBytes());
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   172
            os.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   173
            t.close();
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   174
        }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   175
    }
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   176
e73af7b6ce47 8171243: CatalogManager.catalogResolver throws FileSystemNotFoundException with jar
joehw
parents:
diff changeset
   177
}