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.events;
016    
017    import com.liferay.portal.kernel.events.Action;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.mobile.device.Device;
021    import com.liferay.portal.kernel.mobile.device.DeviceDetectionUtil;
022    import com.liferay.portal.kernel.mobile.device.UnknownDevice;
023    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManagerUtil;
024    import com.liferay.portal.kernel.mobile.device.rulegroup.RuleGroupProcessorUtil;
025    import com.liferay.portal.kernel.servlet.NonSerializableObjectHandler;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
030    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
031    import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
032    
033    import java.util.List;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    import javax.servlet.http.HttpSession;
038    
039    /**
040     * @author Edward Han
041     */
042    public class DeviceServicePreAction extends Action {
043    
044            @Override
045            public void run(HttpServletRequest request, HttpServletResponse response) {
046                    HttpSession session = request.getSession();
047    
048                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
049                            WebKeys.THEME_DISPLAY);
050    
051                    Device device = null;
052    
053                    if (PropsValues.MOBILE_DEVICE_SESSION_CACHE_ENABLED) {
054                            Object value = session.getAttribute(WebKeys.DEVICE);
055    
056                            device = (Device)NonSerializableObjectHandler.getValue(value);
057                    }
058    
059                    if (device == null) {
060                            device = DeviceDetectionUtil.detectDevice(request);
061    
062                            if (PropsValues.MOBILE_DEVICE_SESSION_CACHE_ENABLED) {
063                                    session.setAttribute(
064                                            WebKeys.DEVICE, new NonSerializableObjectHandler(device));
065                            }
066                    }
067    
068                    themeDisplay.setDevice(device);
069    
070                    UnknownDevice unknownDevice = UnknownDevice.getInstance();
071    
072                    if (device.equals(unknownDevice)) {
073                            return;
074                    }
075    
076                    MDRRuleGroupInstance mdrRuleGroupInstance = null;
077    
078                    try {
079                            mdrRuleGroupInstance = RuleGroupProcessorUtil.evaluateRuleGroups(
080                                    themeDisplay);
081    
082                            if (_log.isDebugEnabled()) {
083                                    String logMessage =
084                                            "Rule group evaluation returned rule group instance ";
085    
086                                    if (mdrRuleGroupInstance != null) {
087                                            logMessage += mdrRuleGroupInstance.getRuleGroupInstanceId();
088                                    }
089                                    else {
090                                            logMessage += "null";
091                                    }
092    
093                                    _log.debug(logMessage);
094                            }
095                    }
096                    catch (Exception e) {
097                            if (_log.isWarnEnabled()) {
098                                    _log.warn("Unable to retrieve rule group", e);
099                            }
100    
101                            return;
102                    }
103    
104                    themeDisplay.setMDRRuleGroupInstance(mdrRuleGroupInstance);
105    
106                    if (mdrRuleGroupInstance == null) {
107                            return;
108                    }
109    
110                    try {
111                            List<MDRAction> mdrActions = MDRActionLocalServiceUtil.getActions(
112                                    mdrRuleGroupInstance.getRuleGroupInstanceId());
113    
114                            ActionHandlerManagerUtil.applyActions(
115                                    mdrActions, request, response);
116                    }
117                    catch (Exception e) {
118                            if (_log.isWarnEnabled()) {
119                                    _log.warn("Unable to apply device profile", e);
120                            }
121                    }
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(
125                    DeviceServicePreAction.class);
126    
127    }