1 /*
   2  * Copyright (c) 2004, 2012, 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 5049976
  27  * @modules java.base/sun.net.www
  28  * @library ../../httptest/
  29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
  30  * @run main SetChunkedStreamingMode
  31  * @summary Unspecified NPE is thrown when streaming output mode is enabled
  32  */
  33 
  34 import java.io.*;
  35 import java.net.*;
  36 
  37 public class SetChunkedStreamingMode implements HttpCallback {
  38 
  39     void okReply (HttpTransaction req) throws IOException {
  40         req.setResponseEntityBody ("Hello .");
  41         req.sendResponse (200, "Ok");
  42             System.out.println ("Server: sent response");
  43         req.orderlyClose();
  44     }
  45 
  46     public void request (HttpTransaction req) {
  47         try {
  48             okReply (req);
  49         } catch (IOException e) {
  50             e.printStackTrace();
  51         }
  52     }
  53 
  54     static void read (InputStream is) throws IOException {
  55         int c;
  56         System.out.println ("reading");
  57         while ((c=is.read()) != -1) {
  58             System.out.write (c);
  59         }
  60         System.out.println ("");
  61         System.out.println ("finished reading");
  62     }
  63 
  64     static TestHttpServer server;
  65 
  66     public static void main (String[] args) throws Exception {
  67         try {
  68             server = new TestHttpServer (new SetChunkedStreamingMode(), 1, 10, 0);
  69             System.out.println ("Server: listening on port: " + server.getLocalPort());
  70             URL url = new URL ("http://127.0.0.1:"+server.getLocalPort()+"/");
  71             System.out.println ("Client: connecting to " + url);
  72             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
  73             urlc.setChunkedStreamingMode (0);
  74             urlc.setRequestMethod("POST");
  75             urlc.setDoOutput(true);
  76             InputStream is = urlc.getInputStream();
  77         } catch (Exception e) {
  78             if (server != null) {
  79                 server.terminate();
  80             }
  81             throw e;
  82         }
  83         server.terminate();
  84     }
  85 
  86     public static void except (String s) {
  87         server.terminate();
  88         throw new RuntimeException (s);
  89     }
  90 }