Skip to content

Commit 7389683

Browse files
committed
Introduced ResourceRoute's serveResource() and serveCacheableResource() utility methods
1 parent 34822ee commit 7389683

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

restx-core/src/main/java/restx/ResourcesRoute.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.net.URL;
1515
import java.util.Collections;
1616
import java.util.List;
17+
import java.util.Map;
1718

1819
import static com.google.common.base.Preconditions.checkNotNull;
1920

@@ -116,15 +117,7 @@ public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp
116117
|| RestxContext.Modes.TEST.equals(ctx.getMode())
117118
|| RestxContext.Modes.INFINIREST.equals(ctx.getMode())
118119
);
119-
resp.setLogLevel(RestxLogLevel.QUIET);
120-
resp.setStatus(HttpStatus.OK);
121-
String contentType = HTTP.getContentTypeFromExtension(relativePath).or("application/octet-stream");
122-
Optional<CachedResourcePolicy> cachedResourcePolicy = cachePolicyMatching(contentType, relativePath);
123-
if(cachedResourcePolicy.isPresent()) {
124-
resp.setHeader("Cache-Control", cachedResourcePolicy.get().getCacheValue());
125-
}
126-
resp.setContentType(contentType);
127-
Resources.asByteSource(resource).copyTo(resp.getOutputStream());
120+
serveCacheableResource(resp, resource, relativePath);
128121
} catch (IllegalArgumentException e) {
129122
notFound(resp, relativePath);
130123
}
@@ -134,6 +127,32 @@ protected String requestRelativePath(RestxRequest req) {
134127
return req.getRestxPath().substring(baseRestPath.length());
135128
}
136129

130+
protected void serveCacheableResource(RestxResponse resp, URL resource, String relativePath) throws IOException {
131+
String contentType = HTTP.getContentTypeFromExtension(relativePath).or("application/octet-stream");
132+
133+
ImmutableMap.Builder<String, String> headers = ImmutableMap.builder();
134+
Optional<CachedResourcePolicy> cachedResourcePolicy = cachePolicyMatching(contentType, relativePath);
135+
if(cachedResourcePolicy.isPresent()) {
136+
headers.put("Cache-Control", cachedResourcePolicy.get().getCacheValue());
137+
}
138+
139+
serveResource(resp, resource, contentType, headers.build());
140+
}
141+
142+
protected void serveResource(RestxResponse resp, URL resource, String contentType) throws IOException {
143+
serveResource(resp, resource, contentType, ImmutableMap.<String, String>of());
144+
}
145+
146+
protected void serveResource(RestxResponse resp, URL resource, String contentType, Map<String, String> headers) throws IOException {
147+
resp.setLogLevel(RestxLogLevel.QUIET);
148+
resp.setStatus(HttpStatus.OK);
149+
for(Map.Entry<String,String> headerEntry: headers.entrySet()) {
150+
resp.setHeader(headerEntry.getKey(), headerEntry.getValue());
151+
}
152+
resp.setContentType(contentType);
153+
Resources.asByteSource(resource).copyTo(resp.getOutputStream());
154+
}
155+
137156
protected Optional<CachedResourcePolicy> cachePolicyMatching(String contentType, String path) {
138157
for(CachedResourcePolicy cachedResourcePolicy : cachedResourcePolicies){
139158
if(cachedResourcePolicy.matches(contentType, path)){

0 commit comments

Comments
 (0)