001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Michal Obrebski - Nuxeo
018 */
019
020package org.nuxeo.easyshare;
021
022import javax.servlet.http.HttpServletRequest;
023
024import org.nuxeo.ecm.core.api.CoreInstance;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.IdRef;
027import org.nuxeo.ecm.platform.web.common.RequestCleanupHandler;
028import org.nuxeo.ecm.platform.web.common.RequestContext;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.api.login.NuxeoLoginContext;
031
032public abstract class EasyShareUnrestrictedRunner {
033
034    protected CoreSession session;
035
036    public Object runUnrestricted(String docId) {
037        NuxeoLoginContext loginContext = Framework.loginSystem();
038        try {
039            CoreSession coreSession = CoreInstance.getCoreSession(null);
040
041            // Run unrestricted operation
042            IdRef docRef = new IdRef(docId);
043            return run(coreSession, docRef);
044
045        } finally {
046            RequestContext.getActiveContext().addRequestCleanupHandler(new RequestCleanupHandler() {
047
048                @Override
049                public void cleanup(HttpServletRequest req) {
050                    loginContext.close();
051                }
052            });
053
054        }
055
056    }
057
058    public abstract Object run(CoreSession coreSession, IdRef docId);
059}