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
042
043        if (request.getAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND) != null)
044        {
045            return false;
046        }
047
048        PageRenderRequestParameters parameters = linkEncoder.decodePageRenderRequest(request);
049
050        if (parameters == null) return false;
051
052        componentRequestHandler.handlePageRender(parameters);
053
054        return true;
055    }
056}