-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support for configuring cache control headers Fixes #503 * Add docs for @MimeType and @originalFilename
- Loading branch information
1 parent
bdc1b8a
commit 0428f2f
Showing
6 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
spring-content-rest/src/main/asciidoc/rest-cachecontrol.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
== Cache Control | ||
|
||
By using cache control headers effectively, we can instruct the | ||
browser to cache resources and avoid network hops. This decreases | ||
latency, and also the load on our applications. | ||
|
||
Spring Content REST allows us to control these headers by configuring | ||
a set of cache control rules applied, in the order they are defined, | ||
to all GET requests for content. | ||
|
||
One, or more, rules can be configured as follows: | ||
|
||
==== | ||
[source, java] | ||
---- | ||
@Configuration | ||
class CustomContentRestMvcConfiguration { | ||
@Bean | ||
public ContentRestConfigurer configurer() { | ||
return new ContentRestConfigurer() { | ||
@Override | ||
public void configure(RestConfiguration config) { | ||
config | ||
.cacheControl() | ||
.antMatcher("/testEntities/*", CacheControl.maxAge(Duration.ofSeconds(60))); | ||
} | ||
}; | ||
} | ||
} | ||
---- | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...t/src/main/java/org/springframework/content/rest/config/StoreCacheControlInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.springframework.content.rest.config; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.http.CacheControl; | ||
import org.springframework.util.AntPathMatcher; | ||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
import org.springframework.web.util.UrlPathHelper; | ||
|
||
import internal.org.springframework.content.rest.utils.StoreUtils; | ||
import lombok.Getter; | ||
|
||
public class StoreCacheControlInterceptor extends HandlerInterceptorAdapter { | ||
|
||
private Map<String, CacheControl> cacheControlMap = new HashMap<>(); | ||
private List<CacheControlRule> cacheControlRules = new ArrayList<>(); | ||
private URI baseUri; | ||
|
||
public StoreCacheControlInterceptor() { | ||
|
||
} | ||
|
||
public StoreCacheControlConfigurer configurer() { | ||
return new StoreCacheControlConfigurer(this); | ||
} | ||
|
||
public void addCacheControl(String mapping, CacheControl cacheControl) { | ||
cacheControlMap.put(mapping, cacheControl); | ||
} | ||
|
||
public void addCacheControlRule(CacheControlRule cacheControlRule) { | ||
cacheControlRules.add(cacheControlRule); | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
|
||
if (!"GET".equals(request.getMethod())) { | ||
return true; | ||
} | ||
|
||
UrlPathHelper pathHelper = UrlPathHelper.defaultInstance; | ||
|
||
String lookupPath = pathHelper.getLookupPathForRequest(request); | ||
String storeLookupPath = StoreUtils.storeLookupPath(lookupPath, baseUri); | ||
|
||
CacheControl cacheControl = cacheControlMap.get(storeLookupPath); | ||
|
||
for (CacheControlRule rule : cacheControlRules) { | ||
|
||
if (rule.match(storeLookupPath)) { | ||
response.addHeader("Cache-Control", rule.getCacheControl().getHeaderValue()); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void setBaseUri(URI baseUri) { | ||
this.baseUri = baseUri; | ||
} | ||
|
||
@Getter | ||
static class CacheControlRule { | ||
|
||
private static AntPathMatcher matcher = new AntPathMatcher(); | ||
|
||
private String pattern; | ||
private CacheControl cacheControl; | ||
|
||
public CacheControlRule(String pattern, CacheControl control) { | ||
this.pattern = pattern; | ||
this.cacheControl = control; | ||
} | ||
|
||
public boolean match(String path) { | ||
return matcher.match(pattern, path); | ||
} | ||
} | ||
|
||
public static class StoreCacheControlConfigurer { | ||
|
||
private final StoreCacheControlInterceptor interceptor; | ||
|
||
public StoreCacheControlConfigurer(StoreCacheControlInterceptor interceptor) { | ||
this.interceptor = interceptor; | ||
} | ||
|
||
public StoreCacheControlConfigurer antMatcher(String pattern, CacheControl cacheControl) { | ||
interceptor.addCacheControlRule(new CacheControlRule(pattern, cacheControl)); | ||
return this; | ||
} | ||
} | ||
} |
Oops, something went wrong.