ServletContextUriLocator extends WildcardUriLocatorSupport { public InputStream locate(final String uri) throws IOException { notNull(uri, "URI cannot be NULL!"); LOG.debug("locate resource: {}", uri); try { if (getWildcardStreamLocator().hasWildcard(uri)) { final ServletContext servletContext = Context.get().getServletContext(); final String fullPath = FilenameUtils.getFullPath(uri); final String realPath = servletContext.getRealPath(fullPath); if (realPath == null) { final String message = "[FAIL] determine realPath for resource: " + uri; LOG.debug(message); throw new IOException(message); } return getWildcardStreamLocator().locateStream(uri, new File(URLDecoder.decode(realPath, StandardCharsets.UTF_8.name()))); } } catch (final IOException e) { if (e instanceof NoMoreAttemptsIOException) { throw e; } LOG.debug("[FAIL] localize the stream containing wildcard. Original error message: '{}'", e.getMessage() + "\".\n Trying to locate the stream without the wildcard."); } InputStream inputStream = null; try { switch (locatorStrategy) { case DISPATCHER_FIRST: inputStream = dispatcherFirstStreamLocator(uri); break; case SERVLET_CONTEXT_FIRST: inputStream = servletContextFirstStreamLocator(uri); break; case SERVLET_CONTEXT_ONLY: inputStream = servletContextBasedStreamLocator(uri); break; } validateInputStreamIsNotNull(inputStream, uri); return inputStream; } catch (final IOException e) { LOG.debug("Wrong or empty resource with location: {}", uri); throw e; } }  ServletContextUriLocator setLocatorStrategy(final LocatorStrategy locatorStrategy); boolean accept(final String uri); static boolean isValid(final String uri); static boolean isProtectedResource(final String uri); InputStream locate(final String uri); LocatorStrategy getLocatorStrategy(); @Override String toString(); static final String ALIAS; static final String ALIAS_DISPATCHER_FIRST; static final String ALIAS_SERVLET_CONTEXT_FIRST; static final String ALIAS_SERVLET_CONTEXT_ONLY; static final String PREFIX; }
@Test(expected = NullPointerException.class) public void cannotAcceptNullUri() throws Exception { victim.locate(null); }
@Test public void testWildcard1Resources() throws IOException { victim.locate(createUri("/css/*.css")); }
@Test public void testWildcard2Resources() throws IOException { victim.locate(createUri("/css/*.cs?")); }
@Test public void testWildcard3Resources() throws IOException { victim.locate(createUri("/css/*.???")); }
@Test public void testRecursiveWildcardResources() throws IOException { victim.locate(createUri("/css/**.css")); }
@Test public void shouldFindWildcardResourcesForFolderContainingSpaces() throws IOException { victim.locate(createUri("/folder with spaces/**.css", "test")); }
@Test(expected = IOException.class) public void testWildcardInexistentResources() throws IOException { victim.locate(createUri("/css/**.NOTEXIST")); }
@Test(expected = IOException.class) public void testInvalidUrl() throws Exception { victim.locate("/invalid/resource.css"); }
@Test public void shouldInjectEachLocatorWhenLocatorFactoryIsUsed() throws Exception { final SimpleUriLocatorFactory locatorFactory = new SimpleUriLocatorFactory(); final ServletContextUriLocator locator = new ServletContextUriLocator(); locatorFactory.addLocator(locator); final DispatcherStreamLocator dispatcherLocator = Mockito.mock(DispatcherStreamLocator.class); when( dispatcherLocator.getInputStream(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class), Mockito.anyString())).thenReturn(WroUtil.EMPTY_STREAM); final Injector injector = InjectorBuilder.create(new BaseWroManagerFactory().setUriLocatorFactory(locatorFactory)).setDispatcherLocator( dispatcherLocator).build(); final LocatorFactoryHolder locatorFactoryHolder = new LocatorFactoryHolder(); injector.inject(locatorFactoryHolder); final String uri = "/style.css"; locatorFactoryHolder.locatorFactory.locate(uri); }