View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 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.web;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.cms.core.Channel;
38  import info.magnolia.cms.security.MgnlUser;
39  import info.magnolia.cms.security.User;
40  import info.magnolia.context.Context;
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.context.WebContext;
43  import info.magnolia.module.blossom.template.BlossomAreaDefinition;
44  import info.magnolia.module.blossom.template.BlossomTemplateDefinition;
45  import info.magnolia.module.site.Site;
46  import info.magnolia.module.site.SiteManager;
47  import info.magnolia.module.site.functions.SiteFunctions;
48  import info.magnolia.module.site.theme.Theme;
49  import info.magnolia.objectfactory.Components;
50  import info.magnolia.rendering.context.RenderingContext;
51  import info.magnolia.rendering.template.AreaDefinition;
52  import info.magnolia.rendering.template.TemplateDefinition;
53  
54  import java.util.HashSet;
55  import java.util.Set;
56  
57  import javax.jcr.Node;
58  
59  import org.springframework.beans.factory.InitializingBean;
60  import org.springframework.core.MethodParameter;
61  import org.springframework.web.bind.support.WebDataBinderFactory;
62  import org.springframework.web.context.request.NativeWebRequest;
63  import org.springframework.web.method.support.HandlerMethodArgumentResolver;
64  import org.springframework.web.method.support.ModelAndViewContainer;
65  
66  /**
67   * Resolves parameters for a number of Magnolia objects including {@link Node},
68   * {@link info.magnolia.cms.core.AggregationState}, {@link info.magnolia.context.WebContext} and {@link info.magnolia.context.Context}.
69   * <p/>
70   * To use it configure it on your {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter}.
71   *
72   * @since 3.0.2
73   */
74  public class BlossomHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver, InitializingBean {
75  
76      private SiteManager siteManager;
77      private SiteFunctions siteFunctions;
78  
79      public void setSiteManager(SiteManager siteManager) {
80          this.siteManager = siteManager;
81      }
82  
83      public void setSiteFunctions(SiteFunctions siteFunctions) {
84          this.siteFunctions = siteFunctions;
85      }
86  
87      private Set<Class<?>> supportedParameterTypes = new HashSet<Class<?>>();
88  
89      public BlossomHandlerMethodArgumentResolver() {
90          supportedParameterTypes.add(Node.class);
91          supportedParameterTypes.add(TemplateDefinition.class);
92          supportedParameterTypes.add(BlossomTemplateDefinition.class);
93          supportedParameterTypes.add(AreaDefinition.class);
94          supportedParameterTypes.add(BlossomAreaDefinition.class);
95          supportedParameterTypes.add(AggregationState.class);
96          supportedParameterTypes.add(WebContext.class);
97          supportedParameterTypes.add(Context.class);
98          supportedParameterTypes.add(User.class);
99          supportedParameterTypes.add(MgnlUser.class);
100         supportedParameterTypes.add(Channel.class);
101         supportedParameterTypes.add(Site.class);
102         supportedParameterTypes.add(Theme.class);
103     }
104 
105     @Override
106     public boolean supportsParameter(MethodParameter parameter) {
107         for (Class<?> supportedParameterType : supportedParameterTypes) {
108             if (parameter.getParameterType().isAssignableFrom(supportedParameterType)) {
109                 return true;
110             }
111         }
112         return false;
113     }
114 
115     @Override
116     public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
117 
118         if (methodParameter.getParameterType().isAssignableFrom(Node.class)) {
119             RenderingContext renderingContext = Components.getComponent(RenderingContext.class);
120             if (hasSubsequentParametersOfType(methodParameter, Node.class)) {
121                 return renderingContext.getMainContent();
122             }
123             Node currentContent = renderingContext.getCurrentContent();
124             return currentContent != null ? currentContent : renderingContext.getMainContent();
125         }
126 
127         if (methodParameter.getParameterType().isAssignableFrom(TemplateDefinition.class)) {
128             return Components.getComponent(RenderingContext.class).getRenderableDefinition();
129         }
130         if (methodParameter.getParameterType().isAssignableFrom(BlossomTemplateDefinition.class)) {
131             return Components.getComponent(RenderingContext.class).getRenderableDefinition();
132         }
133         if (methodParameter.getParameterType().isAssignableFrom(AreaDefinition.class)) {
134             return Components.getComponent(RenderingContext.class).getRenderableDefinition();
135         }
136         if (methodParameter.getParameterType().isAssignableFrom(BlossomAreaDefinition.class)) {
137             return Components.getComponent(RenderingContext.class).getRenderableDefinition();
138         }
139         if (methodParameter.getParameterType().isAssignableFrom(AggregationState.class)) {
140             return MgnlContext.getAggregationState();
141         }
142         if (methodParameter.getParameterType().isAssignableFrom(Context.class)) {
143             return MgnlContext.getInstance();
144         }
145         if (methodParameter.getParameterType().isAssignableFrom(WebContext.class)) {
146             return MgnlContext.getWebContext();
147         }
148         if (methodParameter.getParameterType().isAssignableFrom(User.class)) {
149             return MgnlContext.getUser();
150         }
151         if (methodParameter.getParameterType().isAssignableFrom(MgnlUser.class)) {
152             return MgnlContext.getUser();
153         }
154         if (methodParameter.getParameterType().isAssignableFrom(Channel.class)) {
155             return MgnlContext.getAggregationState().getChannel();
156         }
157         if (methodParameter.getParameterType().isAssignableFrom(Theme.class)) {
158             Site site = siteManager.getCurrentSite();
159             if (site == null) {
160                 return null;
161             }
162             return siteFunctions.theme(site);
163         }
164         if (methodParameter.getParameterType().isAssignableFrom(Site.class)) {
165             return siteManager.getCurrentSite();
166         }
167 
168         return null;
169     }
170 
171     private boolean hasSubsequentParametersOfType(MethodParameter methodParameter, Class<?> clazz) {
172         Class<?>[] parameterTypes = methodParameter.getMethod().getParameterTypes();
173         for (int i = methodParameter.getParameterIndex() + 1; i < parameterTypes.length; i++) {
174             Class<?> parameterType = parameterTypes[i];
175             if (parameterType.equals(clazz)) {
176                 return true;
177             }
178         }
179         return false;
180     }
181 
182     @Override
183     public void afterPropertiesSet() throws Exception {
184         if (siteFunctions == null) {
185             siteFunctions = Components.getComponent(SiteFunctions.class);
186         }
187         if (siteManager == null) {
188             siteManager = Components.getComponent(SiteManager.class);
189         }
190     }
191 }