    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.internal.services;
    014
    015import org.apache.tapestry5.internal.InternalConstants;
    016import org.apache.tapestry5.services.*;
    017
    018import java.io.IOException;
    019
    020/**
    021 * Dispatches incoming requests for render requests. Render requests consist of either just a logical page name (case
    022 * insensitive) or a logical page name plus additional context. Because of this structure, it take a little bit of work
    023 * to identify the split point between the page name and the context.
    024 */
    025public class PageRenderDispatcher implements Dispatcher
    026{
    027    private final ComponentRequestHandler componentRequestHandler;
    028
    029    private final ComponentEventLinkEncoder linkEncoder;
    030
    031    public PageRenderDispatcher(ComponentRequestHandler componentRequestHandler, ComponentEventLinkEncoder linkEncoder)
    032    {
    033        this.componentRequestHandler = componentRequestHandler;
    034        this.linkEncoder = linkEncoder;
    035    }
    036
    037    public boolean dispatch(Request request, final Response response) throws IOException
    038    {
    039        // If a component event request arrives (in production)
    040        // with an invalid component id, then we want it to be a 404
    041        // See TAP5-1481 and TAP5-2388
    042
    043        if (request.getAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND) != null)
    044        {
    045            // This needs to be cleared out because the container may submit a request back into the filter
    046            // for the 404 page and some containers reuse the existing HttpServletRequest. See TAP5-2388.
    047            request.setAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND, null);
    048            return false;
    049        }
    050
    051        PageRenderRequestParameters parameters = linkEncoder.decodePageRenderRequest(request);
    052
    053        if (parameters == null) return false;
    054
    055        componentRequestHandler.handlePageRender(parameters);
    056
    057        return true;
    058    }
    059}