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