jdk/src/java.base/share/classes/sun/net/www/protocol/netdoc/Handler.java
changeset 39532 855ab97cde5a
parent 39531 c13b7a0588cd
parent 39184 3aa52182b3ad
child 39533 fe4d6551efca
equal deleted inserted replaced
39531:c13b7a0588cd 39532:855ab97cde5a
     1 /*
       
     2  * Copyright (c) 1996, 1998, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*-
       
    27  * netdoc urls point either into the local filesystem or externally
       
    28  * through an http url, with network documents being preferred.  Useful for
       
    29  * FAQs & other documents which are likely to be changing over time at the
       
    30  * central site, and where the user will want the most recent edition.
       
    31  *
       
    32  * @author Steven B. Byrne
       
    33  */
       
    34 
       
    35 package sun.net.www.protocol.netdoc;
       
    36 
       
    37 import java.net.URL;
       
    38 import java.net.URLConnection;
       
    39 import java.net.MalformedURLException;
       
    40 import java.net.URLStreamHandler;
       
    41 import java.io.InputStream;
       
    42 import java.io.IOException;
       
    43 import sun.security.action.GetPropertyAction;
       
    44 
       
    45 public class Handler extends URLStreamHandler {
       
    46     static URL base;
       
    47 
       
    48     /*
       
    49      * Attempt to find a load the given url using the default (network)
       
    50      * documentation location.  If that fails, use the local copy
       
    51      */
       
    52     public synchronized URLConnection openConnection(URL u)
       
    53         throws IOException
       
    54     {
       
    55         URLConnection uc = null;
       
    56         URL ru;
       
    57 
       
    58         boolean localonly = Boolean.parseBoolean(
       
    59                 GetPropertyAction.privilegedGetProperty("newdoc.localonly"));
       
    60 
       
    61         String docurl = GetPropertyAction.privilegedGetProperty("doc.url");
       
    62 
       
    63         String file = u.getFile();
       
    64         if (!localonly) {
       
    65             try {
       
    66                 if (base == null) {
       
    67                     base = new URL(docurl);
       
    68                 }
       
    69                 ru = new URL(base, file);
       
    70             } catch (MalformedURLException e) {
       
    71                 ru = null;
       
    72             }
       
    73             if (ru != null) {
       
    74                 uc = ru.openConnection();
       
    75             }
       
    76         }
       
    77 
       
    78         if (uc == null) {
       
    79             try {
       
    80                 ru = new URL("file", "~", file);
       
    81 
       
    82                 uc = ru.openConnection();
       
    83                 InputStream is = uc.getInputStream();   // Check for success.
       
    84             } catch (MalformedURLException e) {
       
    85                 uc = null;
       
    86             } catch (IOException e) {
       
    87                 uc = null;
       
    88             }
       
    89         }
       
    90 
       
    91         if (uc == null) {
       
    92             throw new IOException("Can't find file for URL: "
       
    93                                   +u.toExternalForm());
       
    94         }
       
    95         return uc;
       
    96     }
       
    97 }