Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/RetryPost.java
38867 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.*;24import java.util.*;25import java.io.*;26import com.sun.net.httpserver.*;27import java.util.concurrent.Executors;28import java.util.concurrent.ExecutorService;2930public class RetryPost31{32static boolean shouldRetry = true;3334com.sun.net.httpserver.HttpServer httpServer;35MyHandler httpHandler;36ExecutorService executorService;3738public static void main(String[] args) {39if (args.length == 1 && args[0].equals("noRetry"))40shouldRetry = false;4142new RetryPost();43}4445public RetryPost() {46try {47startHttpServer(shouldRetry);48doClient();49} catch (IOException ioe) {50System.err.println(ioe);51}52}5354void doClient() {55try {56InetSocketAddress address = httpServer.getAddress();57URL url = new URL("http://" + address.getHostName() + ":" + address.getPort() + "/test/");58HttpURLConnection uc = (HttpURLConnection)url.openConnection();59uc.setDoOutput(true);60uc.setRequestMethod("POST");61uc.getResponseCode();6263// if we reach here then we have failed64throw new RuntimeException("Failed: POST request being retried");6566} catch (SocketException se) {67// this is what we expect to happen and is OK.68if (shouldRetry && httpHandler.getCallCount() != 2)69throw new RuntimeException("Failed: Handler should have been called twice. " +70"It was called "+ httpHandler.getCallCount() + " times");71else if (!shouldRetry && httpHandler.getCallCount() != 1)72throw new RuntimeException("Failed: Handler should have only been called once" +73"It was called "+ httpHandler.getCallCount() + " times");74} catch (IOException e) {75e.printStackTrace();76} finally {77httpServer.stop(1);78executorService.shutdown();79}80}8182/**83* Http Server84*/85public void startHttpServer(boolean shouldRetry) throws IOException {86httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);87httpHandler = new MyHandler(shouldRetry);8889HttpContext ctx = httpServer.createContext("/test/", httpHandler);9091executorService = Executors.newCachedThreadPool();92httpServer.setExecutor(executorService);93httpServer.start();94}9596class MyHandler implements HttpHandler {97int callCount = 0;98boolean shouldRetry;99100public MyHandler(boolean shouldRetry) {101this.shouldRetry = shouldRetry;102}103104public void handle(HttpExchange t) throws IOException {105callCount++;106107if (callCount > 1 && !shouldRetry) {108// if this bug has been fixed then this method will not be called twice109// when -Dhttp.retryPost=false110t.sendResponseHeaders(400, -1); // indicate failure by returning 400111} else {112// simply close out the stream without sending any data.113OutputStream os = t.getResponseBody();114os.close();115}116}117118public int getCallCount() {119return callCount;120}121}122123}124125126