14
14
import java .net .URL ;
15
15
import java .util .Collections ;
16
16
import java .util .List ;
17
+ import java .util .Map ;
17
18
18
19
import static com .google .common .base .Preconditions .checkNotNull ;
19
20
@@ -116,15 +117,7 @@ public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp
116
117
|| RestxContext .Modes .TEST .equals (ctx .getMode ())
117
118
|| RestxContext .Modes .INFINIREST .equals (ctx .getMode ())
118
119
);
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 );
128
121
} catch (IllegalArgumentException e ) {
129
122
notFound (resp , relativePath );
130
123
}
@@ -134,6 +127,32 @@ protected String requestRelativePath(RestxRequest req) {
134
127
return req .getRestxPath ().substring (baseRestPath .length ());
135
128
}
136
129
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
+
137
156
protected Optional <CachedResourcePolicy > cachePolicyMatching (String contentType , String path ) {
138
157
for (CachedResourcePolicy cachedResourcePolicy : cachedResourcePolicies ){
139
158
if (cachedResourcePolicy .matches (contentType , path )){
0 commit comments