001// Copyright 2010, 2012, 2013 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.services.assets;
016
017import org.apache.tapestry5.Asset;
018import org.apache.tapestry5.internal.services.AssetDispatcher;
019import org.apache.tapestry5.services.Request;
020import org.apache.tapestry5.services.Response;
021
022import javax.servlet.http.HttpServletResponse;
023import java.io.IOException;
024
025/**
026 * Handler for asset requests, which expose some kind of {@link Asset} to
027 * the user agent (i.e., the client web browser). When contributed to the {@link AssetDispatcher} service,
028 * the contributed key is a handler id (such as "meta/core").
029 * <p/>
030 * An example request path might be <code>/assets/meta/core/dd8d73ac51dbab28caaec4865d302bf2/deselect.png</code>.
031 * The handler id would be
032 * <code>meta/core</code>, the {@linkplain AssetChecksumGenerator checksum of the resource content} is the
033 * hex string, and the extra path would be <code>select.png</code>.
034 *
035 * @see AssetDispatcher
036 * @see org.apache.tapestry5.services.AssetRequestDispatcher
037 * @see AssetPathConstructor
038 * @since 5.2.0
039 */
040public interface AssetRequestHandler
041{
042    /**
043     * Given a request targeted (via the handler id) to the specific handler, process the request.
044     * The handler is responsible for processing the request, sending back either a bytestream
045     * (via {@link Response#getOutputStream(String)}) or an error response
046     * (via {@link Response#sendError(int, String)}). It is the handler's responsibility to allow
047     * for client-side caching (possibly sending an {@link HttpServletResponse#SC_NOT_MODIFIED} response).
048     * <p/>
049     * The handler should return true if it provided a response. If the handler returns false, this indicates that the
050     * extra path did not identify a known asset (virtual or otherwise) and the AssetDispatcher service should send a
051     * {@link HttpServletResponse#SC_NOT_FOUND} response.
052     * <p/>
053     * Starting in Tapestry 5.4, the handler is informed by the {@link org.apache.tapestry5.services.AssetRequestDispatcher}
054     * whether or not the content should be compressed (this is determined based on information in the URL).
055     *
056     * @param request
057     *         incoming asset request
058     * @param response
059     *         used to send a response to client
060     * @param extraPath
061     *         additional path to identify the specific asset
062     * @return true if request was handled (and response sent), false if asset not found
063     * @see org.apache.tapestry5.TapestryConstants#COMPRESS_CONTENT
064     */
065    boolean handleAssetRequest(Request request, Response response, String extraPath) throws IOException;
066}