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 
  44 public class Handler extends URLStreamHandler {
  45     static URL base;
  46 
  47     /*
  48      * Attempt to find a load the given url using the default (network)
  49      * documentation location.  If that fails, use the local copy
  50      */
  51     public synchronized URLConnection openConnection(URL u)
  52         throws IOException
  53     {
  54         URLConnection uc = null;
  55         URL ru;
  56 
  57         Boolean tmp = java.security.AccessController.doPrivileged(
  58             new sun.security.action.GetBooleanAction("newdoc.localonly"));
  59         boolean localonly = tmp.booleanValue();
  60 
  61         String docurl = java.security.AccessController.doPrivileged(
  62             new sun.security.action.GetPropertyAction("doc.url"));
  63 
  64         String file = u.getFile();
  65         if (!localonly) {
  66             try {
  67                 if (base == null) {
  68                     base = new URL(docurl);
  69                 }
  70                 ru = new URL(base, file);
  71             } catch (MalformedURLException e) {
  72                 ru = null;
  73             }
  74             if (ru != null) {
  75                 uc = ru.openConnection();
  76             }
  77         }
  78 
  79         if (uc == null) {
  80             try {
  81                 ru = new URL("file", "~", file);
  82 
  83                 uc = ru.openConnection();
  84                 InputStream is = uc.getInputStream();   // Check for success.
  85             } catch (MalformedURLException e) {
  86                 uc = null;
  87             } catch (IOException e) {
  88                 uc = null;
  89             }
  90         }
  91 
  92         if (uc == null) {
  93             throw new IOException("Can't find file for URL: "
  94                                   +u.toExternalForm());
  95         }
  96         return uc;
  97     }
  98 }