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