test/jdk/java/net/URLConnection/HandleContentTypeWithAttrs.java
changeset 57686 70f5cbb711a9
parent 47216 71c04702a3d5
equal deleted inserted replaced
57685:e4cc5231ce2d 57686:70f5cbb711a9
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4160200
    26  * @bug 4160200
    27  * @summary Make sure URLConnection.getContnentHandler
    27  * @summary Make sure URLConnection.getContentHandler
    28  *     can handle MIME types with attributes
    28  *     can handle MIME types with attributes
       
    29  * @library /test/lib
    29  * @modules java.base/sun.net.www java.base/sun.net.www.content.text
    30  * @modules java.base/sun.net.www java.base/sun.net.www.content.text
    30  */
    31  */
    31 import java.net.*;
    32 import java.net.*;
    32 import java.io.*;
    33 import java.io.*;
    33 import sun.net.www.content.text.*;
    34 import sun.net.www.content.text.*;
    34 import sun.net.www.MessageHeader;
    35 import sun.net.www.MessageHeader;
    35 import static java.net.Proxy.NO_PROXY;
    36 import static java.net.Proxy.NO_PROXY;
    36 
    37 
       
    38 import jdk.test.lib.net.URIBuilder;
       
    39 
    37 public class HandleContentTypeWithAttrs {
    40 public class HandleContentTypeWithAttrs {
    38 
    41 
    39     URL url;
    42     URL url;
    40 
    43 
    41     public HandleContentTypeWithAttrs (int port) throws Exception {
    44     public HandleContentTypeWithAttrs (int port) throws Exception {
    42 
    45 
    43         // Request echo.html from myHttpServer.
    46         // Request echo.html from myHttpServer.
    44         // In the header of the response, we make
    47         // In the header of the response, we make
    45         // the content type have some attributes.
    48         // the content type have some attributes.
    46         url = new URL("http://localhost:" + port + "/echo.html");
    49         url = URIBuilder.newBuilder()
       
    50                 .scheme("http")
       
    51                 .loopback()
       
    52                 .port(port)
       
    53                 .path("/echo.html")
       
    54                 .toURL();
    47         URLConnection urlConn = url.openConnection(NO_PROXY);
    55         URLConnection urlConn = url.openConnection(NO_PROXY);
    48 
    56 
    49         // the method getContent() calls the method
    57         // the method getContent() calls the method
    50         // getContentHandler(). With the fix, the method
    58         // getContentHandler(). With the fix, the method
    51         // getContentHandler() gets the correct content
    59         // getContentHandler() gets the correct content
   133     }
   141     }
   134 
   142 
   135     /** Start a server on port <i>port</i>.  It will call serviceRequest()
   143     /** Start a server on port <i>port</i>.  It will call serviceRequest()
   136         for each new connection. */
   144         for each new connection. */
   137     final public void startServer(int port) throws IOException {
   145     final public void startServer(int port) throws IOException {
   138         serverSocket = new ServerSocket(port, 50);
   146         serverSocket = new ServerSocket(port, 50,
       
   147                 InetAddress.getLoopbackAddress());
   139         serverInstance = new Thread(this);
   148         serverInstance = new Thread(this);
   140         serverInstance.start();
   149         serverInstance.start();
   141     }
   150     }
   142 
   151 
   143     final public int getServerLocalPort() throws Exception {
   152     final public int getServerLocalPort() throws Exception {
   217         }
   226         }
   218     }
   227     }
   219 
   228 
   220     public myHttpServer () {
   229     public myHttpServer () {
   221         try {
   230         try {
   222             defaultContext
   231             defaultContext = URIBuilder.newBuilder()
   223             = new URL("http", InetAddress.getLocalHost().getHostName(), "/");
   232                     .scheme("http")
       
   233                     .loopback()
       
   234                     .path("/")
       
   235                     .toURL();
   224         } catch(Exception e) {
   236         } catch(Exception e) {
   225             System.out.println("Failed to construct defauit URL context: "
   237             System.out.println("Failed to construct default URL context: "
   226                                + e);
   238                                + e);
   227             e.printStackTrace();
   239             e.printStackTrace();
   228         }
   240         }
   229     }
   241     }
   230 
   242