jdk/test/com/sun/net/httpserver/Test14.java
author duke
Wed, 05 Jul 2017 16:42:03 +0200
changeset 1266 c2036bf76829
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6270015
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary  Light weight HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
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
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Test filters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class Test14 extends Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final String test_input = "Hello world";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final String test_output = "Ifmmp!xpsme";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /* an outputstream which transforms the output data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * by adding one to each byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static class OffsetOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        OffsetOutputStream (OutputStream os) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            super (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            super.write (b+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static class OffsetFilter extends Filter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public String description() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return "Translates outgoing data";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public void destroy(HttpContext c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void init(HttpContext c) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public void doFilter (HttpExchange exchange, Filter.Chain chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            exchange.setStreams (null, new OffsetOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                exchange.getResponseBody()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            chain.doFilter (exchange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Handler handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        InetSocketAddress addr = new InetSocketAddress (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        HttpServer server = HttpServer.create (addr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        HttpContext ctx = server.createContext ("/test", handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        File logfile = new File (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.getProperty ("test.classes")+ "/log.txt"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ctx.getFilters().add (new OffsetFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ctx.getFilters().add (new LogFilter(logfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (ctx.getFilters().size() != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new RuntimeException ("wrong filter list size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        ExecutorService executor = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        server.setExecutor (executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.out.print ("Test14: " );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String output="";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        while ((x=is.read())!= -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            output = output + (char)x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        error = !output.equals (test_output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        server.stop(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        executor.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (error ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new RuntimeException ("test failed error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println ("OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static boolean error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static class Handler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int invocation = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            while (is.read () != -1) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            String response = test_input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            t.sendResponseHeaders (200, response.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            os.write (response.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}