001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.vfs.spi.servlet;
021    
022    import org.crsh.vfs.spi.AbstractFSDriver;
023    
024    import javax.servlet.ServletContext;
025    import java.io.File;
026    import java.io.FileInputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.net.URL;
030    import java.util.Collections;
031    import java.util.Set;
032    import java.util.regex.Matcher;
033    import java.util.regex.Pattern;
034    
035    public class ServletContextDriver extends AbstractFSDriver<String> {
036    
037      /** A valid path. */
038      static final Pattern pathPattern = Pattern.compile("^(?=/).*?((?<=/)[^/]*)?(/?)$");
039    
040      /** . */
041      private final ServletContext ctx;
042    
043      /** . */
044      private final String root;
045    
046      public ServletContextDriver(ServletContext ctx, String root) {
047        if (ctx == null) {
048          throw new NullPointerException();
049        }
050        if (root == null) {
051          throw new NullPointerException();
052        }
053        assertMatch(root);
054    
055        //
056        this.ctx = ctx;
057        this.root = root;
058      }
059    
060      public String root() throws IOException {
061        return root;
062      }
063    
064      public String name(String file) throws IOException {
065        return assertMatch(file).group(1);
066      }
067    
068      public boolean isDir(String file) throws IOException {
069        Matcher matcher = assertMatch(file);
070        String slash = matcher.group(2);
071        return "/".equals(slash);
072      }
073    
074      public Iterable<String> children(String parent) throws IOException {
075        @SuppressWarnings("unchecked")
076        Set<String> resourcePaths = (Set<String>)ctx.getResourcePaths(parent);
077        return resourcePaths != null ? resourcePaths : Collections.<String>emptyList();
078      }
079    
080      /**
081       * The implementation attempts to get an URL that will be valid for the file system first (when the
082       * war is usually exploded) and if it is not able, it will delegate to {@link ServletContext#getResource(String)}.
083       *
084       * @param file the file path
085       * @return the URL
086       * @throws IOException any io exception
087       */
088      public URL toURL(String file) throws IOException {
089        String realPath = ctx.getRealPath(file);
090        if (realPath != null) {
091          File realFile = new File(realPath);
092          if (realFile.exists() && realFile.isFile()) {
093            return realFile.toURI().toURL();
094          }
095        }
096        return ctx.getResource(file);
097      }
098    
099      public long getLastModified(String handle) throws IOException {
100        String realPath = ctx.getRealPath(handle);
101        if (realPath != null) {
102          File realFile = new File(realPath);
103          if (realFile.exists() && realFile.isFile()) {
104            return realFile.lastModified();
105          }
106        }
107        return ctx.getResource(handle).openConnection().getLastModified();
108      }
109    
110      public InputStream open(String handle) throws IOException {
111        String realPath = ctx.getRealPath(handle);
112        if (realPath != null) {
113          File realFile = new File(realPath);
114          if (realFile.exists() && realFile.isFile()) {
115            return new FileInputStream(realFile);
116          }
117        }
118        return ctx.getResource(handle).openConnection().getInputStream();
119      }
120    
121      private Matcher assertMatch(String path) {
122        Matcher m = pathPattern.matcher(path);
123        if (m.matches()) {
124          return m;
125        } else {
126          throw new IllegalArgumentException("Illegal path " + path);
127        }
128      }
129    }