001// Copyright 2010-2014 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.EventContext;
018import org.apache.tapestry5.TapestryConstants;
019import org.apache.tapestry5.beanmodel.services.*;
020import org.apache.tapestry5.commons.services.TypeCoercer;
021import org.apache.tapestry5.http.services.Request;
022import org.apache.tapestry5.http.services.RequestGlobals;
023import org.apache.tapestry5.internal.EmptyEventContext;
024import org.apache.tapestry5.internal.InternalConstants;
025import org.apache.tapestry5.ioc.IOOperation;
026import org.apache.tapestry5.services.ComponentClassResolver;
027import org.apache.tapestry5.services.ComponentEventResultProcessor;
028import org.apache.tapestry5.services.PageRenderRequestHandler;
029import org.apache.tapestry5.services.PageRenderRequestParameters;
030import org.apache.tapestry5.services.StreamPageContent;
031
032import java.io.IOException;
033
034/**
035 * Used to trigger the rendering of a particular page without causing a redirect to that page.
036 * The content of the page is just streamed to the client.
037 *
038 * @since 5.2.0
039 */
040public class StreamPageContentResultProcessor implements ComponentEventResultProcessor<StreamPageContent>
041{
042    private final PageRenderRequestHandler handler;
043
044    private final ComponentClassResolver resolver;
045
046    private final TypeCoercer typeCoercer;
047
048    private final RequestGlobals requestGlobals;
049
050    private final Request request;
051
052    public StreamPageContentResultProcessor(PageRenderRequestHandler handler, ComponentClassResolver resolver, TypeCoercer typeCoercer, RequestGlobals requestGlobals, Request request)
053    {
054        this.handler = handler;
055        this.resolver = resolver;
056        this.typeCoercer = typeCoercer;
057        this.requestGlobals = requestGlobals;
058        this.request = request;
059    }
060
061    public void processResultValue(StreamPageContent value) throws IOException
062    {
063        Class<?> pageClass = value.getPageClass();
064        Object[] activationContext = value.getPageActivationContext();
065
066        final String pageName = pageClass == null
067                ? requestGlobals.getActivePageName()
068                : resolver.resolvePageClassNameToPageName(pageClass.getName());
069
070        final EventContext context = activationContext == null
071                ? new EmptyEventContext()
072                : new ArrayEventContext(typeCoercer, activationContext);
073
074        if (value.isBypassActivation())
075        {
076            request.setAttribute(InternalConstants.BYPASS_ACTIVATION, true);
077        }
078
079        request.setAttribute(TapestryConstants.RESPONSE_RENDERER, new IOOperation<Void>()
080        {
081            public Void perform() throws IOException
082            {
083                handler.handle(new PageRenderRequestParameters(pageName, context, false));
084
085                return null;
086            }
087        });
088    }
089}