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.portal.servlet.filters.absoluteredirects;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.util.CookieKeys;
020    import com.liferay.portal.util.PortalUtil;
021    
022    import java.io.IOException;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.http.HttpServletResponse;
026    import javax.servlet.http.HttpServletResponseWrapper;
027    
028    /**
029     * @author Jorge Ferrer
030     * @author Shuyang Zhou
031     */
032    public class AbsoluteRedirectsResponse extends HttpServletResponseWrapper {
033    
034            public AbsoluteRedirectsResponse(
035                    HttpServletRequest request, HttpServletResponse response) {
036    
037                    super(response);
038    
039                    _request = request;
040            }
041    
042            @Override
043            public void sendRedirect(String redirect) throws IOException {
044                    if (redirect.charAt(0) == CharPool.SLASH) {
045                            String portalURL = PortalUtil.getPortalURL(_request);
046    
047                            if (Validator.isNotNull(portalURL)) {
048                                    redirect = portalURL.concat(redirect);
049                            }
050                    }
051    
052                    if (!CookieKeys.hasSessionId(_request)) {
053                            redirect = PortalUtil.getURLWithSessionId(
054                                    redirect, _request.getSession().getId());
055                    }
056    
057                    _request.setAttribute(
058                            AbsoluteRedirectsResponse.class.getName(), redirect);
059    
060                    super.sendRedirect(redirect);
061            }
062    
063            private HttpServletRequest _request;
064    
065    }