001    /**
002     * Copyright (c) 2000-2013 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.mobile.device.rulegroup.action.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
025    
026    import java.io.IOException;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * @author Edward Han
033     */
034    public abstract class BaseRedirectActionHandler implements ActionHandler {
035    
036            @Override
037            public void applyAction(
038                            MDRAction mdrAction, HttpServletRequest request,
039                            HttpServletResponse response)
040                    throws PortalException, SystemException {
041    
042                    String url = getURL(mdrAction, request, response);
043    
044                    if (Validator.isNull(url)) {
045                            if (_log.isInfoEnabled()) {
046                                    _log.info("URL is null");
047                            }
048    
049                            return;
050                    }
051    
052                    String requestURL = String.valueOf(request.getRequestURL());
053    
054                    if (StringUtil.contains(requestURL, url)) {
055                            if (_log.isInfoEnabled()) {
056                                    _log.info(
057                                            "Skipping redirect. Current URL contains redirect URL.");
058                            }
059    
060                            return;
061                    }
062    
063                    try {
064                            response.sendRedirect(url);
065                    }
066                    catch (IOException ioe) {
067                            throw new PortalException("Unable to redirect to " + url, ioe);
068                    }
069            }
070    
071            protected abstract String getURL(
072                            MDRAction mdrAction, HttpServletRequest request,
073                            HttpServletResponse response)
074                    throws PortalException, SystemException;
075    
076            private static Log _log = LogFactoryUtil.getLog(
077                    BaseRedirectActionHandler.class);
078    
079    }