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.plugins.workspaces.cmis;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.servlet.ServletContext;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import org.apache.avalon.framework.parameters.Parameters;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.thread.ThreadSafe;
029import org.apache.chemistry.opencmis.server.impl.CmisRepositoryContextListener;
030import org.apache.chemistry.opencmis.server.impl.atompub.CmisAtomPubServlet;
031import org.apache.cocoon.acting.ServiceableAction;
032import org.apache.cocoon.environment.Redirector;
033import org.apache.cocoon.environment.SourceResolver;
034import org.apache.cocoon.environment.http.HttpEnvironment;
035
036import org.ametys.core.servletwrapper.servlet.ServletWrapper;
037
038/**
039 * {@link ServiceableAction} that will wrap the {@link CmisAtomPubServlet}
040 *
041 */
042public class CmisAction extends ServiceableAction implements ThreadSafe
043{
044    private CmisServiceFactory _workspaceCmisServiceFactory;
045    private ServletWrapper _servletWrapper;
046
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        
052        _workspaceCmisServiceFactory = (CmisServiceFactory) smanager.lookup(CmisServiceFactory.ROLE);
053    }
054
055    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
056    {
057        if (_servletWrapper == null)
058        {
059            ServletContext servletContext = (ServletContext) objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT);
060            servletContext.setAttribute(CmisRepositoryContextListener.SERVICES_FACTORY, _workspaceCmisServiceFactory);
061            
062            CmisAtomPubServlet servlet = new CmisAtomPubServlet();
063            _servletWrapper = new ServletWrapper(servlet);
064            Map<String, String> servletParams = new HashMap<>();
065            servletParams.put("cmisVersion", "1.1");
066            servletParams.put("callContextHandler", "org.apache.chemistry.opencmis.server.shared.BasicAuthCallContextHandler");
067            _servletWrapper.init("cmisatom11", servletContext, servletParams);
068        }
069        
070        HttpServletRequest request = (HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
071        CmisHttpServletRequest wsRequest = new CmisHttpServletRequest(request, source);
072        HttpServletResponse response = (HttpServletResponse) objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
073        
074        _servletWrapper.service(wsRequest, response, objectModel, redirector);
075        return EMPTY_MAP;
076    }
077}