jdk/test/sun/net/www/http/HttpClient/RetryPost.java
author chegar
Fri, 31 Jan 2014 11:10:36 +0000
changeset 22625 50ee5cbd439b
parent 5506 202f599c92aa
child 41965 015dea372cb3
permissions -rw-r--r--
7150539: HttpURLConnection.getResponseMessage() doesn't throw IOException on server error (OS X) Reviewed-by: michaelm, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.concurrent.Executors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class RetryPost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    static boolean shouldRetry = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    com.sun.net.httpserver.HttpServer httpServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    MyHandler httpHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    ExecutorService executorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        if (args.length == 1 && args[0].equals("noRetry"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            shouldRetry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        new RetryPost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public RetryPost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            startHttpServer(shouldRetry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            doClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.err.println(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    void doClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            InetSocketAddress address = httpServer.getAddress();
22625
50ee5cbd439b 7150539: HttpURLConnection.getResponseMessage() doesn't throw IOException on server error (OS X)
chegar
parents: 5506
diff changeset
    58
            URL url = new URL("http://localhost:" + address.getPort() + "/test/");
50ee5cbd439b 7150539: HttpURLConnection.getResponseMessage() doesn't throw IOException on server error (OS X)
chegar
parents: 5506
diff changeset
    59
            HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            uc.setDoOutput(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            uc.setRequestMethod("POST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            uc.getResponseCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // if we reach here then we have failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new RuntimeException("Failed: POST request being retried");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            // this is what we expect to happen and is OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (shouldRetry && httpHandler.getCallCount() != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                throw new RuntimeException("Failed: Handler should have been called twice. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                           "It was called "+ httpHandler.getCallCount() + " times");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            else if (!shouldRetry && httpHandler.getCallCount() != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                throw new RuntimeException("Failed: Handler should have only been called once" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                           "It was called "+ httpHandler.getCallCount() + " times");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            httpServer.stop(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            executorService.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Http Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void startHttpServer(boolean shouldRetry) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        httpHandler = new MyHandler(shouldRetry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        HttpContext ctx = httpServer.createContext("/test/", httpHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        executorService = Executors.newCachedThreadPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        httpServer.setExecutor(executorService);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        httpServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    class MyHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int callCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean shouldRetry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public MyHandler(boolean shouldRetry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.shouldRetry = shouldRetry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public void handle(HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            callCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (callCount > 1 && !shouldRetry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                // if this bug has been fixed then this method will not be called twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // when -Dhttp.retryPost=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                t.sendResponseHeaders(400, -1);  // indicate failure by returning 400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                // simply close out the stream without sending any data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        public int getCallCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return callCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}