001    /**
002     * Copyright (c) 2000-2012 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            public void applyAction(
037                            MDRAction mdrAction, HttpServletRequest request,
038                            HttpServletResponse response)
039                    throws PortalException, SystemException {
040    
041                    String url = getURL(mdrAction, request, response);
042    
043                    if (Validator.isNull(url)) {
044                            if (_log.isInfoEnabled()) {
045                                    _log.info("URL is null");
046                            }
047    
048                            return;
049                    }
050    
051                    String requestURL = String.valueOf(request.getRequestURL());
052    
053                    if (StringUtil.contains(requestURL, url)) {
054                            if (_log.isInfoEnabled()) {
055                                    _log.info(
056                                            "Skipping redirect. Current URL contains redirect URL.");
057                            }
058    
059                            return;
060                    }
061    
062                    try {
063                            response.sendRedirect(url);
064                    }
065                    catch (IOException ioe) {
066                            throw new PortalException("Unable to redirect to " + url, ioe);
067                    }
068            }
069    
070            protected abstract String getURL(
071                            MDRAction mdrAction, HttpServletRequest request,
072                            HttpServletResponse response)
073                    throws PortalException, SystemException;
074    
075            private static Log _log = LogFactoryUtil.getLog(
076                    BaseRedirectActionHandler.class);
077    
078    }