jdk/test/java/net/ResponseCache/file2.1
changeset 2 90ce3da70b43
child 80 5ea387b814e6
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /* @test
       
     2  * @summary Unit test for java.net.ResponseCacheHandler
       
     3  * @bug 4837267
       
     4  * @author Yingxian Wang
       
     5  */
       
     6 
       
     7 import java.net.*;
       
     8 import java.util.*;
       
     9 import java.io.*;
       
    10 import java.nio.*;
       
    11 import java.nio.channels.ReadableByteChannel;
       
    12 import sun.net.www.ParseUtil;
       
    13 
       
    14 /**
       
    15  * Request should get serviced by the cache handler. Response get
       
    16  * saved through the cache handler.
       
    17  */
       
    18 public class ResponseCacheHandlerTest implements Runnable {
       
    19     ServerSocket ss;
       
    20     static URL url1;
       
    21     static URL url2;
       
    22     static boolean flag = false;
       
    23     /* 
       
    24      * Our "http" server to return a 404 */
       
    25     public void run() {
       
    26 	try {
       
    27 	    Socket s = ss.accept();
       
    28 
       
    29 	    // check request contains "Cookie"
       
    30 	    InputStream is = s.getInputStream ();
       
    31 	    BufferedReader r = new BufferedReader(new InputStreamReader(is));
       
    32 	    boolean flag = false;
       
    33 	    String x;
       
    34 	    while ((x=r.readLine()) != null) {
       
    35 		if (x.length() ==0) {
       
    36 		    break;
       
    37 		}
       
    38 	    }
       
    39 	    PrintStream out = new PrintStream( 
       
    40 				 new BufferedOutputStream(
       
    41 				    s.getOutputStream() ));
       
    42 	    
       
    43 	    /* send file2.1 */
       
    44 	    File file2 = new File("file2.1");
       
    45 	    out.print("HTTP/1.1 200 OK\r\n");
       
    46 	    out.print("Content-Type: text/html; charset=iso-8859-1\r\n");
       
    47 	    out.print("Content-Length: "+file2.length()+"\r\n");
       
    48 	    out.print("Connection: close\r\n");
       
    49 	    out.print("\r\n");
       
    50 	    FileInputStream fis = new FileInputStream(file2);
       
    51 	    byte[] buf = new byte[(int)file2.length()];
       
    52 	    int len;
       
    53 	    while ((len = fis.read(buf)) != -1) {
       
    54 		out.print(new String(buf));
       
    55 	    }
       
    56 
       
    57 	    out.flush();
       
    58 
       
    59 	    s.close();
       
    60 	    ss.close();
       
    61 	} catch (Exception e) { 
       
    62 	    e.printStackTrace();
       
    63 	}
       
    64     }
       
    65 
       
    66     ResponseCacheHandlerTest() throws Exception {
       
    67 	/* start the server */
       
    68 	ss = new ServerSocket(0);
       
    69 	(new Thread(this)).start();
       
    70 
       
    71 	/* establish http connection to server */
       
    72 	url1 = new URL("http://localhost/file1.cache");
       
    73 	HttpURLConnection http = (HttpURLConnection)url1.openConnection();
       
    74 	InputStream is = null;
       
    75 	System.out.println("headers1:"+http.getHeaderFields());
       
    76 	try {
       
    77 	    is = http.getInputStream();
       
    78 	} catch (IOException ioex) {
       
    79 	    throw new RuntimeException(ioex.getMessage());
       
    80 	}
       
    81 	BufferedReader r = new BufferedReader(new InputStreamReader(is));
       
    82 	String x;
       
    83 	File fileout = new File("file1");
       
    84 	PrintStream out = new PrintStream( 
       
    85 				 new BufferedOutputStream(
       
    86 				    new FileOutputStream(fileout)));
       
    87 	while ((x=r.readLine()) != null) {
       
    88 	    out.print(x+"\n");
       
    89 	}
       
    90 	out.flush();
       
    91 	out.close();
       
    92 
       
    93 	// if !(file1.out == file1.cache)
       
    94 	// throw new RuntimeException("testing ResponseCacheHandler.get() failed");
       
    95 	http.disconnect();
       
    96 
       
    97 	// testing ResponseCacheHandler.put()
       
    98 	url2 = new URL("http://localhost:" + 
       
    99 		       Integer.toString(ss.getLocalPort())+"/file2.1");
       
   100 	http = (HttpURLConnection)url2.openConnection();
       
   101 	System.out.println("headers2:"+http.getHeaderFields());
       
   102 
       
   103 	try {
       
   104 	    is = http.getInputStream();
       
   105 	} catch (IOException ioex) {
       
   106 	    throw new RuntimeException(ioex.getMessage());
       
   107 	}
       
   108 	DataInputStream dis = new DataInputStream(is);
       
   109 	fileout = new File("file2.2");
       
   110 	byte[] buf = new byte[(int)new File("file2.1").length()];
       
   111 	DataOutputStream dos =  new DataOutputStream(new FileOutputStream(fileout));
       
   112 	int len;
       
   113 	while ((len=dis.read(buf)) != -1) {
       
   114 	    dos.write(buf, 0, len);
       
   115 	}
       
   116 	dos.flush();
       
   117 	dos.close();
       
   118 
       
   119 	// if !(file2.1 == file2.2 == file2.cache)
       
   120 	// throw new RuntimeException("testing ResponseCacheHandler.put() failed");
       
   121     }
       
   122     public static void main(String args[]) throws Exception {
       
   123 	ResponseCacheHandler.setDefault(new MyResponseCacheHandler());
       
   124 
       
   125 	new ResponseCacheHandlerTest();
       
   126     }
       
   127 
       
   128     static class MyResponseCacheHandler extends ResponseCacheHandler {
       
   129 	public Response 
       
   130 	get(URI uri, Map requestHeaders)
       
   131 	    throws IOException {
       
   132 	    if (uri.equals(ParseUtil.toURI(url1))) {
       
   133 		return new MyResponse("file1.cache");
       
   134 	    } if (uri.equals(ParseUtil.toURI(url2)) && !flag) {
       
   135 		flag = true;
       
   136 		return null;
       
   137 	    } else {
       
   138 		return new MyResponse("file2.cache");
       
   139 	    }
       
   140 	}
       
   141 	public boolean put(URI uri, Response response)  throws IOException {
       
   142 	    // save cache to file2.cache
       
   143 	    // 1. serialize headers into file2.cache
       
   144 	    // 2. write data to file2.cache
       
   145 	    File file = new File("file2.cache");
       
   146 	    FileOutputStream fos = new FileOutputStream(file);
       
   147 	    ObjectOutputStream oos = new ObjectOutputStream(fos);
       
   148 	    oos.writeObject(response.getHeaders());
       
   149 
       
   150 	    ReadableByteChannel rbc = response.getBody();
       
   151 	    int len = (int)new File("file2.1").length();
       
   152 	    ByteBuffer buf = ByteBuffer.allocate(len);
       
   153 	    byte[] b = new byte[len];
       
   154 	    while ((len = rbc.read(buf)) != -1) {
       
   155 		buf.flip();
       
   156 		buf.get(b, 0, len);
       
   157 		buf.compact();
       
   158 		oos.write(b, 0, len);
       
   159 	    }
       
   160 	    oos.close();
       
   161 	    return true;
       
   162 	}
       
   163     }
       
   164 
       
   165     static class MyResponse extends Response {
       
   166 	FileInputStream fis;
       
   167 	Map headers;
       
   168 	public MyResponse(String filename) {
       
   169 	    try {
       
   170 		fis = new FileInputStream(new File(filename));
       
   171 		headers = (Map)new ObjectInputStream(fis).readObject();
       
   172 	    } catch (Exception ex) {
       
   173 		throw new RuntimeException(ex.getMessage());
       
   174 	    }
       
   175 	}
       
   176 	public ReadableByteChannel getBody() throws IOException {
       
   177 	    return fis.getChannel();
       
   178 	}
       
   179 
       
   180 	public Map getHeaders() throws IOException {
       
   181 	    return headers;
       
   182 	}
       
   183     }
       
   184 }