Example usage for javax.servlet ServletContext getResourceAsStream

List of usage examples for javax.servlet ServletContext getResourceAsStream

Introduction

In this page you can find the example usage for javax.servlet ServletContext getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String path);

Source Link

Document

Returns the resource located at the named path as an InputStream object.

Usage

From source file:com.github.jknack.handlebars.io.ServletContextTemplateLoaderTest.java

@Test
public void subFolderwithDashAtBeginning() throws IOException {
    InputStream is = createMock(InputStream.class);
    is.close();//from  w  w  w .  ja v  a2 s.  co m
    expectLastCall();

    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);

    replay(servletContext, is);

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
    locator.setSuffix(".yml");
    Reader reader = locator.load(URI.create("/mustache/specs/comments"));
    assertNotNull(reader);

    IOUtils.closeQuietly(reader);

    verify(servletContext, is);
}

From source file:com.github.jknack.handlebars.io.ServletContextTemplateLoaderTest.java

@Test
public void setBasePath() throws IOException {
    InputStream is = createMock(InputStream.class);
    is.close();//from   w  w w. j a va2 s  . c  o  m
    expectLastCall();

    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);

    replay(servletContext, is);

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext, "/mustache/specs", ".yml");

    Reader reader = locator.load(URI.create("comments"));
    assertNotNull(reader);

    IOUtils.closeQuietly(reader);

    verify(servletContext, is);
}

From source file:com.github.jknack.handlebars.io.ServletContextTemplateLoaderTest.java

@Test
public void setBasePathWithDashDash() throws IOException {
    InputStream is = createMock(InputStream.class);
    is.close();/* www  . j a  va 2  s  .c  o m*/
    expectLastCall();

    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);

    replay(servletContext, is);

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext, "/mustache/specs", ".yml");

    Reader reader = locator.load(URI.create("comments"));
    assertNotNull(reader);

    IOUtils.closeQuietly(reader);

    verify(servletContext, is);
}

From source file:com.github.jknack.handlebars.io.ServletContextTemplateLoaderTest.java

@Test
public void subFolder() throws IOException {
    InputStream is = createMock(InputStream.class);
    is.close();// w  w  w . ja  v  a2  s.com
    expectLastCall();

    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/mustache/specs/comments.yml")).andReturn(is);

    replay(servletContext, is);

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext);

    locator.setSuffix(".yml");
    Reader reader = locator.load(URI.create("mustache/specs/comments"));
    assertNotNull(reader);

    IOUtils.closeQuietly(reader);

    verify(servletContext, is);

}

From source file:org.owasp.webgoat.application.WebGoatServletListener.java

private void setApplicationVariables(ServletContext context) {
    Application app = Application.getInstance();
    try {//w w w  . j  a  v  a2 s  . c o  m
        InputStream inputStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(inputStream);
        Attributes attr = manifest.getMainAttributes();
        String name = attr.getValue("Specification-Title");
        String version = attr.getValue("Specification-Version");
        String build = attr.getValue("Implementation-Version");
        app.setName(name);
        app.setVersion(version);
        app.setBuild(build);
    } catch (IOException ioe) {
        context.log("Error setting application variables", ioe);
    }
}

From source file:org.scenarioo.rest.application.ScenariooWebApplication.java

private void initializeApplicationVersion(final ServletContext servletContext) {
    Properties properties = new Properties();
    InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/version.properties");

    if (inputStream == null) {
        LOGGER.warn("  version.properties not found, no version information available");
        ApplicationVersionHolder.INSTANCE.initialize("unknown", "unknown", "unknown", "unknown");
        return;// w  w  w  .  j a v  a2  s . c  o m
    }

    try {
        properties.load(inputStream);
        ApplicationVersionHolder.INSTANCE.initializeFromProperties(properties);
    } catch (Exception e) {
        ApplicationVersionHolder.INSTANCE.initialize("unknown", "unknown", "unknown", "unknown");
        e.printStackTrace();
    }

    LOGGER.info("  Version: " + ApplicationVersionHolder.INSTANCE.getApplicationVersion().getVersion());
    LOGGER.info("  Build date: " + ApplicationVersionHolder.INSTANCE.getApplicationVersion().getBuildDate());
}

