List of usage examples for javax.servlet ServletContext getResource
public URL getResource(String path) throws MalformedURLException;
From source file:org.tinygroup.jspengine.compiler.JspConfig.java
private void processWebDotXml(ServletContext ctxt) throws JasperException { InputStream is = null;/* w ww .ja v a2 s . co m*/ try { URL uri = ctxt.getResource(WEB_XML); if (uri == null) { // no web.xml return; } is = uri.openStream(); InputSource ip = new InputSource(is); ip.setSystemId(uri.toExternalForm()); ParserUtils pu = new ParserUtils(); /* SJSAS 6384538 TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip); */ // START SJSAS 6384538 TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip, options.isValidationEnabled()); // END SJSAS 6384538 if (webApp == null || webApp.findAttribute("version") == null || Double.valueOf(webApp.findAttribute("version")).doubleValue() < 2.4) { defaultIsELIgnored = "true"; return; } TreeNode jspConfig = webApp.findChild("jsp-config"); if (jspConfig == null) { return; } jspProperties = new Vector(); Iterator jspPropertyList = jspConfig.findChildren("jsp-property-group"); while (jspPropertyList.hasNext()) { TreeNode element = (TreeNode) jspPropertyList.next(); Iterator list = element.findChildren(); Vector urlPatterns = new Vector(); String pageEncoding = null; String scriptingInvalid = null; String elIgnored = null; String isXml = null; Vector includePrelude = new Vector(); Vector includeCoda = new Vector(); String trimSpaces = null; String poundAllowed = null; while (list.hasNext()) { element = (TreeNode) list.next(); String tname = element.getName(); if ("url-pattern".equals(tname)) urlPatterns.addElement(element.getBody()); else if ("page-encoding".equals(tname)) pageEncoding = element.getBody(); else if ("is-xml".equals(tname)) isXml = element.getBody(); else if ("el-ignored".equals(tname)) elIgnored = element.getBody(); else if ("scripting-invalid".equals(tname)) scriptingInvalid = element.getBody(); else if ("include-prelude".equals(tname)) includePrelude.addElement(element.getBody()); else if ("include-coda".equals(tname)) includeCoda.addElement(element.getBody()); else if ("trim-directive-whitespaces".equals(tname)) trimSpaces = element.getBody(); else if ("deferred-syntax-allowed-as-literal".equals(tname)) poundAllowed = element.getBody(); } if (urlPatterns.size() == 0) { continue; } makeJspPropertyGroups(jspProperties, urlPatterns, isXml, elIgnored, scriptingInvalid, trimSpaces, poundAllowed, pageEncoding, includePrelude, includeCoda); } } catch (Exception ex) { throw new JasperException(ex); } finally { if (is != null) { try { is.close(); } catch (Throwable t) { } } } }
From source file:org.tobarsegais.webapp.ServletContextListenerImpl.java
public void contextInitialized(ServletContextEvent sce) { ServletContext application = sce.getServletContext(); Map<String, String> bundles = new HashMap<String, String>(); Map<String, Toc> contents = new LinkedHashMap<String, Toc>(); List<IndexEntry> keywords = new ArrayList<IndexEntry>(); Directory index = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(LUCENE_VERSON); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(LUCENE_VERSON, analyzer); IndexWriter indexWriter;//from ww w . ja v a 2s. c om try { indexWriter = new IndexWriter(index, indexWriterConfig); } catch (IOException e) { application.log("Cannot create search index. Search will be unavailable.", e); indexWriter = null; } for (String path : (Set<String>) application.getResourcePaths(BUNDLE_PATH)) { if (path.endsWith(".jar")) { String key = path.substring("/WEB-INF/bundles/".length(), path.lastIndexOf(".jar")); application.log("Parsing " + path); URLConnection connection = null; try { URL url = new URL("jar:" + application.getResource(path) + "!/"); connection = url.openConnection(); if (!(connection instanceof JarURLConnection)) { application.log(path + " is not a jar file, ignoring"); continue; } JarURLConnection jarConnection = (JarURLConnection) connection; JarFile jarFile = jarConnection.getJarFile(); Manifest manifest = jarFile.getManifest(); if (manifest != null) { String symbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName"); if (symbolicName != null) { int i = symbolicName.indexOf(';'); if (i != -1) { symbolicName = symbolicName.substring(0, i); } bundles.put(symbolicName, key); key = symbolicName; } } JarEntry pluginEntry = jarFile.getJarEntry("plugin.xml"); if (pluginEntry == null) { application.log(path + " does not contain a plugin.xml file, ignoring"); continue; } Plugin plugin = Plugin.read(jarFile.getInputStream(pluginEntry)); Extension tocExtension = plugin.getExtension("org.eclipse.help.toc"); if (tocExtension == null || tocExtension.getFile("toc") == null) { application.log(path + " does not contain a 'org.eclipse.help.toc' extension, ignoring"); continue; } JarEntry tocEntry = jarFile.getJarEntry(tocExtension.getFile("toc")); if (tocEntry == null) { application.log(path + " is missing the referenced toc: " + tocExtension.getFile("toc") + ", ignoring"); continue; } Toc toc; try { toc = Toc.read(jarFile.getInputStream(tocEntry)); } catch (IllegalStateException e) { application.log("Could not parse " + path + " due to " + e.getMessage(), e); continue; } contents.put(key, toc); Extension indexExtension = plugin.getExtension("org.eclipse.help.index"); if (indexExtension != null && indexExtension.getFile("index") != null) { JarEntry indexEntry = jarFile.getJarEntry(indexExtension.getFile("index")); if (indexEntry != null) { try { keywords.addAll(Index.read(key, jarFile.getInputStream(indexEntry)).getChildren()); } catch (IllegalStateException e) { application.log("Could not parse " + path + " due to " + e.getMessage(), e); } } else { application.log( path + " is missing the referenced index: " + indexExtension.getFile("index")); } } application.log(path + " successfully parsed and added as " + key); if (indexWriter != null) { application.log("Indexing content of " + path); Set<String> files = new HashSet<String>(); Stack<Iterator<? extends TocEntry>> stack = new Stack<Iterator<? extends TocEntry>>(); stack.push(Collections.singleton(toc).iterator()); while (!stack.empty()) { Iterator<? extends TocEntry> cur = stack.pop(); if (cur.hasNext()) { TocEntry entry = cur.next(); stack.push(cur); if (!entry.getChildren().isEmpty()) { stack.push(entry.getChildren().iterator()); } String file = entry.getHref(); if (file == null) { continue; } int hashIndex = file.indexOf('#'); if (hashIndex != -1) { file = file.substring(0, hashIndex); } if (files.contains(file)) { // already indexed // todo work out whether to just pull the section continue; } Document document = new Document(); document.add(new Field("title", entry.getLabel(), Field.Store.YES, Field.Index.ANALYZED)); document.add(new Field("href", key + "/" + entry.getHref(), Field.Store.YES, Field.Index.NO)); JarEntry docEntry = jarFile.getJarEntry(file); if (docEntry == null) { // ignore missing file continue; } InputStream inputStream = null; try { inputStream = jarFile.getInputStream(docEntry); org.jsoup.nodes.Document docDoc = Jsoup.parse(IOUtils.toString(inputStream)); document.add(new Field("contents", docDoc.body().text(), Field.Store.NO, Field.Index.ANALYZED)); indexWriter.addDocument(document); } finally { IOUtils.closeQuietly(inputStream); } } } } } catch (XMLStreamException e) { application.log("Could not parse " + path + " due to " + e.getMessage(), e); } catch (MalformedURLException e) { application.log("Could not parse " + path + " due to " + e.getMessage(), e); } catch (IOException e) { application.log("Could not parse " + path + " due to " + e.getMessage(), e); } finally { if (connection instanceof HttpURLConnection) { // should never be the case, but we should try to be sure ((HttpURLConnection) connection).disconnect(); } } } } if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { application.log("Cannot create search index. Search will be unavailable.", e); } application.setAttribute("index", index); } application.setAttribute("toc", Collections.unmodifiableMap(contents)); application.setAttribute("keywords", new Index(keywords)); application.setAttribute("bundles", Collections.unmodifiableMap(bundles)); application.setAttribute("analyzer", analyzer); application.setAttribute("contentsQueryParser", new QueryParser(LUCENE_VERSON, "contents", analyzer)); }
From source file:org.vulpe.controller.filter.VulpeFilterDispatcher.java
/** * Initiate URL Rewrite Filter./* w w w . j ava 2 s . co m*/ * * @param filterConfig * @throws ServletException */ private void initURLRewrite(final FilterConfig filterConfig) throws ServletException { URL_REWRITE_FILTER.init(filterConfig); final ServletContext context = filterConfig.getServletContext(); String confPath = filterConfig.getInitParameter("confPath"); if (StringUtils.isEmpty(confPath)) { confPath = UrlRewriteFilter.DEFAULT_WEB_CONF_PATH; } URL confUrl = null; try { confUrl = context.getResource(confPath); } catch (MalformedURLException e) { LOG.debug(e.getMessage()); } String confUrlStr = null; if (confUrl != null) { confUrlStr = confUrl.toString(); } final InputStream inputStream = context.getResourceAsStream(confPath); final Conf conf = new Conf(context, inputStream, confPath, confUrlStr, false); URL_REWRITER = new UrlRewriter(conf); }
From source file:org.xwiki.environment.internal.ServletEnvironmentTest.java
@Test public void getResourceWhenMalformedURLException() throws Exception { ServletContext servletContext = mock(ServletContext.class); when(servletContext.getResource("bad resource")).thenThrow(new MalformedURLException("invalid url")); this.environment.setServletContext(servletContext); assertNull(this.environment.getResource("bad resource")); assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]", this.logRule.getMessage(0)); }
From source file:org.zoxweb.server.http.servlet.HTTPServletUtil.java
public static String inputStreamToString(HttpServlet servlet, String resource) throws NullPointerException, IOException { log.info("resouce:" + resource); String content = null;/*w ww . j av a 2s. com*/ try { content = (IOUtil.inputStreamToString(servlet.getClass().getResourceAsStream(resource), true)); } catch (Exception e) { } if (content == null) { ServletContext context = servlet.getServletContext(); URL url = context.getResource(resource); log.info("url:" + url); content = (IOUtil.inputStreamToString(context.getResourceAsStream(resource), true)); } return content; }