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