Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test14.java
38855 views
/*1* Copyright (c) 2005, 2006, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 627001526* @summary Light weight HTTP server27*/2829import com.sun.net.httpserver.*;3031import java.util.*;32import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import java.security.*;36import javax.security.auth.callback.*;37import javax.net.ssl.*;3839/**40* Test filters41*/4243public class Test14 extends Test {4445static final String test_input = "Hello world";46static final String test_output = "Ifmmp!xpsme";4748/* an outputstream which transforms the output data49* by adding one to each byte50*/51static class OffsetOutputStream extends FilterOutputStream {52OffsetOutputStream (OutputStream os) {53super (os);54}55public void write (int b) throws IOException {56super.write (b+1);57}58}5960static class OffsetFilter extends Filter {61public String description() {62return "Translates outgoing data";63}6465public void destroy(HttpContext c) {}66public void init(HttpContext c) {}6768public void doFilter (HttpExchange exchange, Filter.Chain chain)69throws IOException {70exchange.setStreams (null, new OffsetOutputStream(71exchange.getResponseBody()72));73chain.doFilter (exchange);74}75}7677public static void main (String[] args) throws Exception {78Handler handler = new Handler();79InetSocketAddress addr = new InetSocketAddress (0);80HttpServer server = HttpServer.create (addr, 0);81HttpContext ctx = server.createContext ("/test", handler);8283File logfile = new File (84System.getProperty ("test.classes")+ "/log.txt"85);8687ctx.getFilters().add (new OffsetFilter());88ctx.getFilters().add (new LogFilter(logfile));89if (ctx.getFilters().size() != 2) {90throw new RuntimeException ("wrong filter list size");91}92ExecutorService executor = Executors.newCachedThreadPool();93server.setExecutor (executor);94server.start ();9596URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");97System.out.print ("Test14: " );98HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();99InputStream is = urlc.getInputStream();100int x = 0;101String output="";102while ((x=is.read())!= -1) {103output = output + (char)x;104}105error = !output.equals (test_output);106server.stop(2);107executor.shutdown();108if (error ) {109throw new RuntimeException ("test failed error");110}111System.out.println ("OK");112113}114115public static boolean error = false;116117static class Handler implements HttpHandler {118int invocation = 1;119public void handle (HttpExchange t)120throws IOException121{122InputStream is = t.getRequestBody();123Headers map = t.getRequestHeaders();124Headers rmap = t.getResponseHeaders();125while (is.read () != -1) ;126is.close();127String response = test_input;128t.sendResponseHeaders (200, response.length());129OutputStream os = t.getResponseBody();130os.write (response.getBytes());131t.close();132}133}134}135136137