001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Michal Obrebski - Nuxeo 016 */ 017 018package org.nuxeo.easyshare; 019 020import javax.security.auth.login.LoginContext; 021import javax.security.auth.login.LoginException; 022import javax.servlet.http.HttpServletRequest; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.CoreInstance; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.IdRef; 029import org.nuxeo.ecm.webengine.jaxrs.context.RequestCleanupHandler; 030import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 031import org.nuxeo.runtime.api.Framework; 032 033public abstract class EasyShareUnrestrictedRunner { 034 035 private static final Log log = LogFactory.getLog(EasyShareUnrestrictedRunner.class); 036 037 protected CoreSession session; 038 039 public Object runUnrestricted(String docId) { 040 final LoginContext lc; 041 try { 042 lc = Framework.login(); 043 } catch (LoginException ex) { 044 log.error("Unable to render page", ex); 045 return null; 046 } 047 CoreSession coreSession = null; 048 try { 049 coreSession = CoreInstance.openCoreSession(null); 050 051 // Run unrestricted operation 052 IdRef docRef = new IdRef(docId); 053 return run(coreSession, docRef); 054 055 } finally { 056 final CoreSession session2close = coreSession; 057 RequestContext.getActiveContext().addRequestCleanupHandler(new RequestCleanupHandler() { 058 059 @Override 060 public void cleanup(HttpServletRequest req) { 061 try { 062 session2close.close(); 063 lc.logout(); 064 } catch (LoginException e) { 065 log.error("Error during request context cleanup", e); 066 } 067 } 068 }); 069 070 } 071 072 } 073 074 public abstract Object run(CoreSession coreSession, IdRef docId); 075}