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.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.cocoon.environment.Request;
023import org.apache.cocoon.environment.Session;
024import org.apache.cocoon.environment.commandline.CommandLineRequest;
025import org.apache.cocoon.environment.commandline.CommandLineSession;
026
027/**
028 * Simple {@link Request} used for internal requests.
029 */
030public class BackgroundRequest extends CommandLineRequest
031{
032    private Session _session;
033    
034    /**
035     * Constructs this simple request.
036     * @param env the parent environment.
037     */
038    public BackgroundRequest(BackgroundEnvironment env)
039    {
040        super(
041            env,                   // environment
042            "",                    // context path
043            "",                    // servlet path
044            "",                    // path info
045            new HashMap(),         // attributes
046            Collections.EMPTY_MAP, // parameters
047            Collections.EMPTY_MAP  // headers
048        );
049    }
050    
051    /**
052     * Constructs this simple request.
053     * @param env the parent environment.
054     * @param contextPath the context path
055     * @param requestAttributes request attributes.
056     * @param requestParameters request parameters.
057     * @param requestHeaders request headers.
058     */
059    public BackgroundRequest(BackgroundEnvironment env, String contextPath, Map requestAttributes, Map requestParameters, Map requestHeaders)
060    {
061        super(
062            env,                   // environment
063            contextPath,           // context path
064            "",                    // servlet path
065            "",                    // path info
066            requestAttributes,     // attributes
067            requestParameters,     // parameters
068            requestHeaders         // headers
069        );
070    }
071
072    @Override
073    public Session getSession(boolean create)
074    {
075        if (_session != null)
076        {
077            return _session;
078        }
079        
080        if (create)
081        {
082            _session = new CommandLineSession();
083        }
084        
085        return _session;
086    }
087}