001/*
002 *  Copyright 2020 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.core.engine;
017
018import java.io.File;
019import java.io.IOException;
020import java.io.OutputStream;
021import java.net.MalformedURLException;
022import java.util.Map;
023
024import org.apache.avalon.framework.logger.Logger;
025import org.apache.cocoon.environment.AbstractEnvironment;
026import org.apache.cocoon.environment.Context;
027import org.apache.cocoon.environment.ObjectModelHelper;
028import org.apache.cocoon.environment.Request;
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> 
034 * for pipeline calls which are not externally triggered.
035 */
036public class BackgroundEnvironment extends AbstractEnvironment 
037{
038    /**
039     * Constructs the environment.
040     * @param logger a logger for traces.
041     * @param ctx the application {@link Context}.
042     * @throws MalformedURLException if the context is ill-formed
043     */
044    public BackgroundEnvironment(Logger logger, Context ctx) throws MalformedURLException 
045    {
046        super("", null, new File(ctx.getRealPath("/")), null);
047        enableLogging(logger);
048
049        this.outputStream = new NullOutputStream();
050
051        Request request = new BackgroundRequest(this);
052        
053        this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
054        this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, new CommandLineResponse());
055        this.objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, ctx);
056    }
057
058    /**
059     * Constructs the environment.
060     * @param uri the processed uri.
061     * @param requestParameters request parameters.
062     * @param requestAttributes request attributes.
063     * @param requestHeaders request headers.
064     * @param contextPath the application context path.
065     * @param ctx the application {@link Context}.
066     * @param logger a logger for traces.
067     * @param os the {@link OutputStream} to send the response to.
068     * @throws MalformedURLException if the context is ill-formed
069     */
070    public BackgroundEnvironment(String uri, Map requestParameters, Map requestAttributes, Map requestHeaders, String contextPath, Context ctx, Logger logger, OutputStream os) throws MalformedURLException
071    {
072        super(uri, null, new File(ctx.getRealPath("/")), null);
073        enableLogging(logger);
074        
075        Request request = new BackgroundRequest(this, contextPath, requestAttributes, requestParameters, requestHeaders);
076        
077        objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
078        objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, new CommandLineResponse());
079        objectModel.put(ObjectModelHelper.CONTEXT_OBJECT, ctx);
080        
081        outputStream = os;
082    }
083    
084    public void redirect(boolean sessionmode, String newURL) throws IOException 
085    {
086        // nothing to do
087    }
088
089    public void setContentType(String mimeType) 
090    {
091        // nothing to do
092    }
093
094    public String getContentType() 
095    {
096        return null;
097    }
098
099    public void setContentLength(int length)
100    {
101        // nothing to do
102    }
103
104    public boolean isExternal()
105    {
106        return false;
107    }
108}