From source file:uk.ac.ebi.intact.widgets.webapp.servlet.cytoscape.CytoscapeServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String searchQuery = request.getParameter(PARAM_QUERY);
    String format = request.getParameter(PARAM_FORMAT);
    String negative = request.getParameter(PARAM_FILTER_NEGATIVE);
    String spoke = request.getParameter(PARAM_FILTER_SPOKE);
    String ontology = request.getParameter(PARAM_ONTOLOGY_QUERY);
    String sort = request.getParameter(PARAM_SORT);
    String asc = request.getParameter(PARAM_SORT_ASC);

    if (searchQuery == null) {
        throw new ServletException("Parameter '" + PARAM_QUERY + "' was expected");
    }//from ww w . j  a  v a2s  .  c om
    if (log.isDebugEnabled())
        log.debug("Generating a JNLP file to start Cytoscape with query: " + searchQuery);

    searchQuery = encodeURL(searchQuery);
    if (log.isTraceEnabled())
        log.trace("Encoded query: " + searchQuery);

    final String serverContext = "http://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath();
    final String exportUrl = serverContext + "/export?query=" + searchQuery + "&format=" + format
            + "&negative=" + negative + "&spoke=" + spoke + "&ontology=" + ontology + "&sort="
            + sort + "&asc=" + asc;
    ;
    response.setContentType("application/x-java-jnlp-file");

    // Read the template cytoscape.jnlp from from WEB-INF directory.
    String filename = "/WEB-INF/cytoscape/cytoscape.jnlp";
    ServletContext context = getServletContext();
    InputStream is = context.getResourceAsStream(filename);

    PrintWriter writer = null;
    try {
        if (is != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            writer = response.getWriter();
            String text = null;

            try {
                if (log.isTraceEnabled())
                    log.trace("Starting JNLP export ...");

                while ((text = reader.readLine()) != null) {
                    if (text.contains(DATA_URL)) {
                        text = text.replace(DATA_URL, exportUrl);
                    }

                    if (log.isTraceEnabled())
                        log.trace(text);

                    writer.println(text);
                    writer.flush();
                }
                if (log.isTraceEnabled())
                    log.trace("Completed JNLP export.");

            } finally {
                reader.close();
            }
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
        is.close();
    }
}

From source file:com.meltmedia.cadmium.core.config.impl.PropertiesReaderImpl.java

private void readPropertiesWithContext(Properties properties, ServletContext context, String path, Logger log) {

    Reader reader = null;//  w w  w.  j  ava  2  s. c  o m
    try {

        reader = new InputStreamReader(context.getResourceAsStream(path), "UTF-8");
        properties.load(reader);

    } catch (Exception e) {

        log.warn("Failed to load " + path);
    } finally {

        IOUtils.closeQuietly(reader);
    }

}

From source file:com.bah.bahdit.main.search.Search.java

/**
 * // w w w . j a va 2s.  co m
 * The main search object that gets a connection and sets up 
 * the Index and the configurations.
 * 
 * @param context - a servlet context from which the 
 *                search will grab its files from
 * @param indexToUse - text constant found in the search module to use.
 * @throws IOException 
 */
public Search(ServletContext context, String indexToUse) throws IOException {
    properties = new Properties();
    properties.load(context.getResourceAsStream(CONFIGURATION_FILE));
    Injector injector = Guice.createInjector(new SearcherModule(indexToUse));
    this.index = injector.getInstance(Index.class);
    this.conn = connect();
    index.configure(conn, properties, context);
}

From source file:com.ocpsoft.pretty.faces.config.spi.DefaultXMLConfigurationProvider.java

public PrettyConfig loadConfiguration(ServletContext servletContext) {
    final PrettyConfigBuilder builder = new PrettyConfigBuilder();
    PrettyConfigParser configParser = new DigesterPrettyConfigParser();
    final InputStream is = servletContext.getResourceAsStream(DEFAULT_PRETTY_FACES_CONFIG);
    if (is != null) {
        log.trace("Reading config [" + DEFAULT_PRETTY_FACES_CONFIG + "].");

        try {//  w ww  .j a  v a  2  s .c o  m
            configParser.parse(builder, is);
        } catch (Exception e) {
            throw new PrettyException(
                    "Failed to parse PrettyFaces configuration from " + DEFAULT_PRETTY_FACES_CONFIG, e);
        } finally {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
    }

    return builder.build();
}