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

org.febit.wit.servlet.loaders.ServletContextResource Maven / Gradle / Ivy

There is a newer version: 2.7.0-beta
Show newest version
// Copyright (c) 2013-2016, febit.org. All Rights Reserved.
package org.febit.wit.servlet.loaders;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.ServletContext;
import org.febit.wit.exceptions.ResourceNotFoundException;
import org.febit.wit.loaders.Resource;

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

    protected final String path;
    protected final String encoding;
    protected final ServletContext servletContext;
    protected final boolean codeFirst;

    /**
     *
     * @param path
     * @param encoding
     * @param servletContext
     */
    public ServletContextResource(String path, String encoding, ServletContext servletContext) {
        this(path, encoding, servletContext, false);
    }

    /**
     *
     * @param path
     * @param encoding
     * @param servletContext
     * @param codeFirst
     * @since 2.0.0
     */
    public ServletContextResource(String path, String encoding, ServletContext servletContext, boolean codeFirst) {
        this.path = path;
        this.encoding = encoding;
        this.servletContext = servletContext;
        this.codeFirst = codeFirst;
    }

    @Override
    public boolean isModified() {
        return false;
    }

    @Override
    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
     */
    @Override
    public boolean exists() {
        try {
            if (servletContext.getResource(path) != null) {
                return true;
            }
        } catch (Exception ignore) {
        }
        return false;
    }

    /**
     * @since 2.0.0
     */
    @Override
    public boolean isCodeFirst() {
        return codeFirst;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy