| 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.ioc.Resource; |
| 18 | |
import org.apache.tapestry5.services.ClasspathAssetAliasManager; |
| 19 | |
import org.apache.tapestry5.services.Dispatcher; |
| 20 | |
import org.apache.tapestry5.services.Request; |
| 21 | |
import org.apache.tapestry5.services.Response; |
| 22 | |
|
| 23 | |
import javax.servlet.http.HttpServletResponse; |
| 24 | |
import java.io.IOException; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class AssetDispatcher implements Dispatcher |
| 35 | |
{ |
| 36 | |
private final ResourceStreamer streamer; |
| 37 | |
|
| 38 | |
private final ResourceCache resourceCache; |
| 39 | |
|
| 40 | |
private final AssetResourceLocator assetResourceLocator; |
| 41 | |
|
| 42 | |
static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since"; |
| 43 | |
|
| 44 | |
public AssetDispatcher(ResourceStreamer streamer, |
| 45 | |
|
| 46 | |
ResourceCache resourceCache, |
| 47 | |
|
| 48 | |
AssetResourceLocator assetResourceLocator) |
| 49 | 74 | { |
| 50 | 74 | this.streamer = streamer; |
| 51 | 74 | this.resourceCache = resourceCache; |
| 52 | 74 | this.assetResourceLocator = assetResourceLocator; |
| 53 | |
|
| 54 | 74 | } |
| 55 | |
|
| 56 | |
public boolean dispatch(Request request, Response response) throws IOException |
| 57 | |
{ |
| 58 | 1614 | String path = request.getPath(); |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | 1614 | if (!path.startsWith(RequestConstants.ASSET_PATH_PREFIX)) return false; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 110 | Resource resource = assetResourceLocator.findResourceForPath(path); |
| 68 | |
|
| 69 | 110 | if (resource == null) return true; |
| 70 | |
|
| 71 | 104 | if (!resource.exists()) |
| 72 | |
{ |
| 73 | 0 | response.sendError(HttpServletResponse.SC_NOT_FOUND, ServicesMessages |
| 74 | |
.assetDoesNotExist(resource)); |
| 75 | 0 | return true; |
| 76 | |
} |
| 77 | |
|
| 78 | 104 | long ifModifiedSince = 0; |
| 79 | |
|
| 80 | |
try |
| 81 | |
{ |
| 82 | 104 | ifModifiedSince = request.getDateHeader(IF_MODIFIED_SINCE_HEADER); |
| 83 | |
} |
| 84 | 2 | catch (IllegalArgumentException ex) |
| 85 | |
{ |
| 86 | |
|
| 87 | |
|
| 88 | 2 | ifModifiedSince = -1; |
| 89 | 102 | } |
| 90 | |
|
| 91 | 104 | if (ifModifiedSince > 0) |
| 92 | |
{ |
| 93 | 4 | long modified = resourceCache.getTimeModified(resource); |
| 94 | |
|
| 95 | 4 | if (ifModifiedSince >= modified) |
| 96 | |
{ |
| 97 | 2 | response.sendError(HttpServletResponse.SC_NOT_MODIFIED, ""); |
| 98 | 2 | return true; |
| 99 | |
} |
| 100 | |
} |
| 101 | |
|
| 102 | 102 | streamer.streamResource(resource); |
| 103 | |
|
| 104 | 102 | return true; |
| 105 | |
} |
| 106 | |
} |