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