1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.preexecution

File BlossomHandlerMapping.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart2.png
64% of files have more coverage

Code metrics

14
25
3
1
163
92
10
0.4
8.33
3
3.33
30% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
BlossomHandlerMapping 58 25 30% 10 36
0.1428571514.3%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.blossom.preexecution;
35   
36    import javax.servlet.http.HttpServletRequest;
37   
38    import org.springframework.beans.BeansException;
39    import org.springframework.beans.factory.InitializingBean;
40    import org.springframework.context.ApplicationContext;
41    import org.springframework.context.ApplicationContextAware;
42    import org.springframework.core.Ordered;
43    import org.springframework.web.servlet.HandlerExecutionChain;
44    import org.springframework.web.servlet.HandlerMapping;
45    import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
46    import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
47   
48    import info.magnolia.context.MgnlContext;
49    import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
50    import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAware;
51   
52    /**
53    * Detects pre-execution requests and decorates handlers in order to either grab the returned ModelAndView or render
54    * one that was previously returned and now should be rendered.
55    *
56    * @since 0.5
57    */
 
58    public class BlossomHandlerMapping implements HandlerMapping, InitializingBean, ApplicationContextAware, BlossomDispatcherAware, Ordered {
59   
60    private ApplicationContext applicationContext;
61    private HandlerMapping[] targetHandlerMappings;
62    private DirectRenderingVoter directRenderingVoter;
63    private BlossomDispatcher dispatcher;
64    private int order = Integer.MAX_VALUE;
65   
 
66    toggle @Override
67    public int getOrder() {
68    return order;
69    }
70   
 
71    toggle public void setOrder(int order) {
72    this.order = order;
73    }
74   
 
75    toggle public HandlerMapping[] getTargetHandlerMappings() {
76    return targetHandlerMappings;
77    }
78   
 
79    toggle public void setTargetHandlerMapping(HandlerMapping targetHandlerMapping) {
80    this.targetHandlerMappings = new HandlerMapping[]{targetHandlerMapping};
81    }
82   
 
83    toggle public void setTargetHandlerMappings(HandlerMapping[] targetHandlerMappings) {
84    this.targetHandlerMappings = targetHandlerMappings;
85    }
86   
 
87    toggle public DirectRenderingVoter getDirectRenderingPolicy() {
88    return directRenderingVoter;
89    }
90   
 
91    toggle public void setDirectRenderingPolicy(DirectRenderingVoter directRenderingVoter) {
92    this.directRenderingVoter = directRenderingVoter;
93    }
94   
 
95  0 toggle @Override
96    public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
97   
98  0 PreexecutionContext preexecutionContext = PreexecutionContextHolder.get(request);
99   
100    // If it's not a pre-execution request we simply delegate to the target HandlerMapping.
101  0 if (preexecutionContext == null) {
102  0 return getTargetHandler(request);
103    }
104   
105    // If this is a pre-execution request but not for this component then also delegate.
106  0 String localUuid = MgnlContext.getAggregationState().getCurrentContentNode().getIdentifier();
107  0 if (!preexecutionContext.getUuid().equals(localUuid)) {
108  0 return getTargetHandler(request);
109    }
110   
111    // If there's already a MAV from a pre-executed handler, it should be rendered now.
112  0 if (preexecutionContext.isModelAndViewSet()) {
113  0 return new HandlerExecutionChain(new ModelAndViewReuseController(preexecutionContext));
114    }
115   
116    // At this point we know that we're being called by the BlossomFilter to do pre-execution.
117   
118    // Have the target HandlerMapping return a HandlerExecutionChain.
119  0 HandlerExecutionChain executionChain = getTargetHandler(request);
120  0 if (executionChain == null) {
121  0 return null;
122    }
123   
124    // Return a wrapper that will execute the HandlerExecutionChain and grab it's result.
125  0 return new HandlerExecutionChain(new ModelAndViewGrabbingController(preexecutionContext, executionChain, directRenderingVoter, dispatcher));
126    }
127   
 
128  0 toggle private HandlerExecutionChain getTargetHandler(HttpServletRequest request) throws Exception {
129  0 for (HandlerMapping targetHandlerMapping : targetHandlerMappings) {
130  0 HandlerExecutionChain executionChain = targetHandlerMapping.getHandler(request);
131  0 if (executionChain != null) {
132  0 return executionChain;
133    }
134    }
135  0 return null;
136    }
137   
 
138  4 toggle @Override
139    public void afterPropertiesSet() throws Exception {
140   
141  4 if (directRenderingVoter == null) {
142  4 directRenderingVoter = new DefaultDirectRenderingVoter();
143    }
144   
145  4 if (targetHandlerMappings == null) {
146  0 this.targetHandlerMappings = new HandlerMapping[2];
147  0 this.targetHandlerMappings[0] = applicationContext.getAutowireCapableBeanFactory().createBean(BeanNameUrlHandlerMapping.class);
148  0 DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = applicationContext.getAutowireCapableBeanFactory().createBean(DefaultAnnotationHandlerMapping.class);
149  0 defaultAnnotationHandlerMapping.setUseDefaultSuffixPattern(false);
150  0 this.targetHandlerMappings[1] = defaultAnnotationHandlerMapping;
151    }
152    }
153   
 
154    toggle @Override
155    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
156    this.applicationContext = applicationContext;
157    }
158   
 
159    toggle @Override
160    public void setBlossomDispatcher(BlossomDispatcher dispatcher) {
161    this.dispatcher = dispatcher;
162    }
163    }