1 /*
2  * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 6422914
27  * @library /test/lib
28  * @summary change httpserver exception printouts
29  * @run main TestLogging
30  * @run main/othervm -Djava.net.preferIPv6Addresses=true TestLogging
31  */
32 
33 import com.sun.net.httpserver.*;
34 
35 import java.util.*;
36 import java.util.concurrent.*;
37 import java.util.logging.*;
38 import java.io.*;
39 import java.net.*;
40 import java.security.*;
41 import java.security.cert.*;
42 import javax.net.ssl.*;
43 import jdk.test.lib.net.URIBuilder;
44 
45 public class TestLogging extends Test {
46 
main(String[] args)47     public static void main (String[] args) throws Exception {
48         HttpServer s1 = null;
49         ExecutorService executor=null;
50 
51         try {
52             System.out.print ("Test9: ");
53             String root = System.getProperty ("test.src")+ "/docs";
54             InetAddress loopback = InetAddress.getLoopbackAddress();
55             InetSocketAddress addr = new InetSocketAddress(loopback, 0);
56             Logger logger = Logger.getLogger ("com.sun.net.httpserver");
57             logger.setLevel (Level.ALL);
58             Handler h1 = new ConsoleHandler ();
59             h1.setLevel (Level.ALL);
60             logger.addHandler (h1);
61             s1 = HttpServer.create (addr, 0);
62             logger.info (root);
63             HttpHandler h = new FileServerHandler (root);
64             HttpContext c1 = s1.createContext ("/test1", h);
65             executor = Executors.newCachedThreadPool();
66             s1.setExecutor (executor);
67             s1.start();
68 
69             int p1 = s1.getAddress().getPort();
70 
71             URL url = URIBuilder.newBuilder()
72                 .scheme("http")
73                 .loopback()
74                 .port(p1)
75                 .path("/test1/smallfile.txt")
76                 .toURL();
77             System.out.println("URL: " + url);
78             HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
79             InputStream is = urlc.getInputStream();
80             while (is.read() != -1) ;
81             is.close();
82 
83             url = URIBuilder.newBuilder()
84                 .scheme("http")
85                 .loopback()
86                 .port(p1)
87                 .path("/test1/doesntexist.txt")
88                 .toURLUnchecked();
89             System.out.println("URL: " + url);
90             urlc = (HttpURLConnection)url.openConnection();
91             try {
92                 is = urlc.getInputStream();
93                 while (is.read() != -1) ;
94                 is.close();
95             } catch (IOException e) {
96                 System.out.println ("caught expected exception");
97             }
98 
99             Socket s = new Socket (InetAddress.getLoopbackAddress(), p1);
100             OutputStream os = s.getOutputStream();
101             //os.write ("GET xxx HTTP/1.1\r\n".getBytes());
102             os.write ("HELLO WORLD\r\n".getBytes());
103             is = s.getInputStream();
104             while (is.read() != -1) ;
105             os.close(); is.close(); s.close();
106             System.out.println ("OK");
107         } finally {
108             delay();
109             if (s1 != null)
110                 s1.stop(2);
111             if (executor != null)
112                 executor.shutdown();
113         }
114     }
115 }
116