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