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