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