Path: blob/master/test/jdk/sun/net/www/protocol/http/6550798/TestCache.java
41161 views
/*1* Copyright (c) 2010, 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*/2223import java.net.*;24import java.io.*;25import java.io.IOException;26import java.io.InputStream;27import java.io.OutputStream;28import java.io.FileInputStream;29import java.io.FileOutputStream;30import java.io.BufferedInputStream;31import java.io.ByteArrayInputStream;32import java.io.PrintStream;33import java.io.InputStream;34import java.io.File;35import java.net.CacheRequest;36import java.net.CacheResponse;37import java.net.ResponseCache;38import java.net.URI;39import java.net.URL;40import java.net.URLConnection;41import java.util.ArrayList;42import java.util.HashMap;43import java.util.Map;44import java.util.List;45import java.util.Iterator;46import java.util.StringTokenizer;47import java.util.jar.JarInputStream;48import java.util.jar.JarFile;49import java.security.AccessController;50import java.security.PrivilegedAction;51import java.security.PrivilegedExceptionAction;52import java.security.PrivilegedActionException;53import java.security.Principal;54import java.security.cert.Certificate;55import javax.net.ssl.SSLPeerUnverifiedException;5657public class TestCache extends java.net.ResponseCache {58private boolean inCacheHandler = false;59private boolean _downloading = false;6061public static volatile boolean fail = false;6263public static void reset() {64// Set system wide cache handler65System.out.println("install deploy cache handler");66ResponseCache.setDefault(new TestCache());67}6869public synchronized CacheResponse get(final URI uri, String rqstMethod,70Map requestHeaders) throws IOException {71System.out.println("get: " + uri);72Thread.currentThread().dumpStack();73return null;74}7576public synchronized CacheRequest put(URI uri, URLConnection conn)77throws IOException {78System.out.println("put: " + uri);79Thread.currentThread().dumpStack();80URL url = uri.toURL();81return new DeployCacheRequest(url, conn);8283}84}8586class DeployByteArrayOutputStream extends java.io.ByteArrayOutputStream {8788private URL _url;89private URLConnection _conn;9091DeployByteArrayOutputStream(URL url, URLConnection conn) {92_url = url;93_conn = conn;94}959697public void close() throws IOException {9899System.out.println("contentLength: " + _conn.getContentLength());100System.out.println("byte array size: " + size());101if ( _conn.getContentLength() == size()) {102System.out.println("correct content length");103} else {104System.out.println("wrong content length");105System.out.println("TEST FAILED");106TestCache.fail = true;107}108super.close();109}110}111112class DeployCacheRequest extends java.net.CacheRequest {113114private URL _url;115private URLConnection _conn;116private boolean _downloading = false;117118DeployCacheRequest(URL url, URLConnection conn) {119System.out.println("DeployCacheRequest ctor for: " + url);120_url = url;121_conn = conn;122}123124public void abort() {125System.out.println("abort called");126}127128public OutputStream getBody() throws IOException {129System.out.println("getBody called");130return new DeployByteArrayOutputStream(_url, _conn);131}132}133134135