001    /**
002     * Copyright (c) 2000-2013 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.events;
016    
017    import com.liferay.portal.kernel.events.Action;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.struts.LastPath;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PrefsPropsUtil;
030    import com.liferay.portal.util.WebKeys;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    import javax.servlet.http.HttpSession;
035    
036    /**
037     * @author Michael Young
038     */
039    public class DefaultLandingPageAction extends Action {
040    
041            @Override
042            public void run(HttpServletRequest request, HttpServletResponse response)
043                    throws ActionException {
044    
045                    try {
046                            doRun(request, response);
047                    }
048                    catch (Exception e) {
049                            throw new ActionException(e);
050                    }
051            }
052    
053            protected void doRun(
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    long companyId = PortalUtil.getCompanyId(request);
058    
059                    String path = PrefsPropsUtil.getString(
060                            companyId, PropsKeys.DEFAULT_LANDING_PAGE_PATH);
061    
062                    if (_log.isInfoEnabled()) {
063                            _log.info(
064                                    PropsKeys.DEFAULT_LANDING_PAGE_PATH + StringPool.EQUAL + path);
065                    }
066    
067                    if (Validator.isNull(path)) {
068                            return;
069                    }
070    
071                    HttpSession session = request.getSession();
072    
073                    if (path.contains("${liferay:screenName}") ||
074                            path.contains("${liferay:userId}")) {
075    
076                            User user = (User)session.getAttribute(WebKeys.USER);
077    
078                            if (user == null) {
079                                    return;
080                            }
081    
082                            path = StringUtil.replace(
083                                    path,
084                                    new String[] {"${liferay:screenName}", "${liferay:userId}"},
085                                    new String[] {
086                                            HtmlUtil.escapeURL(user.getScreenName()),
087                                            String.valueOf(user.getUserId())
088                                    });
089                    }
090    
091                    LastPath lastPath = new LastPath(StringPool.BLANK, path);
092    
093                    session.setAttribute(WebKeys.LAST_PATH, lastPath);
094    
095                    // The commented code shows how you can programmaticaly set the user's
096                    // landing page. You can modify this class to utilize a custom algorithm
097                    // for forwarding a user to his landing page. See the references to this
098                    // class in portal.properties.
099    
100                    /*Map<String, String[]> params = new HashMap<String, String[]>();
101    
102                    params.put("p_l_id", new String[] {"1806"});
103    
104                    LastPath lastPath = new LastPath("/c", "/portal/layout", params);
105    
106                    session.setAttribute(WebKeys.LAST_PATH, lastPath);*/
107            }
108    
109            private static Log _log = LogFactoryUtil.getLog(
110                    DefaultLandingPageAction.class);
111    
112    }