001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.security.auto.login;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.util.PortalUtil;
022    
023    import javax.servlet.http.HttpServletRequest;
024    import javax.servlet.http.HttpServletResponse;
025    
026    /**
027     * @author Mate Thurzo
028     */
029    public abstract class BaseAutoLogin implements AutoLogin {
030    
031            @Override
032            public String[] handleException(
033                            HttpServletRequest request, HttpServletResponse response,
034                            Exception e)
035                    throws AutoLoginException {
036    
037                    return doHandleException(request, response, e);
038            }
039    
040            @Override
041            public String[] login(
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws AutoLoginException {
044    
045                    try {
046                            return doLogin(request, response);
047                    }
048                    catch (Exception e) {
049                            return handleException(request, response, e);
050                    }
051            }
052    
053            protected void addRedirect(HttpServletRequest request) {
054                    String redirect = ParamUtil.getString(request, "redirect");
055    
056                    if (Validator.isNotNull(redirect)) {
057                            request.setAttribute(
058                                    AUTO_LOGIN_REDIRECT_AND_CONTINUE,
059                                    PortalUtil.escapeRedirect(redirect));
060                    }
061            }
062    
063            protected String[] doHandleException(
064                            HttpServletRequest request, HttpServletResponse response,
065                            Exception e)
066                    throws AutoLoginException {
067    
068                    if (request.getAttribute(AUTO_LOGIN_REDIRECT) == null) {
069                            throw new AutoLoginException(e);
070                    }
071    
072                    _log.error(e, e);
073    
074                    return null;
075            }
076    
077            protected abstract String[] doLogin(
078                            HttpServletRequest request, HttpServletResponse response)
079                    throws Exception;
080    
081            private static final Log _log = LogFactoryUtil.getLog(BaseAutoLogin.class);
082    
083    }