001    /**
002     * Copyright (c) 2000-2013 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.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                    String portalURL = PortalUtil.getPortalURL(_request);
045    
046                    if (redirect.charAt(0) == CharPool.SLASH) {
047                            if (Validator.isNotNull(portalURL)) {
048                                    redirect = portalURL.concat(redirect);
049                            }
050                    }
051    
052                    if (!CookieKeys.hasSessionId(_request) &&
053                            redirect.startsWith(portalURL)) {
054    
055                            redirect = PortalUtil.getURLWithSessionId(
056                                    redirect, _request.getSession().getId());
057                    }
058    
059                    _request.setAttribute(
060                            AbsoluteRedirectsResponse.class.getName(), redirect);
061    
062                    super.sendRedirect(redirect);
063            }
064    
065            private HttpServletRequest _request;
066    
067    }