001    /**
002     * Copyright (c) 2000-2012 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.taglib.security;
016    
017    import com.liferay.portal.kernel.util.HttpUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.util.Encryptor;
029    
030    import java.util.List;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.jsp.JspException;
034    import javax.servlet.jsp.JspWriter;
035    import javax.servlet.jsp.PageContext;
036    import javax.servlet.jsp.tagext.TagSupport;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class DoAsURLTag extends TagSupport {
042    
043            public static void doTag(
044                            long doAsUserId, String var, PageContext pageContext)
045                    throws Exception {
046    
047                    HttpServletRequest request =
048                            (HttpServletRequest)pageContext.getRequest();
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    Company company = themeDisplay.getCompany();
054    
055                    String doAsURL = company.getHomeURL();
056    
057                    if (Validator.isNull(doAsURL)) {
058                            Layout layout = null;
059    
060                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
061                                    themeDisplay.getScopeGroupId(), false,
062                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
063    
064                            if (!layouts.isEmpty()) {
065                                    layout = layouts.get(0);
066    
067                                    doAsURL = PortalUtil.getLayoutFriendlyURL(layout, themeDisplay);
068                            }
069    
070                            if (Validator.isNull(doAsURL)) {
071                                    doAsURL = _COMPANY_DEFAULT_HOME_URL;
072                            }
073                    }
074    
075                    doAsURL = themeDisplay.getPathContext() + doAsURL;
076    
077                    if (doAsUserId <= 0) {
078                            doAsUserId = company.getDefaultUser().getUserId();
079                    }
080    
081                    String encDoAsUserId = Encryptor.encrypt(
082                            company.getKeyObj(), String.valueOf(doAsUserId));
083    
084                    doAsURL = HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
085    
086                    if (Validator.isNotNull(var)) {
087                            pageContext.setAttribute(var, doAsURL);
088                    }
089                    else {
090                            JspWriter jspWriter = pageContext.getOut();
091    
092                            jspWriter.write(doAsURL);
093                    }
094            }
095    
096            @Override
097            public int doEndTag() throws JspException {
098                    try {
099                            doTag(_doAsUserId, _var, pageContext);
100                    }
101                    catch (Exception e) {
102                            throw new JspException(e);
103                    }
104    
105                    return EVAL_PAGE;
106            }
107    
108            public void setDoAsUserId(long doAsUserId) {
109                    _doAsUserId = doAsUserId;
110            }
111    
112            public void setVar(String var) {
113                    _var = var;
114            }
115    
116            private static final String _COMPANY_DEFAULT_HOME_URL = PropsUtil.get(
117                    PropsKeys.COMPANY_DEFAULT_HOME_URL);
118    
119            private long _doAsUserId;
120            private String _var;
121    
122    }