001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.kernel.util.StringPool;
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     */
031    public class AbsoluteRedirectsResponse extends HttpServletResponseWrapper {
032    
033            public AbsoluteRedirectsResponse(
034                    HttpServletRequest request, HttpServletResponse response) {
035    
036                    super(response);
037    
038                    _request = request;
039            }
040    
041            public void sendRedirect(String redirect) throws IOException {
042                    String portalURL = getPortalURL();
043    
044                    if (Validator.isNotNull(portalURL) &&
045                            redirect.startsWith(StringPool.SLASH)) {
046    
047                            redirect = portalURL + redirect;
048                    }
049    
050                    if (!CookieKeys.hasSessionId(_request)) {
051                            redirect = PortalUtil.getURLWithSessionId(
052                                    redirect, _request.getSession().getId());
053                    }
054    
055                    _request.setAttribute(
056                            AbsoluteRedirectsResponse.class.getName(), redirect);
057    
058                    super.sendRedirect(redirect);
059            }
060    
061            protected String getPortalURL() {
062                    return PortalUtil.getPortalURL(_request);
063            }
064    
065            private HttpServletRequest _request;
066    
067    }