List of usage examples for javax.servlet ServletContext getResourceAsStream
public InputStream getResourceAsStream(String path);
InputStream
object. From source file:com.concursive.connect.web.modules.setup.utils.SetupUtils.java
/** * Inserts the default database data// w ww .j a v a 2 s. c om * * @param db * @param context * @param setupPath * @throws Exception */ public static void insertDefaultData(Connection db, ServletContext context, String setupPath) throws Exception { // Prepare the BSH interpreter Interpreter script = new Interpreter(); script.set("db", db); // Default database inserts InputStream source = context.getResourceAsStream(setupPath + "common" + fs + "install.bsh"); BufferedReader in = new BufferedReader(new InputStreamReader(source)); script.eval(in); in.close(); // Module Defaults String initPath = setupPath + "init" + fs; DatabaseUtils.executeSQL(db, context.getResourceAsStream(initPath + "project.sql"), true); DatabaseUtils.executeSQL(db, context.getResourceAsStream(initPath + "task.sql"), true); DatabaseUtils.executeSQL(db, context.getResourceAsStream(initPath + "ticket.sql"), true); // Bring the version database up-to-date with the upgrade utility ArrayList<String> versionList = UpgradeUtils .retrieveDatabaseVersions(context.getResourceAsStream(setupPath + "database_versions.txt")); for (String version : versionList) { if (version.length() == 10) { UpgradeUtils.addVersion(db, version); } } }
From source file:org.apache.wiki.util.PropertyReader.java
/** * Locate a resource stored in the class path. Try first with "WEB-INF/classes" * from the web app and fallback to "resourceName". * * @param context the servlet context/*w w w . j av a 2 s . co m*/ * @param resourceName the name of the resource * @return the input stream of the resource or <b>null</b> if the resource was not found */ public static InputStream locateClassPathResource(ServletContext context, String resourceName) { InputStream result; String currResourceLocation; // garbage in - garbage out if (StringUtils.isEmpty(resourceName)) { return null; } // try with web app class loader searching in "WEB-INF/classes" currResourceLocation = createResourceLocation("/WEB-INF/classes", resourceName); result = context.getResourceAsStream(currResourceLocation); if (result != null) { LOG.debug(" Successfully located the following classpath resource : " + currResourceLocation); return result; } // if not found - try with the current class loader and the given name currResourceLocation = createResourceLocation("", resourceName); result = PropertyReader.class.getResourceAsStream(currResourceLocation); if (result != null) { LOG.debug(" Successfully located the following classpath resource : " + currResourceLocation); return result; } LOG.debug(" Unable to resolve the following classpath resource : " + resourceName); return result; }
From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java
public static DOMSource getDocumentSource(ServletContext context, String path) { if (log.isDebugEnabled()) { log.debug("readDocument(String " + path + ")"); }/* ww w.j ava2 s. co m*/ InputStream inputStream = context.getResourceAsStream(path); String realPath = null; try { realPath = context.getResource(path).toString(); } catch (MalformedURLException e1) { log.debug(e1.getMessage(), e1); } DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); Document document = null; DOMSource source = null; try { setDocumentBuilderFactoryFeatures(builderFactory); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); document = documentBuilder.parse(inputStream); source = new DOMSource(document, realPath); } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } return source; }
From source file:com.ecyrd.jspwiki.ui.TemplateManager.java
/** * Tries to locate a given resource from the template directory. If the * given resource is not found under the current name, returns the * path to the corresponding one in the default template. * * @param sContext The servlet context//from ww w . jav a2 s . c o m * @param name The name of the resource * @return The name of the resource which was found. */ private static String findResource(ServletContext sContext, String name) { InputStream is = sContext.getResourceAsStream(name); if (is == null) { String defname = makeFullJSPName(DEFAULT_TEMPLATE, removeTemplatePart(name)); is = sContext.getResourceAsStream(defname); if (is != null) name = defname; else name = null; } if (is != null) { try { is.close(); } catch (IOException e) { } } return name; }
From source file:org.nuxeo.ecm.core.opencmis.impl.server.NuxeoObjectData.java
/** * @deprecated since 7.3. The thumbnail is now a default rendition, see NXP-16662. *//*from w ww. j a v a 2s . c o m*/ @Deprecated public static InputStream getIconStream(String iconPath, CallContext context) throws ClientException { if (iconPath == null || iconPath.length() == 0) { return null; } if (!iconPath.startsWith("/")) { iconPath = '/' + iconPath; } ServletContext servletContext = (ServletContext) context.get(CallContext.SERVLET_CONTEXT); if (servletContext == null) { throw new CmisRuntimeException("Cannot get servlet context"); } return servletContext.getResourceAsStream(iconPath); }
From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java
/** * Read document from ServletContext/context path * * @param context ServletContext//from w w w . j av a2s .co m * @param path path * * @return Document */ public static Document readDocument(ServletContext context, String path) { if (log.isDebugEnabled()) { log.debug("readDocument(String " + path + ")"); } Document document = null; InputStream inputStream = context.getResourceAsStream(path); String fullpath = context.getRealPath(path); log.debug("readDocument(full path) " + fullpath + ")"); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); try { setDocumentBuilderFactoryFeatures(builderFactory); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); document = documentBuilder.parse(inputStream); } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } return document; }
From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java
/** * Executes SQL scripts// w w w . j a v a2 s .com * * @param context the web context to use for finding the file resource * @param db the database connection to use for executing the script against * @param baseName The resource filename * @throws SQLException Description of the Exception */ private static void upgradeSQL(ServletContext context, Connection db, String baseName) throws Exception { String dbType = null; switch (DatabaseUtils.getType(db)) { case DatabaseUtils.POSTGRESQL: dbType = "postgresql"; break; case DatabaseUtils.MSSQL: dbType = "mssql"; break; default: throw new Exception("Upgrade-> * Database could not be determined: " + DatabaseUtils.getType(db)); } String pathString = baseName.substring(0, 4); LOG.info("Executing " + dbType + " script: " + baseName); DatabaseUtils.executeSQL(db, context.getResourceAsStream( "/WEB-INF/database/" + dbType + "/upgrade/" + pathString + "/" + baseName), true); }
From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java
/** * Executes BSH scripts//from ww w . j a va2 s . c o m * * @param context the web context to use for finding the file resource * @param db the database connection to use for executing the script against * @param scriptName The resource filename * @throws Exception Description of the Exception */ private static void upgradeBSH(ServletContext context, Connection db, String scriptName) throws Exception { LOG.info("Executing BeanShell script " + scriptName); // Prepare bean shell script, if needed Interpreter script = new Interpreter(); script.set("db", db); // Add the ApplicationPrefs... ApplicationPrefs prefs = (ApplicationPrefs) context.getAttribute(Constants.APPLICATION_PREFS); script.set("prefs", prefs); // Read the script String pathString = scriptName.substring(0, 4); String setupPath = "/WEB-INF/database/common/" + pathString + "/"; InputStream source = context.getResourceAsStream(setupPath + scriptName); BufferedReader in = new BufferedReader(new InputStreamReader(source)); // Execute the script script.eval(in); in.close(); }
From source file:com.assignmentone.snippet.ShowCodeSnippet.java
private static String readFileByLines(ServletContext servletContext, String fileName, String startMark, String endMark) {/*ww w . java2s. c o m*/ String filePath = ""; InputStream inputStream = null; BufferedReader reader = null; try { // read the file if (fileName.endsWith(".java")) { String source_location = System.getProperty(VM_ARG); if (source_location != null) { filePath = source_location + JAVA_PACKAGE + fileName; inputStream = new FileInputStream(filePath); } else { filePath = "/WEB-INF/src" + JAVA_PACKAGE + fileName; inputStream = servletContext.getResourceAsStream(filePath); } } else { inputStream = servletContext.getResourceAsStream(fileName); } if (inputStream == null) { return null; } // find the line that has the mark reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String line = null; String contents = ""; int markStart = -1; while ((line = reader.readLine()) != null) { if (line.contains(endMark)) { break; } if (markStart >= 0 && !line.contains(SHOW_MARK)) { if (line.length() <= markStart) { line = ""; } else { line = line.substring(markStart); } contents = contents + line + "\n"; } if (line.contains(startMark)) { String trim = line.trim(); markStart = line.indexOf(trim); } } return contents; } catch (IOException e) { return null; // close } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e1) { } } } }
From source file:org.sakaiproject.tool.help.RestContentProvider.java
/** * synchronize initialization of caching XSL * @param context//from ww w.java2 s.co m */ public static void initializeXsl(ServletContext context) { if (LOG.isDebugEnabled()) { LOG.debug("initializeXsl(ServletContext " + context + ")"); } if (XSL_INITIALIZED.booleanValue()) { return; } else { synchronized (XSL_INITIALIZED_LOCK) { if (!XSL_INITIALIZED.booleanValue()) { //read in and parse xsl InputStream iStreamPreprocess = null; InputStream iStreamAllInOne = null; try { iStreamPreprocess = context.getResourceAsStream(XML_PREPROCESS_XSL); iStreamAllInOne = context.getResourceAsStream(XML_KB_XSL); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); xslDocumentPreprocess = documentBuilder.parse(iStreamPreprocess); xslDocumentAllInOne = documentBuilder.parse(iStreamAllInOne); } catch (ParserConfigurationException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } catch (SAXException e) { LOG.error(e.getMessage(), e); } try { iStreamPreprocess.close(); iStreamAllInOne.close(); } catch (IOException e) { LOG.error(e.getMessage(), e); } XSL_INITIALIZED = Boolean.TRUE; } } } }