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 =
072                                            themeDisplay.getPathContext() + _COMPANY_DEFAULT_HOME_URL;
073                            }
074                    }
075                    else {
076                            doAsURL = themeDisplay.getPathContext() + doAsURL;
077                    }
078    
079                    if (doAsUserId <= 0) {
080                            doAsUserId = company.getDefaultUser().getUserId();
081                    }
082    
083                    String encDoAsUserId = Encryptor.encrypt(
084                            company.getKeyObj(), String.valueOf(doAsUserId));
085    
086                    doAsURL = HttpUtil.addParameter(doAsURL, "doAsUserId", encDoAsUserId);
087    
088                    if (Validator.isNotNull(var)) {
089                            pageContext.setAttribute(var, doAsURL);
090                    }
091                    else {
092                            JspWriter jspWriter = pageContext.getOut();
093    
094                            jspWriter.write(doAsURL);
095                    }
096            }
097    
098            @Override
099            public int doEndTag() throws JspException {
100                    try {
101                            doTag(_doAsUserId, _var, pageContext);
102                    }
103                    catch (Exception e) {
104                            throw new JspException(e);
105                    }
106    
107                    return EVAL_PAGE;
108            }
109    
110            public void setDoAsUserId(long doAsUserId) {
111                    _doAsUserId = doAsUserId;
112            }
113    
114            public void setVar(String var) {
115                    _var = var;
116            }
117    
118            private static final String _COMPANY_DEFAULT_HOME_URL = PropsUtil.get(
119                    PropsKeys.COMPANY_DEFAULT_HOME_URL);
120    
121            private long _doAsUserId;
122            private String _var;
123    
124    }