1 /* 2 * Copyright (c) 1998, 2001, 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 24 /* 25 * @test 26 * @bug 4092605 27 * @summary Test HttpURLConnection setIfModifiedSince 28 * 29 */ 30 31 import java.net.*; 32 import java.io.*; 33 34 public class Modified implements Runnable { 35 36 ServerSocket ss; 37 run()38 public void run() { 39 try { 40 Socket s = ss.accept(); 41 boolean gotIfModified = false; 42 43 BufferedReader in = new BufferedReader( 44 new InputStreamReader(s.getInputStream()) ); 45 46 String str = null; 47 do { 48 str = in.readLine(); 49 if (str.startsWith("If-Modified-Since")) { 50 gotIfModified = true; 51 } 52 if (str.equals("")) { 53 break; 54 } 55 } while (str != null); 56 57 PrintStream out = new PrintStream( 58 new BufferedOutputStream( 59 s.getOutputStream() )); 60 61 if (gotIfModified) { 62 out.print("HTTP/1.1 304 Not Modified\r\n"); 63 } else { 64 out.print("HTTP/1.1 200 OK\r\n"); 65 } 66 67 out.print("Content-Type: text/html\r\n"); 68 out.print("Connection: close\r\n"); 69 out.print("\r\n"); 70 out.flush(); 71 72 s.close(); 73 74 } catch (Exception e) { 75 e.printStackTrace(); 76 } 77 } 78 Modified()79 Modified() throws Exception { 80 81 ss = new ServerSocket(0); 82 Thread thr = new Thread(this); 83 thr.start(); 84 85 URL testURL = new URL("http://localhost:" + ss.getLocalPort() + 86 "/index.html"); 87 URLConnection URLConn = testURL.openConnection(); 88 HttpURLConnection httpConn; 89 90 if (URLConn instanceof HttpURLConnection) { 91 httpConn = (HttpURLConnection)URLConn; 92 httpConn.setAllowUserInteraction(false); 93 httpConn.setIfModifiedSince(9990000000000L); 94 int response = httpConn.getResponseCode(); 95 if (response != 304) 96 throw new RuntimeException("setModifiedSince failure."); 97 } 98 } 99 main(String args[])100 public static void main(String args[]) throws Exception { 101 new Modified(); 102 } 103 } 104