001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 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.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 * anguenot 016 * 017 * $Id: AbstractNuxeoWebService.java 28610 2008-01-09 17:13:52Z sfermigier $ 018 */ 019 020package org.nuxeo.ecm.platform.ws; 021 022import javax.annotation.security.PermitAll; 023import javax.jws.WebMethod; 024import javax.jws.WebParam; 025import javax.security.auth.login.LoginContext; 026import javax.security.auth.login.LoginException; 027 028import org.nuxeo.ecm.core.api.CoreInstance; 029import org.nuxeo.ecm.core.api.CoreSession; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.repository.RepositoryManager; 032import org.nuxeo.ecm.platform.api.ws.BaseNuxeoWebService; 033import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSession; 034import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSessionManager; 035import org.nuxeo.ecm.platform.api.ws.session.impl.WSRemotingSessionImpl; 036import org.nuxeo.ecm.platform.usermanager.UserManager; 037import org.nuxeo.runtime.api.Framework; 038 039/** 040 * Abstract Nuxeo Web Service. 041 * <p> 042 * Extend this if you want to share the Web Service remoting sessions with the other Nuxeo platform web services. 043 * 044 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a> 045 */ 046public abstract class AbstractNuxeoWebService implements BaseNuxeoWebService { 047 048 private static final long serialVersionUID = 5530614356404354863L; 049 050 private WSRemotingSessionManager sessionsManager; 051 052 /** 053 * Returns the platform service that deals with shared Web Service remote sessions. 054 * 055 * @return a <code>WSRemotiungSessionManager</code> service 056 */ 057 protected WSRemotingSessionManager getSessionsManager() { 058 if (sessionsManager == null) { 059 sessionsManager = Framework.getService(WSRemotingSessionManager.class); 060 } 061 return sessionsManager; 062 } 063 064 @PermitAll 065 @WebMethod 066 public String connectOnRepository(@WebParam(name = "userName") String username, 067 @WebParam(name = "password") String password, @WebParam(name = "repositoryName") String repositoryName) 068 { 069 String sid = null; 070 try { 071 // :FIXME: won't work all the time... 072 LoginContext loginContext = Framework.login(); 073 if (repositoryName == null) { 074 RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class); 075 repositoryName = repositoryManager.getDefaultRepositoryName(); 076 } 077 sid = _connect(username, password, repositoryName); 078 loginContext.logout(); 079 } catch (LoginException e) { 080 throw new NuxeoException(e.getMessage(), e); 081 } 082 return sid; 083 } 084 085 @Override 086 @PermitAll 087 @WebMethod 088 public String connect(@WebParam(name = "userName") String username, @WebParam(name = "password") String password) 089 { 090 return connectOnRepository(username, password, null); 091 } 092 093 /** 094 * Internal connect method shared in between above connect() methods. 095 * 096 * @param username the user name. 097 * @param password the user password. 098 * @param repository the repository name. 099 * @return a Nuxeo core session identifier. 100 */ 101 private String _connect(String username, String password, String repositoryName) { 102 // Login before doing anything. 103 login(username, password); 104 CoreSession session = CoreInstance.openCoreSession(repositoryName); 105 String sid = session.getSessionId(); 106 UserManager userMgr = Framework.getService(UserManager.class); 107 WSRemotingSession rs = new WSRemotingSessionImpl(session, userMgr, repositoryName, username, password); 108 getSessionsManager().addSession(sid, rs); 109 return sid; 110 } 111 112 @Override 113 @WebMethod 114 public void disconnect(@WebParam(name = "sessionId") String sid) { 115 WSRemotingSession rs = initSession(sid); 116 rs.getDocumentManager().close(); 117 } 118 119 /** 120 * Initializes a new user session from the credentials bound the Web Service remote session. 121 * 122 */ 123 protected void login(String username, String password) { 124 try { 125 Framework.login(username, password); 126 } catch (LoginException e) { 127 throw new NuxeoException("Login failed for " + username, e); 128 } 129 } 130 131 /** 132 * Initializes a user session. 133 * 134 * @param sid the session identifier. 135 * @return a Web Service remoting session instance. 136 */ 137 protected WSRemotingSession initSession(String sid) { 138 WSRemotingSession rs = getSessionsManager().getSession(sid); 139 if (rs == null) { 140 throw new NuxeoException("Invalid session id: " + sid); 141 } 142 login(rs.getUsername(), rs.getPassword()); 143 return rs; 144 } 145 146}