B6373555.java revision 6073:cea72c2bf071
1/*
2 * Copyright (c) 2006, 2011, 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 6373555
27 * @summary HTTP Server failing to answer client requests
28 */
29
30import java.net.*;
31import java.io.*;
32import javax.xml.soap.*;
33import java.util.*;
34import com.sun.net.httpserver.*;
35import java.util.concurrent.*;
36
37public class B6373555 {
38
39    private static int s_received = 0;
40    private static int sent = 0;
41
42    private static int received = 0;
43    private static int port;
44
45    private static volatile boolean error = false;
46    static HttpServer httpServer;
47    static ExecutorService pool, execs;
48    static int NUM = 1000;
49
50    public static void main(String[] args) throws Exception {
51        try {
52            if (args.length > 0) {
53                NUM = Integer.parseInt (args[0]);
54            }
55            execs = Executors.newFixedThreadPool(5);
56            httpServer = createHttpServer(execs);
57            port = httpServer.getAddress().getPort();
58            pool = Executors.newFixedThreadPool(10);
59            httpServer.start();
60            for (int i=0; i < NUM; i++) {
61                pool.execute(new Client());
62                if (error) {
63                    throw new Exception ("error in test");
64                }
65            }
66            System.out.println("Main thread waiting");
67            pool.shutdown();
68            long latest = System.currentTimeMillis() + 200 * 1000;
69            while (System.currentTimeMillis() < latest) {
70                if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {
71                    System.out.println("Main thread done!");
72                    return;
73                }
74                if (error) {
75                    throw new Exception ("error in test");
76                }
77            }
78            throw new Exception ("error in test: timed out");
79        } finally {
80            httpServer.stop(0);
81            pool.shutdownNow();
82            execs.shutdownNow();
83        }
84    }
85
86    public static class Client implements Runnable {
87
88        byte[] getBuf () {
89            byte[] buf = new byte [5200];
90            for (int i=0; i< 5200; i++) {
91                buf [i] = (byte)i;
92            }
93            return buf;
94        }
95
96        public void run() {
97            try {
98                Thread.sleep(10);
99                byte[] buf = getBuf();
100                URL url = new URL("http://127.0.0.1:"+port+"/test");
101                HttpURLConnection con = (HttpURLConnection)url.openConnection();
102                con.setDoOutput(true);
103                con.setDoInput(true);
104                con.setRequestMethod("POST");
105                con.setRequestProperty(
106                    "Content-Type",
107                    "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");
108                OutputStream out = con.getOutputStream();
109                out.write(buf);
110                out.close();
111                InputStream in = con.getInputStream();
112                byte[] newBuf = readFully(in);
113                in.close();
114                if (buf.length != newBuf.length) {
115                    System.out.println("Doesn't match");
116                    error = true;
117                }
118            }
119            catch(Exception e) {
120                e.printStackTrace();
121                System.out.print (".");
122                error = true;
123            }
124        }
125    }
126
127    private static byte[] readFully(InputStream istream) throws IOException {
128        ByteArrayOutputStream bout = new ByteArrayOutputStream();
129        byte[] buf = new byte[1024];
130        int num = 0;
131
132        if (istream != null) {
133            while ((num = istream.read(buf)) != -1) {
134                bout.write(buf, 0, num);
135            }
136        }
137        byte[] ret = bout.toByteArray();
138        return ret;
139    }
140
141
142    private static HttpServer createHttpServer(ExecutorService execs)
143        throws Exception {
144        InetSocketAddress inetAddress = new InetSocketAddress(0);
145        HttpServer testServer = HttpServer.create(inetAddress, 15);
146        testServer.setExecutor(execs);
147        HttpContext context = testServer.createContext("/test");
148        context.setHandler(new HttpHandler() {
149            public void handle(HttpExchange msg) {
150                try {
151                    String method = msg.getRequestMethod();
152                        if (method.equals("POST")) {
153                        InputStream is = msg.getRequestBody();
154                            byte[] buf = readFully(is);
155                            is.close();
156                            writePostReply(msg, buf);
157                    } else {
158                        System.out.println("****** METHOD not handled ***** "+method);
159                            System.out.println("Received="+s_received);
160                    }
161                }
162                catch(Exception e) {
163                    e.printStackTrace();
164                }
165                finally {
166                    msg.close();
167                }
168            }
169        }
170        );
171        return testServer;
172    }
173
174    private static void writePostReply(HttpExchange msg, byte[] buf)
175        throws Exception {
176        msg.getResponseHeaders().add("Content-Type", "text/xml");
177        msg.sendResponseHeaders(200, buf.length);
178        OutputStream out = msg.getResponseBody();
179        out.write(buf);
180        out.close();
181    }
182
183}
184