1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.web.servlet.HandlerExecutionChain;
43 import org.springframework.web.servlet.HandlerMapping;
44 import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
45 import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
46 import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
47
48 import info.magnolia.module.blossom.support.RepositoryUtils;
49
50
51
52
53
54
55
56 public class BlossomHandlerMapping implements HandlerMapping, InitializingBean, ApplicationContextAware {
57
58 private ApplicationContext applicationContext;
59 private AbstractUrlHandlerMapping[] targetHandlerMappings;
60 private DirectRenderingVoter directRenderingVoter;
61
62 public AbstractUrlHandlerMapping[] getTargetHandlerMappings() {
63 return targetHandlerMappings;
64 }
65
66 public void setTargetHandlerMapping(AbstractUrlHandlerMapping targetHandlerMapping) {
67 this.targetHandlerMappings = new AbstractUrlHandlerMapping[]{targetHandlerMapping};
68 }
69
70 public void setTargetHandlerMappings(AbstractUrlHandlerMapping[] targetHandlerMappings) {
71 this.targetHandlerMappings = targetHandlerMappings;
72 }
73
74 public DirectRenderingVoter getDirectRenderingPolicy() {
75 return directRenderingVoter;
76 }
77
78 public void setDirectRenderingPolicy(DirectRenderingVoter directRenderingVoter) {
79 this.directRenderingVoter = directRenderingVoter;
80 }
81
82 @Override
83 public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
84
85 PreexecutionContext preexecutionContext = PreexecutionContextHolder.get(request);
86
87
88 if (preexecutionContext == null) {
89 return getTargetHandler(request);
90 }
91
92
93 if (!preexecutionContext.getUuid().equals(RepositoryUtils.getLocalContentNodeUuid())) {
94 return getTargetHandler(request);
95 }
96
97
98 if (preexecutionContext.isModelAndViewSet()) {
99 return new HandlerExecutionChain(new ModelAndViewReuseController(preexecutionContext));
100 }
101
102
103
104
105 HandlerExecutionChain executionChain = getTargetHandler(request);
106 if (executionChain == null) {
107 return null;
108 }
109
110
111 return new HandlerExecutionChain(new ModelAndViewGrabbingController(preexecutionContext, executionChain, directRenderingVoter));
112 }
113
114 private HandlerExecutionChain getTargetHandler(HttpServletRequest request) throws Exception {
115 for (AbstractUrlHandlerMapping targetHandlerMapping : targetHandlerMappings) {
116 HandlerExecutionChain executionChain = targetHandlerMapping.getHandler(request);
117 if (executionChain != null) {
118 return executionChain;
119 }
120 }
121 return null;
122 }
123
124 @Override
125 public void afterPropertiesSet() throws Exception {
126
127 if (directRenderingVoter == null) {
128 directRenderingVoter = new DefaultDirectRenderingVoter();
129 }
130
131 if (targetHandlerMappings == null) {
132 this.targetHandlerMappings = new AbstractUrlHandlerMapping[2];
133 this.targetHandlerMappings[0] = (AbstractUrlHandlerMapping) applicationContext.getAutowireCapableBeanFactory().createBean(BeanNameUrlHandlerMapping.class);
134 DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = (DefaultAnnotationHandlerMapping) applicationContext.getAutowireCapableBeanFactory().createBean(DefaultAnnotationHandlerMapping.class);
135 defaultAnnotationHandlerMapping.setUseDefaultSuffixPattern(false);
136 this.targetHandlerMappings[1] = defaultAnnotationHandlerMapping;
137 }
138 }
139
140 @Override
141 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
142 this.applicationContext = applicationContext;
143 }
144 }