Home | History | Annotate | Download | only in net
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.net;
     19 
     20 import java.io.IOException;
     21 
     22 /**
     23  * This class converts the content of a certain format (i.e. a MIME type) into a
     24  * Java type object. It is created by {@code ContentHandlerFactory}. The data
     25  * values should be accessed via {@code URL} or {@code URLConnection}.
     26  *
     27  * @see ContentHandlerFactory
     28  * @see URL#getContent()
     29  * @see URLConnection#getContent()
     30  */
     31 public abstract class ContentHandler {
     32     /**
     33      * Returns the object pointed by the specified URL connection {@code uConn}.
     34      *
     35      * @param uConn
     36      *            URL connection that points to the desired object.
     37      * @return object referred by {@code uConn}.
     38      * @throws IOException
     39      *             if an IO error occurs during the retrieval of the object
     40      */
     41     public abstract Object getContent(URLConnection uConn) throws IOException;
     42 
     43     /**
     44      * Returns the object pointed by the specified URL connection {@code uConn}.
     45      *
     46      * @param uConn
     47      *            URL connection that points to the desired object.
     48      * @param types
     49      *            list of acceptable content types.
     50      * @return resource object pointed by this URL or {@code null} if the
     51      *         content doesn't match one of the specified content types.
     52      * @throws IOException
     53      *             if an error occurred while obtaining the content.
     54      */
     55     // Class arg not generified in the spec.
     56     @SuppressWarnings("unchecked")
     57     public Object getContent(URLConnection uConn, Class[] types)
     58             throws IOException {
     59         Object content = getContent(uConn);
     60         for (int i = 0; i < types.length; i++) {
     61             if (types[i].isInstance(content)) {
     62                 return content;
     63             }
     64         }
     65         return null;
     66     }
     67 }
     68