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