WebJarsResourceResolver.java 4.0 KB
Newer Older
1
/*
2
 * Copyright 2002-2019 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.reactive.resource;

import java.util.List;

import org.webjars.WebJarAssetLocator;
22
import reactor.core.publisher.Mono;
23 24

import org.springframework.core.io.Resource;
25
import org.springframework.lang.Nullable;
26 27 28 29 30 31 32 33 34 35 36 37 38
import org.springframework.web.server.ServerWebExchange;

/**
 * A {@code ResourceResolver} that delegates to the chain to locate a resource and then
 * attempts to find a matching versioned resource contained in a WebJar JAR file.
 *
 * <p>This allows WebJars.org users to write version agnostic paths in their templates,
 * like {@code <script src="/jquery/jquery.min.js"/>}.
 * This path will be resolved to the unique version {@code <script src="/jquery/1.2.0/jquery.min.js"/>},
 * which is a better fit for HTTP caching and version management in applications.
 *
 * <p>This also resolves resources for version agnostic HTTP requests {@code "GET /jquery/jquery.min.js"}.
 *
39 40
 * <p>This resolver requires the {@code org.webjars:webjars-locator-core} library
 * on the classpath and is automatically registered if that library is present.
41 42 43 44
 *
 * @author Rossen Stoyanchev
 * @author Brian Clozel
 * @since 5.0
S
Spring Operator 已提交
45
 * @see <a href="https://www.webjars.org">webjars.org</a>
46 47 48
 */
public class WebJarsResourceResolver extends AbstractResourceResolver {

49
	private static final String WEBJARS_LOCATION = "META-INF/resources/webjars/";
50

51
	private static final int WEBJARS_LOCATION_LENGTH = WEBJARS_LOCATION.length();
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73


	private final WebJarAssetLocator webJarAssetLocator;


	/**
	 * Create a {@code WebJarsResourceResolver} with a default {@code WebJarAssetLocator} instance.
	 */
	public WebJarsResourceResolver() {
		this(new WebJarAssetLocator());
	}

	/**
	 * Create a {@code WebJarsResourceResolver} with a custom {@code WebJarAssetLocator} instance,
	 * e.g. with a custom index.
	 */
	public WebJarsResourceResolver(WebJarAssetLocator webJarAssetLocator) {
		this.webJarAssetLocator = webJarAssetLocator;
	}


	@Override
74 75
	protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange,
			String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
76

77
		return chain.resolveResource(exchange, requestPath, locations)
78
				.switchIfEmpty(Mono.defer(() -> {
79 80 81 82 83 84 85 86
					String webJarsResourcePath = findWebJarResourcePath(requestPath);
					if (webJarsResourcePath != null) {
						return chain.resolveResource(exchange, webJarsResourcePath, locations);
					}
					else {
						return Mono.empty();
					}
				}));
87 88 89
	}

	@Override
90
	protected Mono<String> resolveUrlPathInternal(String resourceUrlPath,
91 92
			List<? extends Resource> locations, ResourceResolverChain chain) {

93
		return chain.resolveUrlPath(resourceUrlPath, locations)
94
				.switchIfEmpty(Mono.defer(() -> {
95 96 97 98 99 100 101 102
					String webJarResourcePath = findWebJarResourcePath(resourceUrlPath);
					if (webJarResourcePath != null) {
						return chain.resolveUrlPath(webJarResourcePath, locations);
					}
					else {
						return Mono.empty();
					}
				}));
103 104
	}

105
	@Nullable
106
	protected String findWebJarResourcePath(String path) {
107 108 109 110 111
		int startOffset = (path.startsWith("/") ? 1 : 0);
		int endOffset = path.indexOf('/', 1);
		if (endOffset != -1) {
			String webjar = path.substring(startOffset, endOffset);
			String partialPath = path.substring(endOffset + 1);
112
			String webJarPath = this.webJarAssetLocator.getFullPathExact(webjar, partialPath);
113
			if (webJarPath != null) {
114 115 116 117 118 119 120
				return webJarPath.substring(WEBJARS_LOCATION_LENGTH);
			}
		}
		return null;
	}

}