001// Copyright 2006, 2007, 2008, 2009, 2010 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 java.io.IOException;
018
019import org.apache.tapestry5.internal.structure.Page;
020import org.apache.tapestry5.ioc.annotations.Primary;
021import org.apache.tapestry5.services.ComponentEventResultProcessor;
022import org.apache.tapestry5.services.PageRenderRequestHandler;
023import org.apache.tapestry5.services.PageRenderRequestParameters;
024import org.apache.tapestry5.services.Traditional;
025
026/**
027 * Handles a page render request by activating and then rendering the page.
028 * 
029 * @see org.apache.tapestry5.internal.services.PageRenderDispatcher
030 */
031@SuppressWarnings("unchecked")
032public class PageRenderRequestHandlerImpl implements PageRenderRequestHandler
033{
034    private final RequestPageCache cache;
035
036    private final ComponentEventResultProcessor resultProcessor;
037
038    private final PageResponseRenderer pageResponseRenderer;
039
040    private final PageActivator pageActivator;
041
042    public PageRenderRequestHandlerImpl(RequestPageCache cache, @Traditional
043    @Primary
044    ComponentEventResultProcessor resultProcessor, PageResponseRenderer pageResponseRenderer,
045            PageActivator pageActivator)
046    {
047        this.cache = cache;
048        this.resultProcessor = resultProcessor;
049        this.pageResponseRenderer = pageResponseRenderer;
050        this.pageActivator = pageActivator;
051    }
052
053    public void handle(PageRenderRequestParameters parameters) throws IOException
054    {
055        Page page = cache.get(parameters.getLogicalPageName());
056
057        if (pageActivator.activatePage(page.getRootElement().getComponentResources(),
058                parameters.getActivationContext(), resultProcessor))
059            return;
060
061        if (!parameters.isLoopback())
062            page.pageReset();
063
064        pageResponseRenderer.renderPageResponse(page);
065    }
066}