Example usage for org.springframework.web.context.support ServletContextResource getInputStream

List of usage examples for org.springframework.web.context.support ServletContextResource getInputStream

Introduction

In this page you can find the example usage for org.springframework.web.context.support ServletContextResource getInputStream.

Prototype

@Override
public InputStream getInputStream() throws IOException 

Source Link

Document

This implementation delegates to ServletContext.getResourceAsStream , but throws a FileNotFoundException if no resource found.

Usage

From source file:com.kurento.kmf.spring.KurentoApplicationContextUtils.java

/**
 * This class returns the Spring KurentoApplicationContext, which is the
 * parent context for all specific Kurento Servlet contexts. In case a
 * pre-exiting Spring root WebApplicationContext if found, the returned
 * KurentoApplicationContext will be made child of this root context. When
 * necessary, this method creates the KurentoApplicationContext, so it
 * should never return null./*from w  w w .j a  v  a2 s .  c  o m*/
 * 
 * This method MUST NOT be called in ServletContextListeners, given that at
 * that stage there might not be information about the presence of a root
 * Spring root WebApplicationConext.
 * 
 * @param ctx
 * @return
 * 
 */
public static AnnotationConfigApplicationContext createKurentoApplicationContext(ServletContext ctx) {
    Assert.notNull(ctx, "Cannot recover KurentoApplicationContext from a null ServletContext");
    Assert.isNull(kurentoApplicationContextInternalReference,
            "Pre-existing Kurento ApplicationContext found. Cannot create a new instance.");

    kurentoApplicationContextInternalReference = new AnnotationConfigApplicationContext();

    // Add or remove packages when required
    kurentoApplicationContextInternalReference.scan("com.kurento.kmf");

    // Recover root WebApplicationContext context just in case
    // application developer is using Spring
    WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(ctx);
    if (rootContext != null) {
        kurentoApplicationContextInternalReference.setParent(rootContext);
    }

    final String jbossServerConfigDir = System.getProperty("jboss.server.config.dir");
    final String kurentoPropertiesDir = System.getProperty("kurento.properties.dir");
    final String kurentoProperties = "/kurento.properties";
    InputStream inputStream = null;
    try {
        if (jbossServerConfigDir != null && new File(jbossServerConfigDir + kurentoProperties).exists()) {
            // First, look for JVM argument "jboss.server.config.dir"
            inputStream = new FileInputStream(jbossServerConfigDir + kurentoProperties);
            log.info("Found custom properties in 'jboss.server.config.dir': " + jbossServerConfigDir);
        } else if (kurentoPropertiesDir != null
                && new File(kurentoPropertiesDir + kurentoProperties).exists()) {
            // Second, look for JVM argument "kurento.properties.dir"
            log.info("Found custom properties in 'kurento.properties.dir': " + kurentoPropertiesDir);
            inputStream = new FileInputStream(kurentoPropertiesDir + kurentoProperties);
        } else {
            // Third, look for properties in Servlet Context
            ServletContextResource servletContextResource = new ServletContextResource(ctx,
                    "/WEB-INF" + kurentoProperties);
            if (servletContextResource.exists()) {
                log.info("Found custom properties in Servlet Context: /WEB-INF" + kurentoProperties);
                inputStream = servletContextResource.getInputStream();
            }
        }

        if (inputStream != null) {
            Properties properties = new Properties();
            properties.load(inputStream);
            PropertyOverrideConfigurer propertyOverrideConfigurer = new PropertyOverrideConfigurer();
            propertyOverrideConfigurer.setProperties(properties);
            kurentoApplicationContextInternalReference.addBeanFactoryPostProcessor(propertyOverrideConfigurer);
            inputStream.close();
        }

    } catch (IOException e) {
        log.error("Exception loading custom properties", e);
        throw new RuntimeException(e);
    }

    kurentoApplicationContextInternalReference.refresh();
    return kurentoApplicationContextInternalReference;
}

From source file:inti.ws.spring.resource.template.TemplateResource.java

public TemplateResource(ServletContext ctx, String moduleName, String containerLocation,
        String templateLocation, List<WebResource> files, Map<String, Object> parameters, Minifier minifier,
        boolean minify) throws Exception {
    super(ctx, containerLocation, null, minifier, minify);

    ServletContextResource resource;
    InputStream inputStream;/*from   w  ww.  j a  v a2s.co  m*/

    this.moduleName = moduleName;
    this.parameters = parameters;
    if (files != null) {
        this.files.addAll(files);
    }

    if (templateLocation != null) {
        resource = new ServletContextResource(ctx, templateLocation);
        inputStream = resource.getInputStream();

        try {
            template = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        } finally {
            inputStream.close();
        }
    }
}

From source file:piecework.ui.StaticResourceAggregator.java

private BufferedReader reader(String path) throws Exception {
    String fullPath;/* www.jav  a 2 s.  c o m*/
    String base = formDisposition != null ? formDisposition.getBase() : null;
    if (StringUtils.isNotEmpty(base)) {
        // Only add the base if we're looking for the resource in the
        // repository
        Scheme scheme = PathUtility.findScheme(path, null);
        if (scheme == Scheme.REPOSITORY)
            fullPath = base + "/" + path;
        else
            fullPath = path;
    } else {
        fullPath = path;
    }

    if (!PathUtility.checkForStaticPath(path)) {
        Scheme scheme = PathUtility.findScheme(path, null);
        // Since we can trust that static resources are not trying to move above their base,
        // allow for cases where the path is on the file system
        if (scheme != Scheme.REPOSITORY)
            base = null;

        ContentResource contentResource = modelProvider != null
                ? contentRepository.findByLocation(modelProvider, fullPath)
                : null;
        if (contentResource != null) {
            return new BufferedReader(new InputStreamReader(contentResource.getInputStream()));
        }
        return null;
    }

    int indexOf = path.indexOf("static/");

    if (indexOf > path.length())
        return null;

    String adjustedPath = path.substring(indexOf);

    if (StringUtils.isNotEmpty(settings.getAssetsDirectoryPath())) {
        File file = new File(settings.getAssetsDirectoryPath(), adjustedPath);
        if (file.exists()) {
            String absolutePath = file.getAbsolutePath();
            LOG.debug("Reading from " + absolutePath);
            if (!file.exists())
                return null;

            return new BufferedReader(new FileReader(file));
        }
    }

    ServletContextResource servletContextResource = new ServletContextResource(servletContext, adjustedPath);

    if (!servletContextResource.exists())
        return null;

    return new BufferedReader(new InputStreamReader(servletContextResource.getInputStream()));
}