protected AbstractInitializrController(InitializrMetadataProvider metadataProvider, ResourceUrlProvider resourceUrlProvider) { this.metadataProvider = metadataProvider; this.linkTo = (link) -> { String result = resourceUrlProvider.getForLookupPath(link); return (result != null) ? result : link; }; }
@Nullable public String resolveUrlPath(String url) { if (this.resourceUrlProvider == null) { logger.trace("ResourceUrlProvider not available via request attribute " + "ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR"); return null; } if (this.indexLookupPath != null && url.startsWith(this.prefixLookupPath)) { int suffixIndex = getEndPathIndex(url); String suffix = url.substring(suffixIndex); String lookupPath = url.substring(this.indexLookupPath, suffixIndex); lookupPath = this.resourceUrlProvider.getForLookupPath(lookupPath); if (lookupPath != null) { return this.prefixLookupPath + lookupPath + suffix; } } return null; }
/** * A variation on {@link #getForLookupPath(String)} that accepts a full request * URL path (i.e. including context and servlet path) and returns the full request * URL path to expose for public use. * @param request the current request * @param requestUrl the request URL path to resolve * @return the resolved public URL path, or {@code null} if unresolved */ @Nullable public final String getForRequestUrl(HttpServletRequest request, String requestUrl) { int prefixIndex = getLookupPathIndex(request); int suffixIndex = getEndPathIndex(requestUrl); if (prefixIndex >= suffixIndex) { return null; } String prefix = requestUrl.substring(0, prefixIndex); String suffix = requestUrl.substring(suffixIndex); String lookupPath = requestUrl.substring(prefixIndex, suffixIndex); String resolvedLookupPath = getForLookupPath(lookupPath); return (resolvedLookupPath != null ? prefix + resolvedLookupPath + suffix : null); }
@Nullable public String resolveUrlPath(String url) { if (this.resourceUrlProvider == null) { logger.trace("ResourceUrlProvider not available via request attribute " + "ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR"); return null; } if (this.indexLookupPath != null && url.startsWith(this.prefixLookupPath)) { int suffixIndex = getEndPathIndex(url); String suffix = url.substring(suffixIndex); String lookupPath = url.substring(this.indexLookupPath, suffixIndex); lookupPath = this.resourceUrlProvider.getForLookupPath(lookupPath); if (lookupPath != null) { return this.prefixLookupPath + lookupPath + suffix; } } return null; }
/** * A variation on {@link #getForLookupPath(String)} that accepts a full request * URL path (i.e. including context and servlet path) and returns the full request * URL path to expose for public use. * @param request the current request * @param requestUrl the request URL path to resolve * @return the resolved public URL path, or {@code null} if unresolved */ @Nullable public final String getForRequestUrl(HttpServletRequest request, String requestUrl) { int prefixIndex = getLookupPathIndex(request); int suffixIndex = getEndPathIndex(requestUrl); if (prefixIndex >= suffixIndex) { return null; } String prefix = requestUrl.substring(0, prefixIndex); String suffix = requestUrl.substring(suffixIndex); String lookupPath = requestUrl.substring(prefixIndex, suffixIndex); String resolvedLookupPath = getForLookupPath(lookupPath); return (resolvedLookupPath != null ? prefix + resolvedLookupPath + suffix : null); }
@Test public void getStaticResourceUrl() { String url = this.urlProvider.getForLookupPath("/resources/foo.css"); assertEquals("/resources/foo.css", url); }
@Test // SPR-16296 public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes() { // given ResourceResolver mockResourceResolver = mock(ResourceResolver.class); when(mockResourceResolver.resolveUrlPath(any(), any(), any())).thenReturn("some-path"); ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); handler.getResourceResolvers().add(mockResourceResolver); ResourceUrlProvider provider = new ResourceUrlProvider(); provider.getHandlerMap().put("/some-pattern/**", handler); // when String lookupForPath = provider.getForLookupPath("/some-pattern/some-lib//some-resource"); // then assertEquals("/some-pattern/some-path", lookupForPath); }
@Test public void getFingerprintedResourceUrl() { Map<String, VersionStrategy> versionStrategyMap = new HashMap<>(); versionStrategyMap.put("/**", new ContentVersionStrategy()); VersionResourceResolver versionResolver = new VersionResourceResolver(); versionResolver.setStrategyMap(versionStrategyMap); List<ResourceResolver> resolvers = new ArrayList<>(); resolvers.add(versionResolver); resolvers.add(new PathResourceResolver()); this.handler.setResourceResolvers(resolvers); String url = this.urlProvider.getForLookupPath("/resources/foo.css"); assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url); }
@Test // SPR-12647 public void bestPatternMatch() throws Exception { ResourceHttpRequestHandler otherHandler = new ResourceHttpRequestHandler(); otherHandler.setLocations(this.locations); Map<String, VersionStrategy> versionStrategyMap = new HashMap<>(); versionStrategyMap.put("/**", new ContentVersionStrategy()); VersionResourceResolver versionResolver = new VersionResourceResolver(); versionResolver.setStrategyMap(versionStrategyMap); List<ResourceResolver> resolvers = new ArrayList<>(); resolvers.add(versionResolver); resolvers.add(new PathResourceResolver()); otherHandler.setResourceResolvers(resolvers); this.handlerMap.put("/resources/*.css", otherHandler); this.urlProvider.setHandlerMap(this.handlerMap); String url = this.urlProvider.getForLookupPath("/resources/foo.css"); assertEquals("/resources/foo-e36d2e05253c6c7085a91522ce43a0b4.css", url); }
public String resolveUrlPath(String url) { if (this.resourceUrlProvider == null) { logger.trace("ResourceUrlProvider not available via request attribute " + "ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR"); return null; } if (this.indexLookupPath != null && url.startsWith(this.prefixLookupPath)) { int suffixIndex = getQueryParamsIndex(url); String suffix = url.substring(suffixIndex); String lookupPath = url.substring(this.indexLookupPath, suffixIndex); lookupPath = this.resourceUrlProvider.getForLookupPath(lookupPath); if (lookupPath != null) { return this.prefixLookupPath + lookupPath + suffix; } } return null; }
/** * A variation on {@link #getForLookupPath(String)} that accepts a full request * URL path (i.e. including context and servlet path) and returns the full request * URL path to expose for public use. * @param request the current request * @param requestUrl the request URL path to resolve * @return the resolved public URL path, or {@code null} if unresolved */ public final String getForRequestUrl(HttpServletRequest request, String requestUrl) { if (logger.isTraceEnabled()) { logger.trace("Getting resource URL for request URL \"" + requestUrl + "\""); } int prefixIndex = getLookupPathIndex(request); int suffixIndex = getEndPathIndex(requestUrl); if (prefixIndex >= suffixIndex) { return null; } String prefix = requestUrl.substring(0, prefixIndex); String suffix = requestUrl.substring(suffixIndex); String lookupPath = requestUrl.substring(prefixIndex, suffixIndex); String resolvedLookupPath = getForLookupPath(lookupPath); return (resolvedLookupPath != null ? prefix + resolvedLookupPath + suffix : null); }