    001package org.apache.turbine.modules.screens;
    002
    003/*
    004 * Licensed to the Apache Software Foundation (ASF) under one
    005 * or more contributor license agreements.  See the NOTICE file
    006 * distributed with this work for additional information
    007 * regarding copyright ownership.  The ASF licenses this file
    008 * to you under the Apache License, Version 2.0 (the
    009 * "License"); you may not use this file except in compliance
    010 * with the License.  You may obtain a copy of the License at
    011 *
    012 *   http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing,
    015 * software distributed under the License is distributed on an
    016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    017 * KIND, either express or implied.  See the License for the
    018 * specific language governing permissions and limitations
    019 * under the License.
    020 */
    021
    022
    023// Turbine stuff.
    024
    025import org.apache.turbine.modules.Screen;
    026import org.apache.turbine.pipeline.PipelineData;
    027import org.apache.turbine.util.RunData;
    028
    029/**
    030 * Base class for writing Screens that output binary data.  This class
    031 * is provided as a helper class for those who want to write Screens
    032 * that output raw binary data.  For example, it may be extended into
    033 * a Screen that outputs a SVG file or a SWF (Flash Player format)
    034 * movie.  The only thing one has to do is to implement the two
    035 * methods <code>getContentType(PipelineData data)</code> and
    036 * <code>doOutput(PipelineData data)</code> (see below).
    037 *
    038 * <p> You might want to take a look at the ImageServer screen class
    039 * contained in the TDK.<br>
    040 *
    041 * @author <a href="mailto:rkoenig@chez.com">Regis Koenig</a>
    042 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    043 * @version $Id: RawScreen.java 1773378 2016-12-09 13:19:59Z tv $
    044 */
    045public abstract class RawScreen extends Screen
    046{
    047    /**
    048     * Build the Screen.  This method actually makes a call to the
    049     * doOutput() method in order to generate the Screen content.
    050     *
    051     * @param pipelineData Turbine information.
    052     * @return A ConcreteElement.
    053     * @throws Exception a generic exception.
    054     */
    055    @Override
    056    protected final String doBuild(PipelineData pipelineData)
    057            throws Exception
    058    {
    059        RunData data = getRunData(pipelineData);
    060        data.getResponse().setContentType(getContentType(pipelineData));
    061        data.declareDirectResponse();
    062        doOutput(pipelineData);
    063        return null;
    064    }
    065
    066    /**
    067     * Set the content type.  This method should be overridden to
    068     * actually set the real content-type header of the output.
    069     *
    070     * @param pipelineData Turbine information.
    071     * @return A String with the content type.
    072     */
    073    protected abstract String getContentType(PipelineData pipelineData);
    074
    075    /**
    076     * Actually output the dynamic content.  The OutputStream can be
    077     * accessed like this: <pre>OutputStream out =
    078     * data.getResponse().getOutputStream();</pre>.
    079     *
    080     * @param pipelineData Turbine information.
    081     * @throws Exception a generic exception.
    082     */
    083    protected abstract void doOutput(PipelineData pipelineData) throws Exception;
    084
    085    /**
    086     * The layout must be set to null.
    087     *
    088     * @param pipelineData Turbine information.
    089     * @return A null String.
    090     */
    091    @Override
    092    public final String getLayout(PipelineData pipelineData)
    093    {
    094        return null;
    095    }
    096}