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:org.opentides.web.controller.ImageController.java

/**
 * Loads the default image // w  w w . jav a  2s . co m
 * @param request
 * @return
 */
private final byte[] defaultImage(HttpServletRequest request) {
    if (StringUtil.isEmpty(defaultImageLocation))
        return ImageUtil.getDefaultImage();
    else {
        HttpSession session = request.getSession();
        ServletContext sc = session.getServletContext();
        InputStream is = sc.getResourceAsStream(defaultImageLocation);
        byte[] barray;

        try {
            barray = IOUtils.toByteArray(is);
        } catch (IOException e) {
            _log.error("Failed to load default image [" + defaultImageLocation + "].", e);
            return ImageUtil.getDefaultImage();
        }

        return barray;
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.servlet.setup.SimpleReasonerSetup.java

/**
 * Read the names of the plugin classes classes.
 * /*  www.  jav  a  2  s.  com*/
 * If there is a problem, set a fatal error, and return an empty list.
 */
private List<String> readFileOfListeners(ServletContext ctx) {
    List<String> list = new ArrayList<String>();

    StartupStatus ss = StartupStatus.getBean(ctx);

    InputStream is = null;
    BufferedReader br = null;
    try {
        is = ctx.getResourceAsStream(FILE_OF_PLUGINS);
        br = new BufferedReader(new InputStreamReader(is));

        String line;
        while (null != (line = br.readLine())) {
            String trimmed = line.trim();
            if (!trimmed.isEmpty() && !trimmed.startsWith("#")) {
                list.add(trimmed);
            }
        }
    } catch (NullPointerException e) {
        // ignore the lack of file
    } catch (IOException e) {
        ss.fatal(this, "Failed while processing the list of startup listeners:  " + FILE_OF_PLUGINS, e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }

    log.debug("Classnames of reasoner plugins = " + list);
    return list;
}

From source file:com.bah.bahdit.main.plugins.imageindex.ImageIndex.java

/**
 * Loads the necessary files from the properties file
 * called from FullTextIndex.configure//w  w w  . j a  va  2s .  co  m
 * 
 * @param context - passed from the servlet
 */
@SuppressWarnings("unchecked")
private void loadResources(ServletContext context) {
    try {
        // get the sample table from resources
        InputStream sample = context.getResourceAsStream(properties.getProperty(IMG_TAG_SAMPLE_TABLE));
        InputStream samplebuffer = new BufferedInputStream(sample);
        ObjectInput objectsample;
        objectsample = new ObjectInputStream(samplebuffer);
        tagSampleTable = (HashMap<String, Integer>) objectsample.readObject();

        tagSpellChecker = LevenshteinDistance.createSpellChecker(context, tagSampleTable);

    } catch (IOException e) {
        log.warn(e.getMessage());
    } catch (ClassNotFoundException e) {
        log.warn(e.getMessage());
    }
}

From source file:org.jfree.eastwood.ChartServlet.java

private Font readFontResource(ServletContext ctx, String fontResourceParam, float fontSize)
        throws ServletException {

    if (!fontResourceParam.startsWith("/")) {
        fontResourceParam = "/" + fontResourceParam;
    }//from  w  w w . ja va  2s .co  m
    InputStream is = ctx.getResourceAsStream(fontResourceParam);
    if (is == null) {
        throw new ServletException("Font resource '" + fontResourceParam + "' not found");
    }
    try {
        return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(fontSize);
    } catch (FontFormatException e) {
        throw new ServletException("Font resource '" + fontResourceParam + "' is not a truetype font", e);
    } catch (IOException e) {
        throw new ServletException("I/O error when reading font resource '" + fontResourceParam + "'", e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.liusoft.dlog4j.action.ActionBase.java

/**
 * ?/*  www  .  j av a 2  s . c o  m*/
 * @return
 * @throws IOException
 */
protected String getTemplate(String tmp) throws IOException {
    ServletContext sc = getServlet().getServletContext();
    InputStream in = sc.getResourceAsStream(tmp);
    StringBuffer template = new StringBuffer(512);
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        do {
            String line = reader.readLine();
            if (line == null)
                break;
            template.append(line);
            template.append("\r\n");
        } while (true);
    } finally {
        in.close();
    }
    return template.toString();
}

From source file:org.wso2.carbon.discovery.cxf.listeners.TomcatCxfDiscoveryListener.java

private InputStream getConfigLocation(ServletContext context) {

    String configLocation = context.getInitParameter("config-location");
    if (configLocation == null) {
        try {//ww w.  j a  v  a  2  s. c  o m
            InputStream is = context.getResourceAsStream("/WEB-INF/cxf-servlet.xml");
            if (is != null && is.available() > 0) {
                is.close();
                configLocation = "/WEB-INF/cxf-servlet.xml";
            }
        } catch (Exception ex) {
            //ignore
        }
    }

    return context.getResourceAsStream(configLocation);
}

From source file:net.sf.click.jquery.examples.page.SourceViewer.java

private void loadFilename(String filename) {
    ServletContext context = getContext().getServletContext();

    // Orion server requires '/' prefix to find resources
    String resourceFilename = (filename.charAt(0) != '/') ? "/" + filename : filename;

    InputStream in = null;/*w  w  w.  j av a  2  s.  co m*/
    try {
        in = context.getResourceAsStream(resourceFilename);

        if (in == null && filename.endsWith(".htm")) {
            resourceFilename = resourceFilename.substring(0, resourceFilename.length() - 4) + ".jsp";

            in = context.getResourceAsStream(resourceFilename);
        }

        if (in != null) {

            loadResource(in, filename);

        } else {
            addModel("error", "File " + resourceFilename + " not found");
        }

    } catch (IOException e) {
        addModel("error", "Could not read " + resourceFilename);

    } finally {
        ClickUtils.close(in);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.services.shortview.FakeApplicationOntologyService.java

/**
 * Load the short view config file into an OntModel.
 *///www .  jav  a2s. c  o  m
private OntModel createModelFromFile(ServletContext ctx) throws ShortViewConfigException {
    InputStream stream = ctx.getResourceAsStream(FILE_OF_SHORT_VIEW_INFO);
    if (stream == null) {
        throw new ShortViewConfigException("The short view config file "
                + "doesn't exist in the servlet context: '" + FILE_OF_SHORT_VIEW_INFO + "'");
    }

    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
    try {
        m.read(stream, null, "N3");
    } catch (Exception e) {
        throw new ShortViewConfigException("Parsing error in the short view config file.", e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    log.debug("Loaded " + m.size() + " statements");
    return m;
}

From source file:org.apereo.portal.portlet.container.services.LocalPortletContextManager.java

/**
 * Creates the portlet.xml deployment descriptor representation.
 *
 * @param servletContext  the servlet context for which the DD is requested.
 * @return the Portlet Application Deployment Descriptor.
 * @throws PortletContainerException/*  w ww . ja v  a 2s .  co m*/
 */
private PortletApplicationDefinition createDefinition(ServletContext servletContext, String name,
        String contextPath) throws PortletContainerException {
    PortletApplicationDefinition portletApp = null;
    try {
        InputStream paIn = servletContext.getResourceAsStream(PORTLET_XML);
        InputStream webIn = servletContext.getResourceAsStream(WEB_XML);
        if (paIn == null) {
            throw new PortletContainerException(
                    "Cannot find '" + PORTLET_XML + "'. Are you sure it is in the deployed package?");
        }
        if (webIn == null) {
            throw new PortletContainerException(
                    "Cannot find '" + WEB_XML + "'. Are you sure it is in the deployed package?");
        }
        portletApp = this.portletAppDescriptorService.read(name, contextPath, paIn);
        this.portletAppDescriptorService.mergeWebDescriptor(portletApp, webIn);
    } catch (Exception ex) {
        throw new PortletContainerException(
                "Exception loading portlet descriptor for: " + servletContext.getServletContextName(), ex);
    }
    return portletApp;
}

From source file:com.bah.bahdit.main.plugins.fulltextindex.FullTextIndex.java

/**
 * loads the necessary files from the properties file
 * called from FullTextIndex.configure/*www  .ja v  a  2 s  . c  o m*/
 * 
 * @param context - passed from the servlet
 */
@SuppressWarnings("unchecked")
private void loadResources(ServletContext context) {

    // get the sample table from resources
    try {
        InputStream sample = context.getResourceAsStream(properties.getProperty(FT_SAMPLE));
        InputStream samplebuffer = new BufferedInputStream(sample);
        ObjectInput objectsample = new ObjectInputStream(samplebuffer);
        sampleTable = (HashMap<String, Integer>) objectsample.readObject();
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    // get the pagerank table from resources
    try {
        InputStream pagerank = context.getResourceAsStream(properties.getProperty(PR_FILE));
        InputStream pagerankbuffer = new BufferedInputStream(pagerank);
        ObjectInput objectpagerank = new ObjectInputStream(pagerankbuffer);
        pagerankTable = (HashMap<String, Double>) objectpagerank.readObject();
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    // get the spell checker
    spellChecker = LevenshteinDistance.createSpellChecker(context, sampleTable);

    // create stop words list from general list
    try {
        stopWords = new HashSet<String>();
        InputStream gstop = context.getResourceAsStream(properties.getProperty(Search.GENERAL_STOP));
        DataInputStream gin = new DataInputStream(gstop);
        BufferedReader gbr = new BufferedReader(new InputStreamReader(gin));
        String strLine;
        while ((strLine = gbr.readLine()) != null)
            stopWords.add(strLine);
    } catch (IOException e) {
        log.error(e.getMessage());
    }

}