Path: blob/master/src/java.base/share/classes/java/net/ContentHandler.java
41152 views
/*1* Copyright (c) 1995, 2020, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;2829/**30* The abstract class {@code ContentHandler} is the superclass31* of all classes that read an {@code Object} from a32* {@code URLConnection}.33* <p>34* An application does not generally call the35* {@code getContent} method in this class directly. Instead, an36* application calls the {@code getContent} method in class37* {@code URL} or in {@code URLConnection}.38* The application's content handler factory (an instance of a class that39* implements the interface {@code ContentHandlerFactory} set up by a call to40* {@link URLConnection#setContentHandlerFactory(ContentHandlerFactory)41* setContentHandlerFactory} is called with a {@code String} giving the42* MIME type of the object being received on the socket. The factory returns an43* instance of a subclass of {@code ContentHandler}, and its44* {@code getContent} method is called to create the object.45* <p>46* If no content handler could be {@linkplain URLConnection#getContent() found},47* URLConnection will look for a content handler in a user-definable set of places.48* Users can define a vertical-bar delimited set of class prefixes49* to search through by defining the <i>{@value java.net.URLConnection#contentPathProp}</i>50* property. The class name must be of the form:51* <blockquote>52* <i>{package-prefix}.{major}.{minor}</i>53* <p>54* where <i>{major}.{minor}</i> is formed by taking the55* content-type string, replacing all slash characters with a56* {@code period} ('.'), and all other non-alphanumeric characters57* with the underscore character '{@code _}'. The alphanumeric58* characters are specifically the 26 uppercase ASCII letters59* '{@code A}' through '{@code Z}', the 26 lowercase ASCII60* letters '{@code a}' through '{@code z}', and the 10 ASCII61* digits '{@code 0}' through '{@code 9}'.62* <p>63* e.g.64* YoyoDyne.experimental.text.plain65* </blockquote>66* If no user-defined content handler is found, then the system67* tries to load a specific <i>content-type</i> handler from one68* of the built-in handlers, if one exists.69* <p>70* If the loading of the content handler class would be performed by71* a classloader that is outside of the delegation chain of the caller,72* the JVM will need the RuntimePermission "getClassLoader".73*74* @author James Gosling75* @see java.net.ContentHandler#getContent(java.net.URLConnection)76* @see java.net.ContentHandlerFactory77* @see java.net.URL#getContent()78* @see java.net.URLConnection79* @see java.net.URLConnection#getContent()80* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)81* @since 1.082*/83public abstract class ContentHandler {8485/**86* Constructor for subclasses to call.87*/88public ContentHandler() {}8990/**91* Given a URL connect stream positioned at the beginning of the92* representation of an object, this method reads that stream and93* creates an object from it.94*95* @param urlc a URL connection.96* @return the object read by the {@code ContentHandler}.97* @throws IOException if an I/O error occurs while reading the object.98*/99public abstract Object getContent(URLConnection urlc) throws IOException;100101/**102* Given a URL connect stream positioned at the beginning of the103* representation of an object, this method reads that stream and104* creates an object that matches one of the types specified.105*106* The default implementation of this method should call107* {@link #getContent(URLConnection)}108* and screen the return type for a match of the suggested types.109*110* @param urlc a URL connection.111* @param classes an array of types requested112* @return the object read by the {@code ContentHandler} that is113* the first match of the suggested types or114* {@code null} if none of the requested are supported.115* @throws IOException if an I/O error occurs while reading the object.116* @since 1.3117*/118@SuppressWarnings("rawtypes")119public Object getContent(URLConnection urlc, Class[] classes) throws IOException {120Object obj = getContent(urlc);121122for (Class<?> c : classes) {123if (c.isInstance(obj)) {124return obj;125}126}127return null;128}129}130131132