1414import java .net .URL ;
1515import java .util .Collections ;
1616import java .util .List ;
17+ import java .util .Map ;
1718
1819import 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