001/*
002 * (C) Copyright 2012 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 *     bjalon
018 */
019package org.nuxeo.ecm.mobile.webengine;
020
021import javax.servlet.http.Cookie;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.Produces;
027import javax.ws.rs.core.Context;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.platform.ui.web.auth.service.PluggableAuthenticationService;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Manage authentication form and logout action
038 *
039 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
040 * @since 5.5
041 */
042@WebObject(type = "WebMobileAuthentication")
043@Produces("text/html;charset=UTF-8")
044public class WebMobileAuthentication extends DefaultObject {
045
046    private static final Log log = LogFactory.getLog(WebMobileAuthentication.class);
047
048    private PluggableAuthenticationService service;
049
050    private String nuxeoContextPath;
051
052    @GET
053    @Path("login")
054    public Object doLogin() {
055        return getView("login-mobile");
056    }
057
058    @GET
059    @Path("logout")
060    public Object doLogout(@Context HttpServletResponse response, @Context HttpServletRequest request) {
061
062        Cookie cookie = new Cookie("JSESSIONID", null);
063        cookie.setMaxAge(0);
064        cookie.setPath("/");
065
066        response.addCookie(cookie);
067        getService().invalidateSession(request);
068
069        return redirect(getNuxeoContextPath());
070    }
071
072    private String getNuxeoContextPath() {
073        if (nuxeoContextPath == null) {
074            nuxeoContextPath = Framework.getProperty("org.nuxeo.ecm.contextPath");
075        }
076        return nuxeoContextPath;
077    }
078
079    private PluggableAuthenticationService getService() {
080        if (service == null && Framework.getRuntime() != null) {
081            service = (PluggableAuthenticationService) Framework.getRuntime().getComponent(
082                    PluggableAuthenticationService.NAME);
083            // init preFilters
084            service.initPreFilters();
085        }
086        return service;
087
088    }
089
090}