TestCache.java revision 2816:b3a4add96d45
1/*
2 * Copyright (c) 2010, 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
24import java.net.*;
25import java.io.*;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.OutputStream;
29import java.io.FileInputStream;
30import java.io.FileOutputStream;
31import java.io.BufferedInputStream;
32import java.io.ByteArrayInputStream;
33import java.io.PrintStream;
34import java.io.InputStream;
35import java.io.File;
36import java.net.CacheRequest;
37import java.net.CacheResponse;
38import java.net.ResponseCache;
39import java.net.URI;
40import java.net.URL;
41import java.net.URLConnection;
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.Map;
45import java.util.List;
46import java.util.Iterator;
47import java.util.StringTokenizer;
48import java.util.jar.JarInputStream;
49import java.util.jar.JarFile;
50import java.security.AccessController;
51import java.security.PrivilegedAction;
52import java.security.PrivilegedExceptionAction;
53import java.security.PrivilegedActionException;
54import java.security.Principal;
55import java.security.cert.Certificate;
56import javax.net.ssl.SSLPeerUnverifiedException;
57
58public class TestCache extends java.net.ResponseCache {
59    private boolean inCacheHandler = false;
60    private boolean _downloading = false;
61
62    public static volatile boolean fail = false;
63
64    public static void reset() {
65        // Set system wide cache handler
66        System.out.println("install deploy cache handler");
67        ResponseCache.setDefault(new TestCache());
68    }
69
70    public synchronized CacheResponse get(final URI uri, String rqstMethod,
71            Map requestHeaders) throws IOException {
72        System.out.println("get: " + uri);
73        Thread.currentThread().dumpStack();
74        return null;
75    }
76
77    public synchronized CacheRequest put(URI uri, URLConnection conn)
78    throws IOException {
79        System.out.println("put: " + uri);
80        Thread.currentThread().dumpStack();
81        URL url = uri.toURL();
82        return new DeployCacheRequest(url, conn);
83
84    }
85}
86
87class DeployByteArrayOutputStream extends java.io.ByteArrayOutputStream {
88
89    private URL _url;
90    private URLConnection _conn;
91
92    DeployByteArrayOutputStream(URL url, URLConnection conn) {
93        _url = url;
94        _conn = conn;
95    }
96
97
98    public void close() throws IOException {
99
100        System.out.println("contentLength: " + _conn.getContentLength());
101        System.out.println("byte array size: " + size());
102        if ( _conn.getContentLength() == size()) {
103            System.out.println("correct content length");
104        } else {
105            System.out.println("wrong content length");
106            System.out.println("TEST FAILED");
107            TestCache.fail = true;
108        }
109        super.close();
110    }
111}
112
113class DeployCacheRequest extends java.net.CacheRequest {
114
115    private URL _url;
116    private URLConnection _conn;
117    private boolean _downloading = false;
118
119    DeployCacheRequest(URL url, URLConnection conn) {
120        System.out.println("DeployCacheRequest ctor for: " + url);
121        _url = url;
122        _conn = conn;
123    }
124
125    public void abort() {
126        System.out.println("abort called");
127    }
128
129    public OutputStream getBody() throws IOException {
130        System.out.println("getBody called");
131        return new DeployByteArrayOutputStream(_url, _conn);
132    }
133}
134