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