001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
068                                            layout, themeDisplay);
069                            }
070    
071                            if (Validator.isNull(doAsURL)) {
072                                    doAsURL = _COMPANY_DEFAULT_HOME_URL;
073                            }
074                    }
075    
076                    doAsURL = themeDisplay.getPathContext() + doAsURL;
077    
078                    if (doAsUserId <= 0) {
079                            doAsUserId = company.getDefaultUser().getUserId();
080                    }
081    
082                    String encDoAsUserId = Encryptor.encrypt(
083                            company.getKeyObj(), String.valueOf(doAsUserId));
084    
085                    doAsURL = HttpUtil.addParameter(
086                            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 =
119                    PropsUtil.get(PropsKeys.COMPANY_DEFAULT_HOME_URL);
120    
121            private long _doAsUserId;
122            private String _var;
123    
124    }