    001    // Copyright 2010, 2011 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
    015    package org.apache.tapestry5.internal.services;
    016
    017    import java.io.IOException;
    018
    019    import org.apache.tapestry5.EventContext;
    020    import org.apache.tapestry5.internal.EmptyEventContext;
    021    import org.apache.tapestry5.internal.InternalConstants;
    022    import org.apache.tapestry5.ioc.annotations.Inject;
    023    import org.apache.tapestry5.ioc.services.TypeCoercer;
    024    import org.apache.tapestry5.services.ComponentClassResolver;
    025    import org.apache.tapestry5.services.ComponentEventResultProcessor;
    026    import org.apache.tapestry5.services.PageRenderRequestHandler;
    027    import org.apache.tapestry5.services.PageRenderRequestParameters;
    028    import org.apache.tapestry5.services.Request;
    029    import org.apache.tapestry5.services.StreamPageContent;
    030
    031    /**
    032     * Used to trigger the rendering of a particular page without causing a redirect to that page.
    033     * The content of the page is just streamed to the client.
    034     *
    035     * @since 5.2.0
    036     */
    037    public class StreamPageContentResultProcessor implements ComponentEventResultProcessor<StreamPageContent>
    038    {
    039        private final PageRenderRequestHandler handler;
    040
    041        private final ComponentClassResolver resolver;
    042
    043        private final TypeCoercer typeCoercer;
    044
    045        public StreamPageContentResultProcessor(PageRenderRequestHandler handler, ComponentClassResolver resolver, TypeCoercer typeCoercer)
    046        {
    047            this.handler = handler;
    048            this.resolver = resolver;
    049            this.typeCoercer = typeCoercer;
    050        }
    051
    052        public void processResultValue(final StreamPageContent value) throws IOException
    053        {
    054
    055            final Class<?> pageClass = value.getPageClass();
    056            final Object[] activationContext = value.getPageActivationContext();
    057
    058            final String pageName = this.resolver.resolvePageClassNameToPageName(pageClass.getName());
    059
    060            final EventContext context = activationContext == null ? new EmptyEventContext() : new ArrayEventContext(
    061                    this.typeCoercer, activationContext);
    062
    063            this.handler.handle(new PageRenderRequestParameters(pageName, context, false));
    064        }
    065    }