2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2012-11-02 15:50:11 +00:00
|
|
|
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @bug 6299712
|
|
|
|
* @library ../../httptest/
|
2012-09-07 15:30:17 +01:00
|
|
|
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
|
2007-12-01 00:00:00 +00:00
|
|
|
* @run main/othervm B6299712
|
|
|
|
* @summary NullPointerException in sun.net.www.protocol.http.HttpURLConnection.followRedirect
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.net.*;
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test Description:
|
|
|
|
* - main thread run as a http client
|
|
|
|
* - another thread runs a http server, which redirect the first call to "/redirect"
|
|
|
|
* and return '200 OK' for the successive call
|
|
|
|
* - a global ResponseCache instance is installed, which return DeployCacheResponse
|
|
|
|
* for url ends with "/redirect", i.e. the url redirected to by our simple http server,
|
|
|
|
* and null for other url.
|
|
|
|
* - the whole result is that the first call will be served by our simple
|
|
|
|
* http server and is redirected to "/redirect". The successive call will be done
|
|
|
|
* automatically by HttpURLConnection, which will be served by DeployCacheResponse.
|
|
|
|
* The NPE will be thrown on the second round if the bug is there.
|
|
|
|
*/
|
|
|
|
public class B6299712 {
|
|
|
|
static SimpleHttpTransaction httpTrans;
|
2012-09-07 15:30:17 +01:00
|
|
|
static TestHttpServer server;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
ResponseCache.setDefault(new DeployCacheHandler());
|
|
|
|
startHttpServer();
|
|
|
|
|
|
|
|
makeHttpCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void startHttpServer() {
|
|
|
|
try {
|
|
|
|
httpTrans = new SimpleHttpTransaction();
|
2012-09-07 15:30:17 +01:00
|
|
|
server = new TestHttpServer(httpTrans, 1, 10, 0);
|
2007-12-01 00:00:00 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void makeHttpCall() {
|
|
|
|
try {
|
|
|
|
System.out.println("http server listen on: " + server.getLocalPort());
|
|
|
|
URL url = new URL("http" , InetAddress.getLocalHost().getHostAddress(),
|
|
|
|
server.getLocalPort(), "/");
|
|
|
|
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
|
|
|
|
System.out.println(uc.getResponseCode());
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
server.terminate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SimpleHttpTransaction implements HttpCallback {
|
|
|
|
/*
|
|
|
|
* Our http server which simply redirect first call
|
|
|
|
*/
|
|
|
|
public void request(HttpTransaction trans) {
|
|
|
|
try {
|
|
|
|
String path = trans.getRequestURI().getPath();
|
|
|
|
if (path.equals("/")) {
|
|
|
|
// the first call, redirect it
|
|
|
|
String location = "/redirect";
|
|
|
|
trans.addResponseHeader("Location", location);
|
|
|
|
trans.sendResponse(302, "Moved Temporarily");
|
|
|
|
} else {
|
|
|
|
// the second call
|
|
|
|
trans.sendResponse(200, "OK");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeployCacheHandler extends java.net.ResponseCache {
|
|
|
|
private boolean inCacheHandler = false;
|
|
|
|
private boolean _downloading = false;
|
|
|
|
|
|
|
|
public synchronized CacheResponse get(final URI uri, String rqstMethod,
|
|
|
|
Map requestHeaders) throws IOException {
|
|
|
|
System.out.println("get!!!: " + uri);
|
|
|
|
try {
|
|
|
|
if (!uri.toString().endsWith("redirect")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DeployCacheResponse(new EmptyInputStream(), new HashMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized CacheRequest put(URI uri, URLConnection conn)
|
|
|
|
throws IOException {
|
|
|
|
URL url = uri.toURL();
|
|
|
|
return new DeployCacheRequest(url, conn);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeployCacheRequest extends java.net.CacheRequest {
|
|
|
|
|
|
|
|
private URL _url;
|
|
|
|
private URLConnection _conn;
|
|
|
|
private boolean _downloading = false;
|
|
|
|
|
|
|
|
DeployCacheRequest(URL url, URLConnection conn) {
|
|
|
|
_url = url;
|
|
|
|
_conn = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void abort() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public OutputStream getBody() throws IOException {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeployCacheResponse extends java.net.CacheResponse {
|
|
|
|
protected InputStream is;
|
|
|
|
protected Map headers;
|
|
|
|
|
|
|
|
DeployCacheResponse(InputStream is, Map headers) {
|
|
|
|
this.is = is;
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputStream getBody() throws IOException {
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map getHeaders() throws IOException {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmptyInputStream extends InputStream {
|
|
|
|
public EmptyInputStream() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public int read()
|
|
|
|
throws IOException {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|