RetryPost.java revision 9218:0c3ce4f14c1d
1/* 2 * Copyright (c) 2006, 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.util.*; 26import java.io.*; 27import com.sun.net.httpserver.*; 28import java.util.concurrent.Executors; 29import java.util.concurrent.ExecutorService; 30 31public class RetryPost 32{ 33 static boolean shouldRetry = true; 34 35 com.sun.net.httpserver.HttpServer httpServer; 36 MyHandler httpHandler; 37 ExecutorService executorService; 38 39 public static void main(String[] args) { 40 if (args.length == 1 && args[0].equals("noRetry")) 41 shouldRetry = false; 42 43 new RetryPost(); 44 } 45 46 public RetryPost() { 47 try { 48 startHttpServer(shouldRetry); 49 doClient(); 50 } catch (IOException ioe) { 51 System.err.println(ioe); 52 } 53 } 54 55 void doClient() { 56 try { 57 InetSocketAddress address = httpServer.getAddress(); 58 URL url = new URL("http://localhost:" + address.getPort() + "/test/"); 59 HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); 60 uc.setDoOutput(true); 61 uc.setRequestMethod("POST"); 62 uc.getResponseCode(); 63 64 // if we reach here then we have failed 65 throw new RuntimeException("Failed: POST request being retried"); 66 67 } catch (SocketException se) { 68 // this is what we expect to happen and is OK. 69 if (shouldRetry && httpHandler.getCallCount() != 2) 70 throw new RuntimeException("Failed: Handler should have been called twice. " + 71 "It was called "+ httpHandler.getCallCount() + " times"); 72 else if (!shouldRetry && httpHandler.getCallCount() != 1) 73 throw new RuntimeException("Failed: Handler should have only been called once" + 74 "It was called "+ httpHandler.getCallCount() + " times"); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } finally { 78 httpServer.stop(1); 79 executorService.shutdown(); 80 } 81 } 82 83 /** 84 * Http Server 85 */ 86 public void startHttpServer(boolean shouldRetry) throws IOException { 87 httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0); 88 httpHandler = new MyHandler(shouldRetry); 89 90 HttpContext ctx = httpServer.createContext("/test/", httpHandler); 91 92 executorService = Executors.newCachedThreadPool(); 93 httpServer.setExecutor(executorService); 94 httpServer.start(); 95 } 96 97 class MyHandler implements HttpHandler { 98 int callCount = 0; 99 boolean shouldRetry; 100 101 public MyHandler(boolean shouldRetry) { 102 this.shouldRetry = shouldRetry; 103 } 104 105 public void handle(HttpExchange t) throws IOException { 106 callCount++; 107 108 if (callCount > 1 && !shouldRetry) { 109 // if this bug has been fixed then this method will not be called twice 110 // when -Dhttp.retryPost=false 111 t.sendResponseHeaders(400, -1); // indicate failure by returning 400 112 } else { 113 // simply close out the stream without sending any data. 114 OutputStream os = t.getResponseBody(); 115 os.close(); 116 } 117 } 118 119 public int getCallCount() { 120 return callCount; 121 } 122 } 123 124} 125