1/*
2 * Copyright (c) 2005, 2012, 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 6296310
27 * @modules java.base/sun.net.www
28 * @library ../../httptest/
29 * @build HttpCallback TestHttpServer HttpTransaction
30 * @run main/othervm B6296310
31 * @summary  REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
32 */
33
34import java.net.*;
35import java.io.*;
36import java.util.*;
37
38/*
39 * http server returns 200 and content-length=0
40 * Test will throw NPE if bug still exists
41 */
42
43public class B6296310
44{
45   static SimpleHttpTransaction httpTrans;
46   static TestHttpServer server;
47
48   public static void main(String[] args)
49   {
50      ResponseCache.setDefault(new MyCacheHandler());
51      startHttpServer();
52
53      makeHttpCall();
54   }
55
56   public static void startHttpServer() {
57      try {
58         httpTrans = new SimpleHttpTransaction();
59         server = new TestHttpServer(httpTrans, 1, 10, 0);
60      } catch (IOException e) {
61         e.printStackTrace();
62      }
63   }
64
65   public static void makeHttpCall() {
66      try {
67         System.out.println("http server listen on: " + server.getLocalPort());
68         URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
69                            server.getLocalPort(), "/");
70         HttpURLConnection uc = (HttpURLConnection)url.openConnection();
71         System.out.println(uc.getResponseCode());
72      } catch (IOException e) {
73         e.printStackTrace();
74      } finally {
75         server.terminate();
76      }
77   }
78}
79
80class SimpleHttpTransaction implements HttpCallback
81{
82   /*
83    * Our http server which simply retruns a file with no content
84    */
85   public void request(HttpTransaction trans) {
86      try {
87         trans.setResponseEntityBody("");
88         trans.sendResponse(200, "OK");
89      } catch (Exception e) {
90         e.printStackTrace();
91      }
92   }
93}
94
95class MyCacheHandler extends ResponseCache
96{
97   public CacheResponse get(URI uri, String rqstMethod, Map rqstHeaders)
98   {
99      return null;
100   }
101
102   public CacheRequest put(URI uri, URLConnection conn)
103   {
104      return new MyCacheRequest();
105   }
106}
107
108class MyCacheRequest extends CacheRequest
109{
110   public void abort() {}
111
112   public OutputStream getBody() throws IOException {
113       return null;
114   }
115}
116