001/*
002 * (C) Copyright ${year} Nuxeo SA (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 *     bjalon
016 */
017
018package org.nuxeo.application.definition;
019
020import javax.servlet.http.Cookie;
021import javax.servlet.http.HttpServletResponse;
022import javax.ws.rs.GET;
023import javax.ws.rs.Path;
024import javax.ws.rs.Produces;
025import javax.ws.rs.core.Context;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
032import org.nuxeo.runtime.api.Framework;
033
034import static org.nuxeo.ecm.platform.ui.web.auth.NXAuthConstants.REQUESTED_URL;
035
036/**
037 * The root entry for the WebEngine module.
038 *
039 * @author bjalon
040 */
041@Path("/myApplication")
042@Produces("text/html;charset=UTF-8")
043@WebObject(type = "MyApplication")
044public class MyApplication extends ModuleRoot {
045    private static final Log log = LogFactory.getLog(MyApplication.class);
046
047    private PluggableAuthenticationService service;
048
049    @GET
050    public Object doGet() {
051        return getView("index");
052    }
053
054    private PluggableAuthenticationService getService() {
055        if (service == null && Framework.getRuntime() != null) {
056            service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(
057                    PluggableAuthenticationService.NAME);
058            // init preFilters
059            service.initPreFilters();
060        }
061        return service;
062
063    }
064
065    @GET
066    @Path("login")
067    public Object doLogin() {
068        return getView("login");
069    }
070
071    @GET
072    @Path("logout")
073    public Object doLogout(@Context HttpServletResponse response) {
074
075        Cookie cookie = new Cookie("JSESSIONID", null);
076        cookie.setMaxAge(0);
077        cookie.setPath("/");
078
079        response.addCookie(cookie);
080        getService().invalidateSession(request);
081
082        String redirect = request.getParameter(REQUESTED_URL);
083        if (redirect != null) {
084            log.debug("Logout done: Redirect to default URL: " + redirect);
085        } else {
086            redirect = getContext().getBasePath();
087        }
088        return redirect(redirect);
089    }
090}