All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.sbt.playground.vfs.ServletVFS Maven / Gradle / Ivy

There is a newer version: 1.1.12.20161007-1200
Show newest version
/*
 * Copyright IBM Corp. 2012
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */
package com.ibm.sbt.playground.vfs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import javax.servlet.ServletContext;

import com.ibm.commons.util.StringUtil;


/**
 * VFS to access files from the file system.
 * 
 * @author priand
 */
public class ServletVFS extends VFS {

	protected static class ServletVFSFile extends VFSFile {
		private ServletContext context;
		private String dir;
		private VFSFile[] children;
		public ServletVFSFile(ServletVFS vfs, VFSFile parent, String name, ServletContext context, String dir) {
			super(vfs,parent,name);
			this.context = context;
			this.dir = dir;
		}
		@Override
		public ServletVFS getVFS() {
			return (ServletVFS)super.getVFS();
		}
		@Override
		public boolean isFile() throws IOException {
			return !dir.endsWith("/");
		}
		@Override
		public boolean isFolder() throws IOException {
			return dir.endsWith("/");
		}
		@Override
		public boolean exists() throws IOException {
			// Always exists...
			return true;
		}
		// File content
		@Override
		public InputStream getInputStream() throws IOException {
			return context.getResourceAsStream(dir);
		}
		@Override
		public OutputStream getOutputStream() throws IOException {
			throw new IOException("Cannot create an OutputStream in a servlet context");
		}
		@Override
		public void delete() throws IOException {
			throw new IOException("Cannot create a file in a servlet context");
		}
		// Children
		@Override
		public VFSFile getChild(String name) throws IOException {
			VFSFile[] children = getChildren();
			if(children!=null) {
				for(int i=0; i children = context.getResourcePaths(dir);
				VFSFile[] files = new VFSFile[children.size()];
				int i=0;
				for(String s: children) {
					String name = null;
					if(s.endsWith("/")) {
						name = s.substring(dir.length(),s.length()-1);
					} else {
						name = s.substring(dir.length());
					}
					files[i++]= new ServletVFSFile(getVFS(),this, name, context, s);
				}
				this.children = files;
				return files;
			}
			return children;
		}
	}

	private ServletVFSFile root;
	
	public ServletVFS(ServletContext context, String baseDir) {
		this.root = new ServletVFSFile(this,null, "", context, baseDir);
	}

	@Override
	public VFSFile getRoot() {
		return root;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy