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:rapture.server.web.servlet.ReflexScriptPageServlet.java

@Override
protected String getReflexScript(HttpServletRequest req) {
    ServletContext context = getServletContext();
    String result = null;//  w w w  .j a  v  a  2s  .  c  o  m
    try (InputStream is = context.getResourceAsStream(req.getServletPath())) {
        if (is != null) {
            result = ResourceLoader.getResourceFromInputStream(is);
        }
    } catch (IOException e) {
        log.error(ExceptionToString.format(e));
    }
    return result;
}

From source file:com.sri.save.FloraHttpServlet.java

@Override
public void init() throws ServletException {
    Properties properties = new Properties();
    try {//from w ww . j a v  a2 s. com
        ServletContext context = getServletContext();
        properties.load(context.getResourceAsStream("/WEB-INF/config.properties"));
    } catch (IOException e) {
        throw new ServletException("Could not open config.properties!");
    }
    String xsb = properties.getProperty("xsb");
    String floradir = properties.getProperty("flora.dir");
    String content_dir = properties.getProperty("ontology.dir"); // base dir for imported files

    // Initialize the server
    try {
        florajson = new FloraJsonServer(new File(xsb), new File(floradir), new File(content_dir));
    } catch (FloraJsonServerException ex) {
        throw new ServletException(ex);
    }
}

From source file:$.ExampleResource.java

/**
     * Default view for the base path.//from  w w w. ja v  a2s .c o m
     *
     * @param context
     * @return
     * @throws IOException
     */
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String get(@Context ServletContext context) throws IOException {
        InputStream is = context.getResourceAsStream("/index.html");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringWriter sw = new StringWriter();
        String text = "";

        while ((text = reader.readLine()) != null) {
            sw.write(text);
        }
        return sw.toString();
    }

From source file:com.thejustdo.util.Utils.java

/**
 * Given a temporal directory and getting help from the Constants class, 
 * we may copy if necessary all the info required for the correct generation 
 * of PDF files.//from w ww  . j  a v  a 2s .c  o m
 * @param tmp Where is the temporal directory of the server.
 * @return The directory in which the temporal files can be saved.
 */
public static File preparePDFDirs(File tmp, ServletContext sc) throws IOException {
    // 0. Creating the pdf directory if needed.
    File pdf = new File(tmp, Constants.TMP_PDF);

    if (pdf.exists()) {
        log.info(String.format("Found temporal directory for pdfs %s", pdf.getAbsolutePath()));
        return pdf;
    }

    // 1. Creating the structure.
    pdf.mkdirs();

    File tmpImg = new File(pdf, "images");
    tmpImg.mkdirs();

    // 2. Copying content.
    InputStream is;
    FileOutputStream os;
    File goal;
    int read;
    byte bytes[] = new byte[1024];
    for (String s : Constants.PDF_IMGS) {
        log.info(String.format("Copying file %s%s", Constants.PDF_IMAGE_PATH, s));
        is = sc.getResourceAsStream(Constants.PDF_IMAGE_PATH + s);
        goal = new File(tmpImg, s);
        os = new FileOutputStream(goal);

        // Writing file.
        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }

        // Done writing.
        os.flush();
        os.close();
        is.close();
    }

    // File origin = new File(sc.getRealPath(Constants.PDF_IMAGE_PATH));
    // FileUtils.copyDirectoryToDirectory(origin, pdf);
    log.info(String.format("Created pdf data at: %s", pdf.getAbsolutePath()));

    return pdf;
}

From source file:fm.last.citrine.web.AdminController.java

@Override
protected void initServletContext(ServletContext servletContext) {
    super.initServletContext(servletContext);
    try {//from w  ww .  j a  v  a  2s .co  m
        InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(inputStream);
        Attributes attributes = manifest.getMainAttributes();
        buildVersion = attributes.getValue("Build-Version");
        buildDateTime = attributes.getValue("Build-DateTime");
        log.info("Citrine Build-Version: " + attributes.getValue("Build-Version"));
        log.info("Citrine Build-DateTime: " + attributes.getValue("Build-DateTime"));
    } catch (Exception e) {
        log.error("Error determining build version", e);
    }
}

From source file:com.boylesoftware.web.impl.routes.RoutesRouterConfiguration.java

@Override
protected void buildRoutes(final ServletContext sc, final RoutesBuilder routes) throws UnavailableException {

    try (final InputStream in = sc.getResourceAsStream(ROUTES_PATH)) {

        if (in == null)
            throw new UnavailableException("No " + ROUTES_PATH + " found in the web-application.");

        final RoutesParser parser = new RoutesParser(
                new CommonTokenStream(new RoutesLexer(new ANTLRInputStream(in))));
        parser.setErrorHandler(new BailErrorStrategy());
        parser.setRoutesBuilder(routes);
        try {/* w  ww .  java2 s  . c o  m*/
            parser.config();
        } catch (final Exception e) {
            this.error(e);
        }

    } catch (final IOException e) {
        this.error(e);
    }
}

From source file:com.jaeksoft.searchlib.web.Version.java

public Version(ServletContext servletContext) throws IOException {
    InputStream is = null;/*from   ww  w . java  2  s  .c o  m*/
    try {
        is = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
        if (is != null) {
            Manifest manifest = new Manifest(is);
            Attributes attributes = manifest.getMainAttributes();
            title = attributes.getValue("Implementation-Title");
            version = attributes.getValue("Implementation-Version");
            build = attributes.getValue("Implementation-Build");
        } else {
            title = null;
            version = null;
            build = null;
        }
        updateUrl = toUpdateUrl();
        versionString = toVersionString();
    } finally {
        if (is != null)
            is.close();
    }
}

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

@Test(expected = FileNotFoundException.class)
public void failLocate() throws IOException {
    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/notExist.hbs")).andReturn(null);

    replay(servletContext);/*from  w  ww.j a v  a 2 s  .com*/

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext);

    locator.load(URI.create("notExist"));

    verify(servletContext);

}

From source file:edu.cornell.mannlib.vitro.webapp.config.RevisionInfoSetup.java

private BufferedReader openRevisionInfoReader(ServletContext context) throws FileNotFoundException {
    InputStream stream = context.getResourceAsStream(RESOURCE_PATH);
    if (stream == null) {
        throw new FileNotFoundException("Can't find a resource in the webapp at '" + RESOURCE_PATH + "'.");
    } else {//from  www .  j  a  v  a  2 s. c  o  m
        return new BufferedReader(new InputStreamReader(stream));
    }
}

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

@Test
public void defaultLoad() throws IOException {
    InputStream is = createMock(InputStream.class);
    is.close();/*from   ww  w.j a  v  a 2 s.co  m*/
    expectLastCall();

    ServletContext servletContext = createMock(ServletContext.class);
    expect(servletContext.getResourceAsStream("/template.hbs")).andReturn(is);

    replay(servletContext, is);

    TemplateLoader locator = new ServletContextTemplateLoader(servletContext);
    Reader reader = locator.load(URI.create("template"));
    assertNotNull(reader);
    IOUtils.closeQuietly(reader);

    verify(servletContext, is);
}