List of usage examples for javax.servlet ServletContext getResource
public URL getResource(String path) throws MalformedURLException;
From source file:org.openlegacy.web.tags.TagUtils.java
public static boolean fileExists(ServletContext application, String file) { try {/*from ww w . j a v a2 s . co m*/ return application.getResource(file) != null; } catch (MalformedURLException e) { return false; } }
From source file:org.openmrs.web.Listener.java
/** * Hacky way to get the current contextPath. This will usually be "openmrs". This method will be * obsolete when servlet api ~2.6 comes out...at which point a call like * servletContext.getContextRoot() would be sufficient * * @return current contextPath of this webapp without initial slash *//*from w w w . j av a2 s . c o m*/ private String getContextPath(ServletContext servletContext) { // Get the context path without the request. String contextPath = ""; try { contextPath = servletContext.getContextPath(); } catch (NoSuchMethodError ex) { // ServletContext.getContextPath() was added in version 2.5 of the Servlet API. Tomcat 5.5 // has version 2.4 of the servlet API, so we fall back to the hacky code we were previously // using try { String path = servletContext.getResource("/").getPath(); contextPath = path.substring(0, path.lastIndexOf("/")); contextPath = contextPath.substring(contextPath.lastIndexOf("/")); } catch (Exception e) { log.error(e); } } catch (Exception e) { log.error(e); } // trim off initial slash if it exists if (contextPath.indexOf("/") != -1) { contextPath = contextPath.substring(1); } return contextPath; }
From source file:org.ops4j.pax.web.service.internal.WelcomeFilesFilter.java
/** * Serves the welcome files if request path ends with "/". * * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) *//*from w w w. j av a2s . c o m*/ public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { LOG.debug("Apply welcome files filter..."); if (m_welcomeFiles.length > 0 && request instanceof HttpServletRequest) { String servletPath = (((HttpServletRequest) request).getServletPath()); String pathInfo = ((HttpServletRequest) request).getPathInfo(); LOG.debug("Servlet path: " + servletPath); LOG.debug("Path info: " + pathInfo); if ((pathInfo != null && pathInfo.endsWith("/")) || (servletPath != null && servletPath.endsWith("/"))) { final ServletContext servletContext = m_filterConfig.getServletContext(); for (String welcomeFile : m_welcomeFiles) { final String welcomePath = addPaths(servletPath, addPaths(pathInfo, welcomeFile)); final URL welcomeFileUrl = servletContext.getResource(welcomePath); if (welcomeFileUrl != null) { if (m_redirect && response instanceof HttpServletResponse) { ((HttpServletResponse) response).sendRedirect(welcomeFile); return; } else { final RequestDispatcher requestDispatcher = request.getRequestDispatcher(welcomePath); if (requestDispatcher != null) { requestDispatcher.forward(request, response); return; } } } } } } else { if (m_welcomeFiles.length == 0) { LOG.debug("Welcome filter not applied as there are no welcome files configured."); } if (!(request instanceof HttpServletRequest)) { LOG.debug("Welcome filter not applied as the request is not an " + HttpServletRequest.class.getSimpleName()); } } // if we are here means that the request was not handled by welcome files filter so, go on chain.doFilter(request, response); }
From source file:org.sakaiproject.portal.render.portlet.PortletToolRenderService.java
private boolean isPortletApplication(ServletContext context, ToolConfiguration configuration) throws ToolRenderException, MalformedURLException { SakaiPortletWindow window = registry.getOrCreatePortletWindow(configuration); if (window == null) { return false; }/*from w w w.ja va2s . co m*/ if (LOG.isDebugEnabled()) { LOG.debug("Checking context for potential portlet "); } ServletContext crossContext = context.getContext(window.getContextPath()); if (LOG.isDebugEnabled()) { LOG.debug("Got servlet context as " + crossContext); LOG.debug("Getting Context for path " + window.getContextPath()); LOG.debug("Base Path " + crossContext.getRealPath("/")); LOG.debug("Context Name " + crossContext.getServletContextName()); LOG.debug("Server Info " + crossContext.getServerInfo()); LOG.debug(" and it is a portlet ? :" + (crossContext.getResource("/WEB-INF/portlet.xml") != null)); } return crossContext.getResource("/WEB-INF/portlet.xml") != null; }
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 + ")"); }/*w w w . j av a2 s . c o 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:org.seasar.mayaa.impl.source.ApplicationResourceSourceDescriptor.java
protected URL getURL() { if (_url == null) { String path = _systemID;//from ww w .j a va2 s . c o m if (StringUtil.isEmpty(path)) { path = "/"; } // TODO ???ServletContext??????? ServletContext context = (ServletContext) getApplicationScope().getUnderlyingContext(); try { // TODO GAE for Java???null?????????... _url = context.getResource(path); if (_url == null) { LOG.debug("NG. getResource failed. - " + path); } else { LOG.debug("OK. getResource url succeed. - " + path); } return _url; } catch (MalformedURLException e) { throw new IllegalStateException("invalid: " + path); } } return _url; }
From source file:org.seasar.mayaa.impl.source.ApplicationSourceDescriptor.java
protected URL getURL() { String path = _fileSourceDescriptor.getRealPath(); if (StringUtil.isEmpty(path)) { path = "/"; }// ww w . j av a2 s . c o m // TODO ???ServletContext??????? ServletContext context = (ServletContext) getApplicationScope().getUnderlyingContext(); try { URL url = context.getResource(path); return url; } catch (MalformedURLException e) { throw new IllegalStateException("invalid: " + path); } }
From source file:org.sonar.server.platform.web.WebPagesFilter.java
private static String readIndexFile(ServletContext servletContext) { try {//from ww w.jav a2 s. co m return IOUtils.toString(requireNonNull(servletContext.getResource("/index.html")), UTF_8); } catch (Exception e) { throw new IllegalStateException("Fail to provide index file", e); } }
From source file:org.squale.welcom.struts.webServer.URLManager.java
/** * Retoune le webFile dans le repertoire webcontent * //from w w w. j a v a2 s. c o m * @param context context * @param pUrl url * @return le webfile */ public WebFile getWebFileFromContext(ServletContext context, String pUrl) { WebFile webFile = new WebFile(WebFile.TYPE_SERVLET); // Compatibilit Tomcat !! try { webFile.setUrl(context.getResource(pUrl)); } catch (final Exception e) { try { // Test compatiblite Tomcat (Necessite un /) webFile.setUrl(context.getResource("/" + pUrl)); } catch (final Exception ee) { // Recherche si la resource demand ne se trouve pas dans le jar Welcom } } if (webFile.getUrl() != null) { // recherche la date final String file = context.getRealPath(sUrl); final File f = new File(file); webFile.setLastDate(new Date(f.lastModified())); return webFile; } else { return null; } }
From source file:org.theospi.portfolio.portal.web.XsltPortal.java
protected void processCategoryFiles(ToolCategory toolCategory, ServletContext context) throws IOException { File base = new File(categoryBasePath); String parentPath = base.getParent(); Set paths = context.getResourcePaths(parentPath); for (Iterator<String> i = paths.iterator(); i.hasNext();) { String path = i.next();/*from w ww . j av a 2s.c o m*/ if (path.startsWith(categoryBasePath)) { path += toolCategory.getHomePagePath(); if (context.getResource(path) != null) { processCategoryFile(toolCategory, path, context.getResourceAsStream(path)); } } } }