0N/A/*
3261N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Unit test for java.net.ResponseCache
0N/A * @bug 4837267
0N/A * @author Yingxian Wang
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/Aimport sun.net.www.ParseUtil;
0N/Aimport javax.net.ssl.*;
0N/A
0N/A/**
0N/A * Request should get serviced by the cache handler. Response get
0N/A * saved through the cache handler.
0N/A */
0N/Apublic class ResponseCacheTest implements Runnable {
0N/A ServerSocket ss;
0N/A static URL url1;
0N/A static URL url2;
0N/A static String FNPrefix, OutFNPrefix;
2612N/A static List<Closeable> streams = new ArrayList<>();
2612N/A static List<File> files = new ArrayList<>();
2612N/A
0N/A /*
0N/A * Our "http" server to return a 404 */
0N/A public void run() {
2612N/A Socket s = null;
2612N/A FileInputStream fis = null;
0N/A try {
2612N/A s = ss.accept();
0N/A
0N/A InputStream is = s.getInputStream ();
0N/A BufferedReader r = new BufferedReader(new InputStreamReader(is));
0N/A String x;
0N/A while ((x=r.readLine()) != null) {
0N/A if (x.length() ==0) {
0N/A break;
0N/A }
0N/A }
0N/A PrintStream out = new PrintStream(
0N/A new BufferedOutputStream(
0N/A s.getOutputStream() ));
0N/A
0N/A /* send file2.1 */
0N/A File file2 = new File(FNPrefix+"file2.1");
0N/A out.print("HTTP/1.1 200 OK\r\n");
0N/A out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
0N/A out.print("Content-Length: "+file2.length()+"\r\n");
0N/A out.print("Connection: close\r\n");
0N/A out.print("\r\n");
2612N/A fis = new FileInputStream(file2);
0N/A byte[] buf = new byte[(int)file2.length()];
0N/A int len;
0N/A while ((len = fis.read(buf)) != -1) {
0N/A out.print(new String(buf));
0N/A }
0N/A
0N/A out.flush();
0N/A
0N/A s.close();
0N/A ss.close();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
2612N/A } finally {
2612N/A try { ss.close(); } catch (IOException unused) {}
2612N/A try { s.close(); } catch (IOException unused) {}
2612N/A try { fis.close(); } catch (IOException unused) {}
0N/A }
0N/A }
0N/Astatic class NameVerifier implements HostnameVerifier {
0N/A public boolean verify(String hostname, SSLSession session) {
0N/A return true;
0N/A }
0N/A }
0N/A ResponseCacheTest() throws Exception {
0N/A /* start the server */
0N/A ss = new ServerSocket(0);
0N/A (new Thread(this)).start();
0N/A /* establish http connection to server */
0N/A url1 = new URL("http://localhost/file1.cache");
0N/A HttpURLConnection http = (HttpURLConnection)url1.openConnection();
0N/A InputStream is = null;
0N/A System.out.println("request headers: "+http.getRequestProperties());
0N/A System.out.println("responsecode is :"+http.getResponseCode());
0N/A Map<String,List<String>> headers1 = http.getHeaderFields();
0N/A try {
0N/A is = http.getInputStream();
0N/A } catch (IOException ioex) {
0N/A throw new RuntimeException(ioex.getMessage());
0N/A }
0N/A BufferedReader r = new BufferedReader(new InputStreamReader(is));
0N/A String x;
0N/A File fileout = new File(OutFNPrefix+"file1");
0N/A PrintStream out = new PrintStream(
0N/A new BufferedOutputStream(
0N/A new FileOutputStream(fileout)));
0N/A while ((x=r.readLine()) != null) {
0N/A out.print(x+"\n");
0N/A }
0N/A out.flush();
0N/A out.close();
0N/A
0N/A http.disconnect();
0N/A
0N/A // testing ResponseCacheHandler.put()
0N/A url2 = new URL("http://localhost:" +
0N/A Integer.toString(ss.getLocalPort())+"/file2.1");
0N/A http = (HttpURLConnection)url2.openConnection();
0N/A System.out.println("responsecode2 is :"+http.getResponseCode());
0N/A Map<String,List<String>> headers2 = http.getHeaderFields();
0N/A
0N/A try {
0N/A is = http.getInputStream();
0N/A } catch (IOException ioex) {
0N/A throw new RuntimeException(ioex.getMessage());
0N/A }
0N/A r = new BufferedReader(new InputStreamReader(is));
0N/A fileout = new File(OutFNPrefix+"file2.2");
0N/A out = new PrintStream(
0N/A new BufferedOutputStream(
0N/A new FileOutputStream(fileout)));
0N/A while ((x=r.readLine()) != null) {
0N/A out.print(x+"\n");
0N/A }
0N/A out.flush();
0N/A out.close();
0N/A
0N/A // assert (headers1 == headers2 && file1 == file2.2)
0N/A File file1 = new File(OutFNPrefix+"file1");
0N/A File file2 = new File(OutFNPrefix+"file2.2");
2612N/A files.add(file1);
2612N/A files.add(file2);
0N/A System.out.println("headers1"+headers1+"\nheaders2="+headers2);
0N/A if (!headers1.equals(headers2) || file1.length() != file2.length()) {
0N/A throw new RuntimeException("test failed");
0N/A }
0N/A }
2612N/A
0N/A public static void main(String args[]) throws Exception {
2244N/A try {
2244N/A ResponseCache.setDefault(new MyResponseCache());
2244N/A FNPrefix = System.getProperty("test.src", ".")+"/";
2244N/A OutFNPrefix = System.getProperty("test.scratch", ".")+"/";
2244N/A new ResponseCacheTest();
2244N/A } finally{
2244N/A ResponseCache.setDefault(null);
2612N/A for (Closeable c: streams) {
2612N/A try { c.close(); } catch (IOException unused) {}
2612N/A }
2612N/A for (File f: files) {
2612N/A f.delete();
2612N/A }
2244N/A }
0N/A }
0N/A
0N/A static class MyResponseCache extends ResponseCache {
0N/A public CacheResponse
0N/A get(URI uri, String rqstMethod, Map<String,List<String>> rqstHeaders)
0N/A throws IOException {
0N/A if (uri.equals(ParseUtil.toURI(url1))) {
0N/A return new MyCacheResponse(FNPrefix+"file1.cache");
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A public CacheRequest put(URI uri, URLConnection conn) throws IOException {
0N/A // save cache to file2.cache
0N/A // 1. serialize headers into file2.cache
0N/A // 2. write data to file2.cache
0N/A return new MyCacheRequest(OutFNPrefix+"file2.cache", conn.getHeaderFields());
0N/A }
0N/A }
0N/A
0N/A static class MyCacheResponse extends CacheResponse {
0N/A FileInputStream fis;
0N/A Map<String,List<String>> headers;
0N/A public MyCacheResponse(String filename) {
0N/A try {
0N/A fis = new FileInputStream(new File(filename));
2612N/A streams.add(fis);
0N/A ObjectInputStream ois = new ObjectInputStream(fis);
0N/A headers = (Map<String,List<String>>)ois.readObject();
0N/A } catch (Exception ex) {
0N/A // throw new RuntimeException(ex.getMessage());
0N/A }
0N/A }
0N/A
0N/A public InputStream getBody() throws IOException {
0N/A return fis;
0N/A }
0N/A
0N/A public Map<String,List<String>> getHeaders() throws IOException {
0N/A return headers;
0N/A }
0N/A }
0N/A
0N/A static class MyCacheRequest extends CacheRequest {
0N/A FileOutputStream fos;
0N/A public MyCacheRequest(String filename, Map<String,List<String>> rspHeaders) {
0N/A try {
0N/A File file = new File(filename);
0N/A fos = new FileOutputStream(file);
2612N/A streams.add(fos);
2612N/A files.add(file);
0N/A ObjectOutputStream oos = new ObjectOutputStream(fos);
0N/A oos.writeObject(rspHeaders);
0N/A } catch (Exception ex) {
0N/A throw new RuntimeException(ex.getMessage());
0N/A }
0N/A }
0N/A public OutputStream getBody() throws IOException {
0N/A return fos;
0N/A }
0N/A
0N/A public void abort() {
0N/A // no op
0N/A }
0N/A }
0N/A
0N/A
0N/A}