Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 324   Methods: 12
NCLOC: 166   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AssetService.java 50% 82.1% 100% 78.9%
coverage coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.asset;
 16   
 17    import java.io.BufferedInputStream;
 18    import java.io.IOException;
 19    import java.io.InputStream;
 20    import java.io.OutputStream;
 21    import java.net.URL;
 22    import java.net.URLConnection;
 23    import java.util.HashMap;
 24    import java.util.Map;
 25   
 26    import javax.servlet.http.HttpServletResponse;
 27   
 28    import org.apache.hivemind.ApplicationRuntimeException;
 29    import org.apache.hivemind.ClassResolver;
 30    import org.apache.hivemind.util.Defense;
 31    import org.apache.hivemind.util.IOUtils;
 32    import org.apache.tapestry.IRequestCycle;
 33    import org.apache.tapestry.Tapestry;
 34    import org.apache.tapestry.engine.IEngineService;
 35    import org.apache.tapestry.engine.ILink;
 36    import org.apache.tapestry.error.RequestExceptionReporter;
 37    import org.apache.tapestry.services.LinkFactory;
 38    import org.apache.tapestry.services.ServiceConstants;
 39    import org.apache.tapestry.util.ContentType;
 40    import org.apache.tapestry.web.WebContext;
 41    import org.apache.tapestry.web.WebRequest;
 42    import org.apache.tapestry.web.WebResponse;
 43   
 44    /**
 45    * A service for building URLs to and accessing {@link org.apache.tapestry.IAsset}s. Most of the
 46    * work is deferred to the {@link org.apache.tapestry.IAsset}instance.
 47    * <p>
 48    * The retrieval part is directly linked to {@link PrivateAsset}. The service responds to a URL
 49    * that encodes the path of a resource within the classpath. The {@link #service(IRequestCycle)}
 50    * method reads the resource and streams it out.
 51    * <p>
 52    * TBD: Security issues. Should only be able to retrieve a resource that was previously registerred
 53    * in some way ... otherwise, hackers will be able to suck out the .class files of the application!
 54    *
 55    * @author Howard Lewis Ship
 56    */
 57   
 58    public class AssetService implements IEngineService
 59    {
 60   
 61    /** @since 4.0 */
 62    private ClassResolver _classResolver;
 63   
 64    /** @since 4.0 */
 65    private LinkFactory _linkFactory;
 66   
 67    /** @since 4.0 */
 68    private WebContext _context;
 69   
 70    /** @since 4.0 */
 71   
 72    private WebRequest _request;
 73   
 74    /** @since 4.0 */
 75    private WebResponse _response;
 76   
 77    /** @since 4.0 */
 78    private ResourceDigestSource _digestSource;
 79   
 80    /**
 81    * Defaults MIME types, by extension, used when the servlet container doesn't provide MIME
 82    * types. ServletExec Debugger, for example, fails to provide these.
 83    */
 84   
 85    private final static Map _mimeTypes;
 86   
 87    static
 88    {
 89  1 _mimeTypes = new HashMap(17);
 90  1 _mimeTypes.put("css", "text/css");
 91  1 _mimeTypes.put("gif", "image/gif");
 92  1 _mimeTypes.put("jpg", "image/jpeg");
 93  1 _mimeTypes.put("jpeg", "image/jpeg");
 94  1 _mimeTypes.put("htm", "text/html");
 95  1 _mimeTypes.put("html", "text/html");
 96    }
 97   
 98    private static final int BUFFER_SIZE = 10240;
 99   
 100    /**
 101    * Startup time for this service; used to set the Last-Modified response header.
 102    *
 103    * @since 4.0
 104    */
 105   
 106    private final long _startupTime = System.currentTimeMillis();
 107   
 108    /**
 109    * Time vended assets expire. Since a change in asset content is a change in asset URI, we want
 110    * them to not expire ... but a year will do.
 111    */
 112   
 113    private final long _expireTime = _startupTime + 365 * 24 * 60 * 60 * 1000;
 114   
 115    /** @since 4.0 */
 116   
 117    private RequestExceptionReporter _exceptionReporter;
 118   
 119    /**
 120    * Query parameter that stores the path to the resource (with a leading slash).
 121    *
 122    * @since 4.0
 123    */
 124   
 125    public static final String PATH = "path";
 126   
 127    /**
 128    * Query parameter that stores the digest for the file; this is used to authenticate that the
 129    * client is allowed to access the file.
 130    *
 131    * @since 4.0
 132    */
 133   
 134    public static final String DIGEST = "digest";
 135   
 136    /**
 137    * Builds a {@link ILink}for a {@link PrivateAsset}.
 138    * <p>
 139    * A single parameter is expected, the resource path of the asset (which is expected to start
 140    * with a leading slash).
 141    */
 142   
 143  75 public ILink getLink(IRequestCycle cycle, boolean post, Object parameter)
 144    {
 145  75 Defense.isAssignable(parameter, String.class, "parameter");
 146   
 147  75 String path = (String) parameter;
 148   
 149  75 String digest = _digestSource.getDigestForResource(path);
 150   
 151  75 Map parameters = new HashMap();
 152   
 153  75 parameters.put(ServiceConstants.SERVICE, getName());
 154  75 parameters.put(PATH, path);
 155  75 parameters.put(DIGEST, digest);
 156   
 157    // Service is stateless, which is the exception to the rule.
 158   
 159  75 return _linkFactory.constructLink(cycle, post, parameters, false);
 160    }
 161   
 162  101 public String getName()
 163    {
 164  101 return Tapestry.ASSET_SERVICE;
 165    }
 166   
 167  1 private String getMimeType(String path)
 168    {
 169  1 String result = _context.getMimeType(path);
 170   
 171  1 if (result == null)
 172    {
 173  0 int dotx = path.lastIndexOf('.');
 174  0 String key = path.substring(dotx + 1).toLowerCase();
 175   
 176  0 result = (String) _mimeTypes.get(key);
 177   
 178  0 if (result == null)
 179  0 result = "text/plain";
 180    }
 181   
 182  1 return result;
 183    }
 184   
 185    /**
 186    * Retrieves a resource from the classpath and returns it to the client in a binary output
 187    * stream.
 188    */
 189   
 190  1 public void service(IRequestCycle cycle) throws IOException
 191    {
 192  1 String path = cycle.getParameter(PATH);
 193  1 String md5Digest = cycle.getParameter(DIGEST);
 194   
 195  1 try
 196    {
 197  1 if (!_digestSource.getDigestForResource(path).equals(md5Digest))
 198    {
 199  0 _response.sendError(HttpServletResponse.SC_FORBIDDEN, AssetMessages
 200    .md5Mismatch(path));
 201  0 return;
 202    }
 203   
 204    // If they were vended an asset in the past then it must be up-to date.
 205    // Asset URIs change if the underlying file is modified.
 206   
 207  1 if (_request.getHeader("If-Modified-Since") != null)
 208    {
 209  0 _response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
 210  0 return;
 211    }
 212   
 213  1 URL resourceURL = _classResolver.getResource(path);
 214   
 215  1 if (resourceURL == null)
 216  0 throw new ApplicationRuntimeException(AssetMessages.noSuchResource(path));
 217   
 218  1 URLConnection resourceConnection = resourceURL.openConnection();
 219   
 220  1 writeAssetContent(cycle, path, resourceConnection);
 221    }
 222    catch (Throwable ex)
 223    {
 224  0 _exceptionReporter.reportRequestException(AssetMessages.exceptionReportTitle(path), ex);
 225    }
 226   
 227    }
 228   
 229    /** @since 2.2 */
 230   
 231  1 private void writeAssetContent(IRequestCycle cycle, String resourcePath,
 232    URLConnection resourceConnection) throws IOException
 233    {
 234  1 InputStream input = null;
 235   
 236  1 try
 237    {
 238    // Getting the content type and length is very dependant
 239    // on support from the application server (represented
 240    // here by the servletContext).
 241   
 242  1 String contentType = getMimeType(resourcePath);
 243  1 int contentLength = resourceConnection.getContentLength();
 244   
 245  1 if (contentLength > 0)
 246  1 _response.setContentLength(contentLength);
 247   
 248  1 _response.setDateHeader("Last-Modified", _startupTime);
 249  1 _response.setDateHeader("Expires", _expireTime);
 250   
 251    // Set the content type. If the servlet container doesn't
 252    // provide it, try and guess it by the extension.
 253   
 254  1 if (contentType == null || contentType.length() == 0)
 255  0 contentType = getMimeType(resourcePath);
 256   
 257  1 OutputStream output = _response.getOutputStream(new ContentType(contentType));
 258   
 259  1 input = new BufferedInputStream(resourceConnection.getInputStream());
 260   
 261  1 byte[] buffer = new byte[BUFFER_SIZE];
 262   
 263  1 while (true)
 264    {
 265  2 int bytesRead = input.read(buffer);
 266   
 267  2 if (bytesRead < 0)
 268  1 break;
 269   
 270  1 output.write(buffer, 0, bytesRead);
 271    }
 272   
 273  1 input.close();
 274  1 input = null;
 275    }
 276    finally
 277    {
 278  1 IOUtils.close(input);
 279    }
 280    }
 281   
 282    /** @since 4.0 */
 283   
 284  26 public void setExceptionReporter(RequestExceptionReporter exceptionReporter)
 285    {
 286  26 _exceptionReporter = exceptionReporter;
 287    }
 288   
 289    /** @since 4.0 */
 290  26 public void setLinkFactory(LinkFactory linkFactory)
 291    {
 292  26 _linkFactory = linkFactory;
 293    }
 294   
 295    /** @since 4.0 */
 296  26 public void setClassResolver(ClassResolver classResolver)
 297    {
 298  26 _classResolver = classResolver;
 299    }
 300   
 301    /** @since 4.0 */
 302  26 public void setContext(WebContext context)
 303    {
 304  26 _context = context;
 305    }
 306   
 307    /** @since 4.0 */
 308  26 public void setResponse(WebResponse response)
 309    {
 310  26 _response = response;
 311    }
 312   
 313    /** @since 4.0 */
 314  26 public void setDigestSource(ResourceDigestSource md5Source)
 315    {
 316  26 _digestSource = md5Source;
 317    }
 318   
 319    /** @since 4.0 */
 320  26 public void setRequest(WebRequest request)
 321    {
 322  26 _request = request;
 323    }
 324    }