1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 package com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
  22 
  23 import java.io.FileInputStream;
  24 
  25 import com.sun.org.apache.xml.internal.utils.URI;
  26 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
  27 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
  28 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
  29 import org.w3c.dom.Attr;
  30 
  31 /**
  32  * A simple ResourceResolver for requests into the local filesystem.
  33  *
  34  * @author $Author: mullan $
  35  */
  36 public class ResolverLocalFilesystem extends ResourceResolverSpi {
  37 
  38    /** {@link java.util.logging} logging facility */
  39     static java.util.logging.Logger log =
  40         java.util.logging.Logger.getLogger(
  41                     ResolverLocalFilesystem.class.getName());
  42 
  43     public boolean engineIsThreadSafe() {
  44            return true;
  45    }
  46    /**
  47     * @inheritDoc
  48     */
  49    public XMLSignatureInput engineResolve(Attr uri, String BaseURI)
  50            throws ResourceResolverException {
  51 
  52      try {
  53         URI uriNew = getNewURI(uri.getNodeValue(), BaseURI);
  54 
  55         // if the URI contains a fragment, ignore it
  56         URI uriNewNoFrag = new URI(uriNew);
  57 
  58         uriNewNoFrag.setFragment(null);
  59 
  60         String fileName =
  61            ResolverLocalFilesystem
  62               .translateUriToFilename(uriNewNoFrag.toString());
  63         FileInputStream inputStream = new FileInputStream(fileName);
  64         XMLSignatureInput result = new XMLSignatureInput(inputStream);
  65 
  66         result.setSourceURI(uriNew.toString());
  67 
  68         return result;
  69      } catch (Exception e) {
  70         throw new ResourceResolverException("generic.EmptyMessage", e, uri,
  71                                             BaseURI);
  72       }
  73    }
  74 
  75    private static int FILE_URI_LENGTH="file:/".length();
  76    /**
  77     * Method translateUriToFilename
  78     *
  79     * @param uri
  80     * @return the string of the filename
  81     */
  82    private static String translateUriToFilename(String uri) {
  83 
  84       String subStr = uri.substring(FILE_URI_LENGTH);
  85 
  86       if (subStr.indexOf("%20") > -1)
  87       {
  88         int offset = 0;
  89         int index = 0;
  90         StringBuffer temp = new StringBuffer(subStr.length());
  91         do
  92         {
  93           index = subStr.indexOf("%20",offset);
  94           if (index == -1) temp.append(subStr.substring(offset));
  95           else
  96           {
  97             temp.append(subStr.substring(offset,index));
  98             temp.append(' ');
  99             offset = index+3;
 100           }
 101         }
 102         while(index != -1);
 103         subStr = temp.toString();
 104       }
 105 
 106       if (subStr.charAt(1) == ':') {
 107          // we're running M$ Windows, so this works fine
 108          return subStr;
 109       }
 110       // we're running some UNIX, so we have to prepend a slash
 111       return "/" + subStr;
 112    }
 113 
 114    /**
 115     * @inheritDoc
 116     */
 117    public boolean engineCanResolve(Attr uri, String BaseURI) {
 118 
 119       if (uri == null) {
 120          return false;
 121       }
 122 
 123       String uriNodeValue = uri.getNodeValue();
 124 
 125       if (uriNodeValue.equals("") || (uriNodeValue.charAt(0)=='#') ||
 126           uriNodeValue.startsWith("http:")) {
 127          return false;
 128       }
 129 
 130       try {
 131                  //URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
 132                  if (log.isLoggable(java.util.logging.Level.FINE))
 133                         log.log(java.util.logging.Level.FINE, "I was asked whether I can resolve " + uriNodeValue/*uriNew.toString()*/);
 134 
 135                  if ( uriNodeValue.startsWith("file:") ||
 136                                          BaseURI.startsWith("file:")/*uriNew.getScheme().equals("file")*/) {
 137                     if (log.isLoggable(java.util.logging.Level.FINE))
 138                         log.log(java.util.logging.Level.FINE, "I state that I can resolve " + uriNodeValue/*uriNew.toString()*/);
 139 
 140                     return true;
 141                  }
 142       } catch (Exception e) {}
 143 
 144       log.log(java.util.logging.Level.FINE, "But I can't");
 145 
 146       return false;
 147    }
 148 
 149    private static URI getNewURI(String uri, String BaseURI)
 150            throws URI.MalformedURIException {
 151 
 152       if ((BaseURI == null) || "".equals(BaseURI)) {
 153          return new URI(uri);
 154       }
 155       return new URI(new URI(BaseURI), uri);
 156    }
 157 }