Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

return correct etag looked up in manifest for assets in war file

  • Loading branch information...
commit 5e1bad7ed1149faf75bc403ce80713ed06b513f2 1 parent 6918dd0
@tednaleid tednaleid authored
View
2  AssetPipelineGrailsPlugin.groovy
@@ -44,7 +44,7 @@ class AssetPipelineGrailsPlugin {
try {
manifestFile = application.getParentContext().getResource("assets/manifest.properties")
} catch(e) {
- //Silent fail
+ println "Unable to find asset-pipeline manifest, etags will not be properly generated"
}
if(manifestFile?.exists()) {
try {
View
26 src/groovy/asset/pipeline/AssetPipelineFilter.groovy
@@ -2,7 +2,6 @@ package asset.pipeline
import javax.servlet.*
import org.springframework.web.context.support.WebApplicationContextUtils
-import grails.util.Environment
import groovy.util.logging.Log4j
@Log4j
@@ -66,22 +65,11 @@ class AssetPipelineFilter implements Filter {
/**
* Here we check if the request is contingent upon an ETag and if not, we append the ETag to the header key.
* This ETag is essentially the digested file name as it is unique unless the file changes.
- * @return Wether processing should continue or not
+ * @return Whether processing should continue or not
*/
Boolean checkETag(ServletRequest request, ServletResponse response, fileUri) {
- def manifestPath = fileUri
- if(fileUri.startsWith('/')) {
- manifestPath = fileUri.substring(1) //Omit forward slash
- }
+ String etagName = getCurrentETag(fileUri)
- def etagName = manifestPath
- def manifest = applicationContext.grailsApplication.config.grails.assets.manifest
- if(manifest) {
- def digestedName = manifest.getProperty(manifestPath)
- if(digestedName) {
- etagName = manifestPath
- }
- }
def ifNoneMatchHeader = request.getHeader('If-None-Match')
if(ifNoneMatchHeader && ifNoneMatchHeader == etagName) {
response.status = 304
@@ -92,4 +80,14 @@ class AssetPipelineFilter implements Filter {
return true
}
+ String getCurrentETag(String fileUri) {
+ def manifestPath = fileUri
+ if(fileUri.startsWith('/')) {
+ manifestPath = fileUri.substring(1) //Omit forward slash
+ }
+
+ def manifest = applicationContext.grailsApplication.config.grails.assets.manifest
+
+ return manifest?.getProperty(manifestPath) ?: manifestPath
+ }
}
View
71 test/unit/asset/pipeline/AssetPipelineFilterSpec.groovy
@@ -0,0 +1,71 @@
+/*
+ * Copyright 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 asset.pipeline
+
+import grails.test.mixin.TestMixin
+import grails.test.mixin.support.GrailsUnitTestMixin
+import spock.lang.Specification
+
+@TestMixin(GrailsUnitTestMixin)
+class AssetPipelineFilterSpec extends Specification {
+ AssetPipelineFilter filter
+ Properties manifest
+
+ void setup() {
+ filter = new AssetPipelineFilter(applicationContext: [grailsApplication: grailsApplication])
+ manifest = new Properties()
+ grailsApplication.config.grails.assets.manifest = manifest
+ }
+
+ void "current etag should be fileUri when manifest is missing"() {
+ given:
+ grailsApplication.config.grails.assets.manifest = null
+ String fileUri = "favicon.ico"
+
+ when:
+ String etag = filter.getCurrentETag(fileUri)
+
+
+ then:
+ assert etag == fileUri
+ }
+
+
+ void "current etag should be fileUri when not in manifest"() {
+ given:
+ String fileUri = "favicon.ico"
+
+ when:
+ String etag = filter.getCurrentETag(fileUri)
+
+ then:
+ assert etag == fileUri
+ }
+
+ void "current etag should come from manifest when present"() {
+ given:
+ String fileUri = "favicon.ico"
+ String digestUri = "favicon-9ef27019cc7a636e29ecc851528f716e.ico"
+ manifest.setProperty(fileUri, digestUri)
+
+ when:
+ String etag = filter.getCurrentETag(fileUri)
+
+ then:
+ assert etag == digestUri
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.