    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.internal.structure.Page;
    017import org.apache.tapestry5.ioc.util.UnknownValueException;
    018import org.apache.tapestry5.services.*;
    019
    020import java.io.IOException;
    021
    022/**
    023 * A filter, used only in production mode, that does a "pre-flight check" that the indicated component
    024 * actually exists. If it does not, then the handling of the component event is aborted and other
    025 * hooks will ensure that request ultimately becomes a 404.
    026 *
    027 * @since 5.4
    028 */
    029public class ProductionModeUnknownComponentFilter implements ComponentRequestFilter
    030{
    031    private final Request request;
    032
    033    private final RequestPageCache cache;
    034
    035    public ProductionModeUnknownComponentFilter(Request request, RequestPageCache cache)
    036    {
    037        this.request = request;
    038        this.cache = cache;
    039    }
    040
    041    @Override
    042    public void handleComponentEvent(ComponentEventRequestParameters parameters, ComponentRequestHandler handler) throws IOException
    043    {
    044        Page containerPage = cache.get(parameters.getContainingPageName());
    045
    046        try
    047        {
    048            containerPage.getComponentElementByNestedId(parameters.getNestedComponentId());
    049
    050            handler.handleComponentEvent(parameters);
    051
    052        } catch (UnknownValueException ex)
    053        {
    054            request.setAttribute(InternalConstants.REFERENCED_COMPONENT_NOT_FOUND, true);
    055        }
    056    }
    057
    058    @Override
    059    public void handlePageRender(PageRenderRequestParameters parameters, ComponentRequestHandler handler) throws IOException
    060    {
    061        // Pass these through to the default handler.
    062        handler.handlePageRender(parameters);
    063    }
    064}