List of usage examples for javax.servlet ServletContext getResourceAsStream
public InputStream getResourceAsStream(String path);
InputStream
object. From source file:org.ajax4jsf.resource.ResourceBuilderImpl.java
/** * Build resource for link to static context in webapp. * /*from w w w . j ava 2s. c o m*/ * @param path * @return * @throws FacesException */ protected InternetResource createStaticResource(String path) throws ResourceNotFoundException, FacesException { FacesContext context = FacesContext.getCurrentInstance(); if (null != context) { if (context.getExternalContext().getContext() instanceof ServletContext) { ServletContext servletContext = (ServletContext) context.getExternalContext().getContext(); InputStream in = servletContext.getResourceAsStream(path); if (null != in) { InternetResourceBase res = new StaticResource(path); setRenderer(res, path); res.setLastModified(new Date(getStartTime())); addResource(path, res); try { in.close(); } catch (IOException e) { } return res; } } } throw new ResourceNotFoundException(Messages.getMessage(Messages.STATIC_RESOURCE_NOT_FOUND_ERROR, path)); }
From source file:org.apache.struts2.dispatcher.PlainTextResult.java
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { // verify charset Charset charset = null;//w ww . j av a2 s . c o m if (charSet != null) { if (Charset.isSupported(charSet)) { charset = Charset.forName(charSet); } else { _log.warn("charset [" + charSet + "] is not recognized "); charset = null; } } HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); ServletContext servletContext = (ServletContext) invocation.getInvocationContext().get(SERVLET_CONTEXT); if (charset != null) { response.setContentType("text/plain; charset=" + charSet); } else { response.setContentType("text/plain"); } response.setHeader("Content-Disposition", "inline"); PrintWriter writer = response.getWriter(); InputStreamReader reader = null; try { if (charset != null) { reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation), charset); } else { reader = new InputStreamReader(servletContext.getResourceAsStream(finalLocation)); } if (reader == null) { _log.warn("resource at location [" + finalLocation + "] cannot be obtained (return null) from ServletContext !!! "); } else { char[] buffer = new char[BUFFER_SIZE]; int charRead = 0; while ((charRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charRead); } } } finally { if (reader != null) reader.close(); if (writer != null) { writer.flush(); writer.close(); } } }
From source file:org.apache.struts.tiles.xmlDefinition.I18nFactorySet.java
/** * Parse specified xml file and add definition to specified definitions set. * This method is used to load several description files in one instances list. * If filename exists and definition set is <code>null</code>, create a new set. Otherwise, return * passed definition set (can be <code>null</code>). * @param servletContext Current servlet context. Used to open file. * @param filename Name of file to parse. * @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions * set is created on request./*from w w w .ja v a 2 s . c o m*/ * @return XmlDefinitionsSet The definitions set created or passed as parameter. * @throws DefinitionsFactoryException On errors parsing file. */ protected XmlDefinitionsSet parseXmlFile(ServletContext servletContext, String filename, XmlDefinitionsSet xmlDefinitions) throws DefinitionsFactoryException { try { InputStream input = servletContext.getResourceAsStream(filename); // Try to load using real path. // This allow to load config file under websphere 3.5.x // Patch proposed Houston, Stephen (LIT) on 5 Apr 2002 if (null == input) { try { input = new java.io.FileInputStream(servletContext.getRealPath(filename)); } catch (Exception e) { } } // If the config isn't in the servlet context, try the class loader // which allows the config files to be stored in a jar if (input == null) { input = getClass().getResourceAsStream(filename); } // If still nothing found, this mean no config file is associated if (input == null) { if (log.isDebugEnabled()) { log.debug("Can't open file '" + filename + "'"); } return xmlDefinitions; } // Check if parser already exist. // Doesn't seem to work yet. //if( xmlParser == null ) if (true) { xmlParser = new XmlParser(); xmlParser.setValidating(isValidatingParser); } // Check if definition set already exist. if (xmlDefinitions == null) { xmlDefinitions = new XmlDefinitionsSet(); } xmlParser.parse(input, xmlDefinitions); } catch (SAXException ex) { if (log.isDebugEnabled()) { log.debug("Error while parsing file '" + filename + "'."); ex.printStackTrace(); } throw new DefinitionsFactoryException("Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } catch (IOException ex) { throw new DefinitionsFactoryException( "IO Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } return xmlDefinitions; }
From source file:org.ramadda.repository.server.RepositoryServlet.java
/** * Create the repository//from w w w. jav a 2 s . c o m * * @param request - an HttpServletRequest object that contains the request the client has made of the servlet * * @throws Exception - if an Exception occurs during the creation of the repository */ private void createRepository(HttpServletRequest request) throws Exception { Properties webAppProperties = new Properties(); ServletContext context = getServletContext(); if (context != null) { String propertyFile = "/WEB-INF/repository.properties"; InputStream is = context.getResourceAsStream(propertyFile); if (is != null) { webAppProperties.load(is); } } createRepository(request.getServerPort(), webAppProperties, true); }
From source file:org.apache.tiles.xmlDefinition.I18nFactorySet.java
/** * Parse specified xml file and add definition to specified definitions set. * This method is used to load several description files in one instances list. * If filename exists and definition set is <code>null</code>, create a new set. Otherwise, return * passed definition set (can be <code>null</code>). * @param servletContext Current servlet context. Used to open file. * @param filename Name of file to parse. * @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions * set is created on request.// w w w .j a va 2s . c o m * @return XmlDefinitionsSet The definitions set created or passed as parameter. * @throws DefinitionsFactoryException On errors parsing file. */ private XmlDefinitionsSet parseXmlFile(ServletContext servletContext, String filename, XmlDefinitionsSet xmlDefinitions) throws DefinitionsFactoryException { try { InputStream input = servletContext.getResourceAsStream(filename); // Try to load using real path. // This allow to load config file under websphere 3.5.x // Patch proposed Houston, Stephen (LIT) on 5 Apr 2002 if (null == input) { try { input = new java.io.FileInputStream(servletContext.getRealPath(filename)); } catch (Exception e) { } } // If still nothing found, this mean no config file is associated if (input == null) { if (log.isDebugEnabled()) { log.debug("Can't open file '" + filename + "'"); } return xmlDefinitions; } // Check if parser already exist. // Doesn't seem to work yet. //if( xmlParser == null ) if (true) { xmlParser = new XmlParser(); xmlParser.setValidating(isValidatingParser); } // Check if definition set already exist. if (xmlDefinitions == null) { xmlDefinitions = new XmlDefinitionsSet(); } xmlParser.parse(input, xmlDefinitions); } catch (SAXException ex) { if (log.isDebugEnabled()) { log.debug("Error while parsing file '" + filename + "'."); ex.printStackTrace(); } throw new DefinitionsFactoryException("Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } catch (IOException ex) { throw new DefinitionsFactoryException( "IO Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } return xmlDefinitions; }
From source file:org.apache.click.extras.control.MenuFactory.java
/** * Return a copy of the Applications root Menu as defined by the * configuration file./*w w w. j av a 2 s . c o m*/ * <p/> * If the fileName starts with a '/' character it is assumed to be an * absolute path and Click will attempt to load the file from the Servlet * context path and if not found from the classpath. * <p/> * If the fileName does not start with a '/' character it is assumed to be * a relative path and Click will load the file from the Servlet context * by <tt>prefixing</tt> the fileName with '/WEB-INF'. If not found the * file will be loaded from the classpath. * <p/> * The returned root menu is always selected. * * @param name the name of the root menu * @param fileName the configuration fileName defining the menu definitions * @param accessController the menu access controller * @param menuClass the menu class to instantiate * @return a copy of the application's root Menu */ protected Menu loadFromMenuXml(String name, String fileName, AccessController accessController, Class<? extends Menu> menuClass) { if (name == null) { throw new IllegalArgumentException("Null name parameter"); } if (fileName == null) { throw new IllegalArgumentException("Null fileName parameter"); } if (accessController == null) { throw new IllegalArgumentException("Null accessController parameter"); } String webinfFileName = null; boolean absolute = fileName.startsWith("/"); if (!absolute) { fileName = '/' + fileName; webinfFileName = "/WEB-INF" + fileName; } Context context = Context.getThreadLocalContext(); Menu menu = null; if (menuClass == null) { menu = new Menu(); } else { menu = createMenu(menuClass); } menu.setName(name); menu.setAccessController(accessController); ServletContext servletContext = context.getServletContext(); InputStream inputStream = null; if (absolute) { inputStream = servletContext.getResourceAsStream(fileName); } else { inputStream = servletContext.getResourceAsStream(webinfFileName); } if (inputStream == null) { if (absolute) { inputStream = ClickUtils.getResourceAsStream(fileName, MenuFactory.class); if (inputStream == null) { String msg = "could not find configuration file:" + fileName + " on classpath"; throw new RuntimeException(msg); } } else { inputStream = ClickUtils.getResourceAsStream(fileName, MenuFactory.class); if (inputStream == null) { String msg = "could not find configuration file:" + webinfFileName + " or " + fileName + " on classpath"; throw new RuntimeException(msg); } } } Document document = ClickUtils.buildDocument(inputStream); Element rootElm = document.getDocumentElement(); NodeList list = rootElm.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node instanceof Element) { Menu childMenu = buildMenu((Element) node, accessController, menuClass); menu.add(childMenu); } } return menu; }
From source file:com.ecyrd.jspwiki.ui.TemplateManager.java
/** * Check the existence of a template./*w ww .ja va 2 s.c o m*/ */ // FIXME: Does not work yet public boolean templateExists(String templateName) { ServletContext context = m_engine.getServletContext(); InputStream in = context.getResourceAsStream(getPath(templateName) + "ViewTemplate.jsp"); if (in != null) { try { in.close(); } catch (IOException e) { } return true; } return false; }
From source file:de.kp.ames.web.function.security.SecurityServiceImpl.java
/** * A helper method to retrieve a set of default AND * user independent set of application descriptions * //w ww.j a v a 2 s . co m * @param ctx * @return * @throws Exception */ private JSONArray getDefaultApps(RequestContext ctx) throws Exception { ServletContext context = ctx.getContext(); String filename = "/WEB-INF/classes/resources/defaultapps.xml"; InputStream is = context.getResourceAsStream(filename); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); JSONArray jDefaultApps = new JSONArray(); try { Document xmlDoc = factory.newDocumentBuilder().parse(is); NodeList apps = xmlDoc.getElementsByTagName("app"); for (int i = 0; i < apps.getLength(); i++) { Element eService = (Element) apps.item(i); JSONObject jApp = getAppParams(eService); if (jApp == null) continue; jDefaultApps.put(jApp); } } catch (Exception e) { e.printStackTrace(); } return jDefaultApps; }
From source file:com.liferay.portal.struts.PortalTilesDefinitionsFactory.java
/** * Parse specified xml file and add definition to specified definitions set. * This method is used to load several description files in one instances list. * If filename exists and definition set is <code>null</code>, create a new set. Otherwise, return * passed definition set (can be <code>null</code>). * @param servletContext Current servlet context. Used to open file. * @param filename Name of file to parse. * @param xmlDefinitions Definitions set to which definitions will be added. If null, a definitions * set is created on request./*from w w w .j a v a 2 s . c o m*/ * @return XmlDefinitionsSet The definitions set created or passed as parameter. * @throws DefinitionsFactoryException On errors parsing file. */ private XmlDefinitionsSet parseXmlFile(ServletContext servletContext, String filename, XmlDefinitionsSet xmlDefinitions) throws DefinitionsFactoryException { try { InputStream input = servletContext.getResourceAsStream(filename); // Try to load using real path. // This allow to load config file under websphere 3.5.x // Patch proposed Houston, Stephen (LIT) on 5 Apr 2002 if (null == input) { try { input = new java.io.FileInputStream(servletContext.getRealPath(filename)); } catch (Exception e) { } } // If the config isn't in the servlet context, try the class loader // which allows the config files to be stored in a jar if (input == null) { input = getClass().getResourceAsStream(filename); } // If still nothing found, this mean no config file is associated if (input == null) { if (log.isDebugEnabled()) { log.debug("Can't open file '" + filename + "'"); } return xmlDefinitions; } // Check if parser already exist. // Doesn't seem to work yet. //if( xmlParser == null ) if (true) { xmlParser = new XmlParser(); xmlParser.setValidating(isValidatingParser); } // Check if definition set already exist. if (xmlDefinitions == null) { xmlDefinitions = new XmlDefinitionsSet(); } xmlParser.parse(input, xmlDefinitions); } catch (SAXException ex) { if (log.isDebugEnabled()) { log.debug("Error while parsing file '" + filename + "'."); ex.printStackTrace(); } throw new DefinitionsFactoryException("Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } catch (IOException ex) { throw new DefinitionsFactoryException( "IO Error while parsing file '" + filename + "'. " + ex.getMessage(), ex); } return xmlDefinitions; }