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.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    }