alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (RedirectOnPost.java)

This example Java source code file (RedirectOnPost.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

bytearrayoutputstream, exception, executorservice, handler, hello, httpcontext, httpserver, httpsserver, httpsurlconnection, inetsocketaddress, inputstream, ioexception, net, network, ssl, sslcontext, string, threading, threads, util

The RedirectOnPost.java Java example source code

/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @compile ../../../../../com/sun/net/httpserver/SimpleSSLContext.java RedirectOnPost.java
 * @run main/othervm RedirectOnPost
 * @bug 8029127
 * @summary A redirect POST request does not work and illegalStateException on HttpURLConnection.getInputStream
 */

import java.net.*;
import java.io.*;
import java.util.*;
import com.sun.net.httpserver.*;
import java.util.concurrent.*;
import javax.net.ssl.*;

public class RedirectOnPost {


    public static void main(String[] args) throws Exception {
            ExecutorService e= Executors.newFixedThreadPool(5);
        String keysdir = System.getProperty("test.src")
                  + "/../../../../../com/sun/net/httpserver/";
        SSLContext ctx = new SimpleSSLContext(keysdir).get();
            HttpServer httpServer = getHttpServer(e);
            HttpsServer httpsServer = getHttpsServer(e, ctx);

        try {
            // take the keystore from elsewhere in test hierarchy
            int port = httpServer.getAddress().getPort();
            int sslPort = httpsServer.getAddress().getPort();
            httpServer.start();
            httpsServer.start();
            runTest("http://127.0.0.1:"+port+"/test/", null);
            runTest("https://127.0.0.1:"+sslPort+"/test/", ctx);
            System.out.println("Main thread waiting");
        } finally {
            httpServer.stop(0);
            httpsServer.stop(0);
            e.shutdownNow();
        }
    }

    public static void runTest(String baseURL, SSLContext ctx) throws Exception
    {
        byte[] buf = "Hello world".getBytes();
        URL url = new URL(baseURL + "a");
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        if (con instanceof HttpsURLConnection) {
            HttpsURLConnection ssl = (HttpsURLConnection)con;
            ssl.setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String host, SSLSession sess) {
                    return true;
                }
            });
            ssl.setSSLSocketFactory (ctx.getSocketFactory());
        }
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        try (OutputStream out = con.getOutputStream()) {
            out.write(buf);
        }
        try (InputStream in = con.getInputStream()) {
            byte[] newBuf = readFully(in);
        }
    }

    private static byte[] readFully(InputStream istream) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int num = 0;

        if (istream != null) {
            while ((num = istream.read(buf)) != -1) {
                bout.write(buf, 0, num);
            }
        }
        byte[] ret = bout.toByteArray();
        return ret;
    }


    static class Handler implements HttpHandler {

        String baseURL;

        Handler(String baseURL) {
            this.baseURL = baseURL;
        }

        int calls = 0;

        public void handle(HttpExchange msg) {
            try {
                String method = msg.getRequestMethod();
                System.out.println ("Server: " + baseURL);
                if (calls++ == 0) {
                    System.out.println ("Server: redirecting");
                    InputStream is = msg.getRequestBody();
                    byte[] buf = readFully(is);
                    is.close();
                    Headers h = msg.getResponseHeaders();
                    h.add("Location", baseURL + "b");
                    msg.sendResponseHeaders(302, -1);
                    msg.close();
                } else {
                    System.out.println ("Server: second call");
                    InputStream is = msg.getRequestBody();
                    byte[] buf = readFully(is);
                    is.close();
                    msg.sendResponseHeaders(200, -1);
                    msg.close();
                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            finally {
                msg.close();
            }
        }
    }

    private static HttpServer getHttpServer(ExecutorService execs)
        throws Exception
    {
        InetSocketAddress inetAddress = new InetSocketAddress(0);
        HttpServer testServer = HttpServer.create(inetAddress, 15);
        int port = testServer.getAddress().getPort();
        testServer.setExecutor(execs);
        String base = "http://127.0.0.1:"+port+"/test";
        HttpContext context = testServer.createContext("/test");
        context.setHandler(new Handler(base));
        return testServer;
    }

    private static HttpsServer getHttpsServer(
        ExecutorService execs, SSLContext ctx
    )
        throws Exception
    {
        InetSocketAddress inetAddress = new InetSocketAddress(0);
        HttpsServer testServer = HttpsServer.create(inetAddress, 15);
        int port = testServer.getAddress().getPort();
        testServer.setExecutor(execs);
        testServer.setHttpsConfigurator(new HttpsConfigurator (ctx));
        String base = "https://127.0.0.1:"+port+"/test";
        HttpContext context = testServer.createContext("/test");
        context.setHandler(new Handler(base));
        return testServer;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java RedirectOnPost.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.