001// Copyright 2030 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.ComponentResources;
018import org.apache.tapestry5.EventContext;
019import org.apache.tapestry5.ioc.annotations.Primary;
020import org.apache.tapestry5.services.ComponentEventResultProcessor;
021import org.apache.tapestry5.services.HttpError;
022import org.apache.tapestry5.services.Traditional;
023import org.slf4j.Logger;
024
025import javax.servlet.http.HttpServletResponse;
026import java.io.IOException;
027
028/**
029 * Default implementation for {@link UnknownActivationContextHandler} which answer with a 404 NOT FOUND error.
030 */
031public class UnknownActivationContextHandlerImpl implements UnknownActivationContextHandler
032{
033    private final Logger logger;
034
035    private final ComponentEventResultProcessor resultProcessor;
036
037
038    public UnknownActivationContextHandlerImpl(Logger logger,
039                                               @Traditional @Primary
040                                               ComponentEventResultProcessor resultProcessor)
041    {
042        this.logger = logger;
043        this.resultProcessor = resultProcessor;
044    }
045
046    public void handleUnknownContext(ComponentResources pageResources, EventContext activationContext)
047            throws IOException
048    {
049        logger.warn("Activate event on page {} was fired with context {} but was not handled",
050                pageResources.getPage().getClass(), activationContext);
051
052        String message = String.format("Activation context %s unrecognized for page %s",
053                activationContext, pageResources.getPage().getClass());
054
055        resultProcessor.processResultValue(new HttpError(HttpServletResponse.SC_NOT_FOUND, message));
056    }
057}