Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLConnection/contentHandler/UserContentHandler.java
38828 views
/*1* Copyright (c) 1999, 2010, 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* @build UserContentHandler27* @run main/othervm UserContentHandler28*/2930/* Run in othervm mode since the test sets a system property, java.content.handler.pkgs,31* that prepends a specific package prefix defining a text/plain content32* handler. If other URLConnection tests run before this one they might trigger33* the Sun implementation text/plain content handler in sun.net.www.content34* to be loaded and cached, this will break this test.35*/3637import java.net.*;38import java.io.*;39import java.util.*;4041public class UserContentHandler implements Runnable {4243ServerSocket ss;4445public void run() {46try {4748Socket s = ss.accept();49s.setTcpNoDelay(true);5051PrintStream out = new PrintStream(52new BufferedOutputStream(53s.getOutputStream() ));5455out.print("HTTP/1.1 200 OK\r\n");56out.print("Content-Length: 11\r\n");57out.print("Content-Type: text/plain\r\n");58out.print("\r\n");59out.print("l;ajfdjafd\n");60out.flush();6162// don't close the connection immediately as otherwise63// the http headers may not have been received and the64// http client will re-connect.65Thread.sleep(2000);6667s.close();6869} catch (Exception e) {70e.printStackTrace();71}72}7374UserContentHandler() throws Exception {7576ss = new ServerSocket(0);77Thread thr = new Thread(this);78thr.start();7980try {81Object o = new COM.foo.content.text.plain();82} catch (Exception ex) {83ex.printStackTrace();84}85Properties props = System.getProperties();86props.put("java.content.handler.pkgs", "COM.foo.content");87System.setProperties(props);8889URL u = new URL("http://localhost:" + ss.getLocalPort() +90"/anything.txt");91if (!(u.openConnection().getContent() instanceof String)) {92throw new RuntimeException("Load user defined content handler failed.");93} else {94System.err.println("Load user defined content handler succeed!");95}96}9798public static void main(String args[]) throws Exception {99new UserContentHandler();100}101}102103104