| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
package org.apache.tapestry5.internal.services; |
| 16 | |
|
| 17 | |
import org.apache.tapestry5.SymbolConstants; |
| 18 | |
import org.apache.tapestry5.ioc.Resource; |
| 19 | |
import org.apache.tapestry5.ioc.annotations.Inject; |
| 20 | |
import org.apache.tapestry5.ioc.annotations.Symbol; |
| 21 | |
import org.apache.tapestry5.ioc.internal.util.ClasspathResource; |
| 22 | |
import org.apache.tapestry5.services.AssetFactory; |
| 23 | |
import org.apache.tapestry5.services.ClasspathAssetAliasManager; |
| 24 | |
import org.apache.tapestry5.services.ContextProvider; |
| 25 | |
import org.apache.tapestry5.services.Response; |
| 26 | |
|
| 27 | |
import javax.servlet.http.HttpServletResponse; |
| 28 | |
import java.io.IOException; |
| 29 | |
|
| 30 | |
public class AssetResourceLocatorImpl implements AssetResourceLocator |
| 31 | |
{ |
| 32 | |
|
| 33 | |
private final ClasspathAssetAliasManager aliasManager; |
| 34 | |
|
| 35 | |
private final ResourceCache resourceCache; |
| 36 | |
|
| 37 | |
private final AssetFactory contextAssetFactory; |
| 38 | |
|
| 39 | |
private final Response response; |
| 40 | |
|
| 41 | |
private final String applicationAssetPrefix; |
| 42 | |
|
| 43 | |
public AssetResourceLocatorImpl(ClasspathAssetAliasManager aliasManager, |
| 44 | |
|
| 45 | |
ResourceCache resourceCache, |
| 46 | |
|
| 47 | |
@Inject @Symbol(SymbolConstants.APPLICATION_VERSION) |
| 48 | |
String applicationVersion, |
| 49 | |
|
| 50 | |
@ContextProvider |
| 51 | |
AssetFactory contextAssetFactory, |
| 52 | |
|
| 53 | |
Response response) |
| 54 | |
|
| 55 | 26 | { |
| 56 | 26 | this.aliasManager = aliasManager; |
| 57 | 26 | this.resourceCache = resourceCache; |
| 58 | 26 | this.contextAssetFactory = contextAssetFactory; |
| 59 | 26 | this.response = response; |
| 60 | |
|
| 61 | 26 | applicationAssetPrefix = RequestConstants.ASSET_PATH_PREFIX + RequestConstants.CONTEXT_FOLDER + applicationVersion + "/"; |
| 62 | 26 | } |
| 63 | |
|
| 64 | |
public Resource findResourceForPath(String path) throws IOException |
| 65 | |
{ |
| 66 | 252 | if (path.startsWith(applicationAssetPrefix)) |
| 67 | 24 | return findContextResource(path.substring(applicationAssetPrefix.length())); |
| 68 | |
|
| 69 | 228 | String resourcePath = aliasManager.toResourcePath(path); |
| 70 | |
|
| 71 | 228 | Resource resource = new ClasspathResource(resourcePath); |
| 72 | |
|
| 73 | 228 | if (!resourceCache.requiresDigest(resource)) return resource; |
| 74 | |
|
| 75 | 8 | String file = resource.getFile(); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | 8 | boolean valid = false; |
| 81 | 8 | Resource result = resource; |
| 82 | |
|
| 83 | 8 | int lastdotx = file.lastIndexOf('.'); |
| 84 | |
|
| 85 | 8 | if (lastdotx > 0) |
| 86 | |
{ |
| 87 | 6 | int prevdotx = file.lastIndexOf('.', lastdotx - 1); |
| 88 | |
|
| 89 | 6 | if (prevdotx > 0) |
| 90 | |
{ |
| 91 | |
|
| 92 | 4 | String requestDigest = file.substring(prevdotx + 1, lastdotx); |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | 4 | String realFile = file.substring(0, prevdotx) + file.substring(lastdotx); |
| 97 | |
|
| 98 | 4 | result = resource.forFile(realFile); |
| 99 | |
|
| 100 | 4 | String actualDigest = resourceCache.getDigest(result); |
| 101 | |
|
| 102 | 4 | valid = requestDigest.equals(actualDigest); |
| 103 | |
} |
| 104 | |
} |
| 105 | |
|
| 106 | 8 | if (valid) return result; |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | 6 | response.sendError(HttpServletResponse.SC_FORBIDDEN, |
| 112 | |
ServicesMessages.wrongAssetDigest(result)); |
| 113 | |
|
| 114 | 6 | return null; |
| 115 | |
} |
| 116 | |
|
| 117 | |
private Resource findContextResource(String contextPath) |
| 118 | |
{ |
| 119 | 24 | return contextAssetFactory.getRootResource().forFile(contextPath); |
| 120 | |
} |
| 121 | |
} |