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.dispatcher.BlossomDispatcher;
49 import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAware;
50 import info.magnolia.module.blossom.support.RepositoryUtils;
51
52
53
54
55
56
57
58 public class BlossomHandlerMapping implements HandlerMapping, InitializingBean, ApplicationContextAware, BlossomDispatcherAware {
59
60 private ApplicationContext applicationContext;
61 private AbstractUrlHandlerMapping[] targetHandlerMappings;
62 private DirectRenderingVoter directRenderingVoter;
63 private BlossomDispatcher dispatcher;
64
65 public AbstractUrlHandlerMapping[] getTargetHandlerMappings() {
66 return targetHandlerMappings;
67 }
68
69 public void setTargetHandlerMapping(AbstractUrlHandlerMapping targetHandlerMapping) {
70 this.targetHandlerMappings = new AbstractUrlHandlerMapping[]{targetHandlerMapping};
71 }
72
73 public void setTargetHandlerMappings(AbstractUrlHandlerMapping[] targetHandlerMappings) {
74 this.targetHandlerMappings = targetHandlerMappings;
75 }
76
77 public DirectRenderingVoter getDirectRenderingPolicy() {
78 return directRenderingVoter;
79 }
80
81 public void setDirectRenderingPolicy(DirectRenderingVoter directRenderingVoter) {
82 this.directRenderingVoter = directRenderingVoter;
83 }
84
85 @Override
86 public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
87
88 PreexecutionContext preexecutionContext = PreexecutionContextHolder.get(request);
89
90
91 if (preexecutionContext == null) {
92 return getTargetHandler(request);
93 }
94
95
96 if (!preexecutionContext.getUuid().equals(RepositoryUtils.getLocalContentNodeUuid())) {
97 return getTargetHandler(request);
98 }
99
100
101 if (preexecutionContext.isModelAndViewSet()) {
102 return new HandlerExecutionChain(new ModelAndViewReuseController(preexecutionContext));
103 }
104
105
106
107
108 HandlerExecutionChain executionChain = getTargetHandler(request);
109 if (executionChain == null) {
110 return null;
111 }
112
113
114 return new HandlerExecutionChain(new ModelAndViewGrabbingController(preexecutionContext, executionChain, directRenderingVoter, dispatcher));
115 }
116
117 private HandlerExecutionChain getTargetHandler(HttpServletRequest request) throws Exception {
118 for (AbstractUrlHandlerMapping targetHandlerMapping : targetHandlerMappings) {
119 HandlerExecutionChain executionChain = targetHandlerMapping.getHandler(request);
120 if (executionChain != null) {
121 return executionChain;
122 }
123 }
124 return null;
125 }
126
127 @Override
128 public void afterPropertiesSet() throws Exception {
129
130 if (directRenderingVoter == null) {
131 directRenderingVoter = new DefaultDirectRenderingVoter();
132 }
133
134 if (targetHandlerMappings == null) {
135 this.targetHandlerMappings = new AbstractUrlHandlerMapping[2];
136 this.targetHandlerMappings[0] = (AbstractUrlHandlerMapping) applicationContext.getAutowireCapableBeanFactory().createBean(BeanNameUrlHandlerMapping.class);
137 DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = (DefaultAnnotationHandlerMapping) applicationContext.getAutowireCapableBeanFactory().createBean(DefaultAnnotationHandlerMapping.class);
138 defaultAnnotationHandlerMapping.setUseDefaultSuffixPattern(false);
139 this.targetHandlerMappings[1] = defaultAnnotationHandlerMapping;
140 }
141 }
142
143 @Override
144 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
145 this.applicationContext = applicationContext;
146 }
147
148 @Override
149 public void setBlossomDispatcher(BlossomDispatcher dispatcher) {
150 this.dispatcher = dispatcher;
151 }
152 }