Path: blob/master/test/jdk/java/net/URLConnection/contentHandler/UserContentHandler.java
41153 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 419114725* @summary 1.2beta4 does not load user defined content handlers26* @library /test/lib27* @build UserContentHandler28* @run main/othervm UserContentHandler29*/3031/* Run in othervm mode since the test sets a system property, java.content.handler.pkgs,32* that prepends a specific package prefix defining a text/plain content33* handler. If other URLConnection tests run before this one they might trigger34* the Sun implementation text/plain content handler in sun.net.www.content35* to be loaded and cached, this will break this test.36*/3738import java.net.*;39import java.io.*;40import java.util.*;41import jdk.test.lib.net.URIBuilder;42import static java.net.Proxy.NO_PROXY;4344public class UserContentHandler implements Runnable {4546ServerSocket ss;4748public void run() {49try {5051Socket s = ss.accept();52s.setTcpNoDelay(true);5354PrintStream out = new PrintStream(55new BufferedOutputStream(56s.getOutputStream() ));5758out.print("HTTP/1.1 200 OK\r\n");59out.print("Content-Length: 11\r\n");60out.print("Content-Type: text/plain\r\n");61out.print("\r\n");62out.print("l;ajfdjafd\n");63out.flush();6465// don't close the connection immediately as otherwise66// the http headers may not have been received and the67// http client will re-connect.68Thread.sleep(2000);6970s.close();7172} catch (Exception e) {73e.printStackTrace();74}75}7677UserContentHandler() throws Exception {7879InetAddress loopback = InetAddress.getLoopbackAddress();80ss = new ServerSocket();81ss.bind(new InetSocketAddress(loopback, 0));82Thread thr = new Thread(this);83thr.start();8485try {86Object o = new COM.foo.content.text.plain();87} catch (Exception ex) {88ex.printStackTrace();89}90Properties props = System.getProperties();91props.put("java.content.handler.pkgs", "COM.foo.content");92System.setProperties(props);9394URL u = URIBuilder.newBuilder()95.scheme("http")96.loopback()97.port(ss.getLocalPort())98.path("/anything.txt")99.toURL();100101if (!(u.openConnection(NO_PROXY).getContent() instanceof String)) {102throw new RuntimeException("Load user defined content handler failed.");103} else {104System.err.println("Load user defined content handler succeed!");105}106}107108public static void main(String args[]) throws Exception {109new UserContentHandler();110}111}112113114