001/*
002 *  Copyright 2017 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.servletwrapper.servlet;
017
018import java.io.IOException;
019import java.util.Map;
020
021import javax.servlet.Servlet;
022import javax.servlet.ServletContext;
023import javax.servlet.ServletException;
024import javax.servlet.http.HttpServletRequest;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.http.HttpEnvironment;
029/**
030 * This class is a generic servlet wrapping an actual {@link Servlet}. 
031 * {@link ServletWrapper} are used to handle servlets from inside the application, instead of inside the servlet engine.
032 */
033public class ServletWrapper
034{
035    private Servlet _servlet;
036
037    /**
038     * Constructor
039     * @param servlet A servlet to apply
040     */
041    public ServletWrapper(Servlet servlet)
042    {
043        _servlet = servlet;
044    }
045    
046    /**
047     * Initialize the servlet in the wrapper
048     * @param servletName Name
049     * @param servletContext Context
050     * @param parameters Parameters
051     * @throws ServletException Exception if servlet init fails
052     */
053    public void init(String servletName, ServletContext servletContext, Map<String, String> parameters) throws ServletException
054    {
055        try
056        {
057            _servlet.init(new ServletWrapperConfig(servletName, servletContext, parameters));
058        }
059        catch (Exception e)
060        {
061            throw new ServletException("Impossible to initialize the filter.", e);
062        }
063    }
064    
065    /**
066     * Calls service in the servlet, wrapping the request and the response
067     * @param objectModel object model
068     * @param redirect redirector
069     * @throws ServletException exception thrown by the servlet
070     * @throws IOException exception thrown by the servlet
071     */
072    public void service(Map objectModel, Redirector redirect) throws ServletException, IOException
073    {
074        HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
075        HttpServletResponse response = (HttpServletResponse) objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
076        this.service(request, response, objectModel, redirect);
077    }
078    
079    /**
080     * Calls service in the servlet
081     * @param request request
082     * @param response response
083     * @param objectModel object model
084     * @param redirect redirector
085     * @throws ServletException exception thrown by the servlet
086     * @throws IOException exception thrown by the servlet
087     */
088    public void service(HttpServletRequest request, HttpServletResponse response, Map objectModel, Redirector redirect) throws ServletException, IOException
089    {
090        ServletWrapperResponse filteredResponse = new ServletWrapperResponse(response, redirect);
091        
092        _servlet.service(request, filteredResponse);
093    }
094}