Package webit.script.servlet.loaders

Source Code of webit.script.servlet.loaders.ServletContextResource

// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.script.servlet.loaders;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.ServletContext;
import webit.script.exceptions.ResourceNotFoundException;
import webit.script.loaders.Resource;

/**
*
* @author zqq90
*/
public class ServletContextResource implements Resource {

    private final String path;
    private final String encoding;
    private final ServletContext servletContext;

    public ServletContextResource(String path, String encoding, ServletContext servletContext) {
        this.path = path;
        this.encoding = encoding;
        this.servletContext = servletContext;
    }

    public boolean isModified() {
        return false;
    }

    public Reader openReader() throws IOException {
        final InputStream in = servletContext.getResourceAsStream(path);
        if (in != null) {
            return new InputStreamReader(in, encoding);
        }
        throw new ResourceNotFoundException("Resource Not Found: ".concat(path));
    }

    /**
     * @since 1.4.1
     */
    public boolean exists() {
        try {
            if (servletContext.getResource(path) != null) {
                return true;
            }
        } catch (Exception ignore) {
        }
        return false;
    }
}
TOP

Related Classes of webit.script.servlet.loaders.ServletContextResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.