Add AppCacheResourceTransformer

This change adds a new ResourceTransformer that helps handling resources
within HTML5 AppCache manifests for HTML5 offline application.

This transformer:
* modifies links to match the public URL paths
* appends a comment in the manifest, containing a Hash (e.g. "# Hash:
9de0f09ed7caf84e885f1f0f11c7e326")

See http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#offline
for more details on HTML5 offline apps and appcache manifests.

Here is a WebConfig example:

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {

    AppCacheResourceTransformer appCacheTransformer =
          new AppCacheResourceTransformer();

      registry.addResourceHandler("/**")
          .addResourceLocations("classpath:static/")
          .setResourceResolvers(...)
          .setResourceTransformers(..., appCacheTransformer);
  }

Issue: SPR-11964
This commit is contained in:
Brian Clozel
2014-07-10 16:55:25 +02:00
parent 2bd6e24b65
commit f11815c960
4 changed files with 359 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* 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.servlet.resource;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;
/**
* Unit tests for
* {@link org.springframework.web.servlet.resource.AppCacheResourceTransformer}.
*
* @author Brian Clozel
*/
public class AppCacheResourceTransformerTests {
private AppCacheResourceTransformer transformer;
private ResourceTransformerChain chain;
private HttpServletRequest request;
@Before
public void setup() {
this.transformer = new AppCacheResourceTransformer();
this.chain = mock(ResourceTransformerChain.class);
this.request = mock(HttpServletRequest.class);
}
@Test
public void noTransformIfExtensionNoMatch() throws Exception {
Resource resource = mock(Resource.class);
when(resource.getFilename()).thenReturn("foobar.file");
when(this.chain.transform(this.request, resource)).thenReturn(resource);
Resource result = this.transformer.transform(this.request, resource, this.chain);
assertEquals(resource, result);
}
@Test
public void syntaxErrorInManifest() throws Exception {
Resource resource = new ClassPathResource("test/error.manifest", getClass());
when(this.chain.transform(this.request, resource)).thenReturn(resource);
Resource result = this.transformer.transform(this.request, resource, this.chain);
assertEquals(resource, result);
}
@Test
public void transformManifest() throws Exception {
VersionResourceResolver versionResourceResolver = new VersionResourceResolver();
versionResourceResolver
.setVersionStrategyMap(Collections.singletonMap("/**", new ContentBasedVersionStrategy()));
List<ResourceResolver> resolvers = new ArrayList<ResourceResolver>();
resolvers.add(versionResourceResolver);
resolvers.add(new PathResourceResolver());
ResourceResolverChain resolverChain = new DefaultResourceResolverChain(resolvers);
List<ResourceTransformer> transformers = new ArrayList<>();
transformers.add(new CssLinkResourceTransformer());
this.chain = new DefaultResourceTransformerChain(resolverChain, transformers);
Resource resource = new ClassPathResource("test/appcache.manifest", getClass());
Resource result = this.transformer.transform(this.request, resource, this.chain);
byte[] bytes = FileCopyUtils.copyToByteArray(result.getInputStream());
String content = new String(bytes, "UTF-8");
assertThat("should rewrite resource links", content,
Matchers.containsString("foo-e36d2e05253c6c7085a91522ce43a0b4.css"));
assertThat("should rewrite resource links", content,
Matchers.containsString("bar-11e16cf79faee7ac698c805cf28248d2.css"));
assertThat("should rewrite resource links", content,
Matchers.containsString("js/bar-bd508c62235b832d960298ca6c0b7645.js"));
assertThat("should not rewrite external resources", content,
Matchers.containsString("//example.org/style.css"));
assertThat("should not rewrite external resources", content,
Matchers.containsString("http://example.org/image.png"));
assertThat("should generate fingerprint", content,
Matchers.containsString("# Hash: 4bf0338bcbeb0a5b3a4ec9ed8864107d"));
}
}