001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.generation;
017
018import java.io.File;
019import java.io.IOException;
020import java.net.MalformedURLException;
021import java.util.Collections;
022
023import org.apache.avalon.framework.logger.Logger;
024import org.apache.cocoon.environment.AbstractEnvironment;
025import org.apache.cocoon.environment.Context;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.commandline.CommandLineRequest;
029import org.apache.cocoon.environment.commandline.CommandLineResponse;
030import org.apache.cocoon.util.NullOutputStream;
031
032/**
033 * A simple implementation of <code>org.apache.cocoon.environment.Environment</code> for pipeline
034 * calls which are not externally triggered.
035 */
036public class GenerationEnvironment extends AbstractEnvironment
037{
038    /**
039     * Creates a generation environment.
040     * @param logger the logger.
041     * @param ctx the context
042     * @param targetContextPath the path to the context of the server where the generation will be deployed.
043     * @throws MalformedURLException if the environment cannot be initialized with the context.
044     */
045    public GenerationEnvironment(Logger logger, Context ctx, String targetContextPath) throws MalformedURLException
046    {
047        super("", null, new File(ctx.getRealPath("/")), null);
048        enableLogging(logger);
049        this.outputStream = new NullOutputStream();
050        Request request = new CommandLineRequest(this, targetContextPath, "", "", null, Collections.EMPTY_MAP, Collections.EMPTY_MAP);
051        objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
052        objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, new CommandLineResponse());
053        objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, ctx);
054    }
055
056    @Override
057    public void redirect(boolean sessionmode, String newURL) throws IOException
058    {
059        throw new UnsupportedOperationException("Redirection is not supported");
060    }
061
062    @Override
063    public void setContentType(String mimeType)
064    {
065        // not handled
066    }
067
068    @Override
069    public String getContentType()
070    {
071        return null;
072    }
073
074    @Override
075    public void setContentLength(int length)
076    {
077        // not handled
078    }
079
080    @Override
081    public boolean isExternal()
082    {
083        return false;
084    }
085}