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

File BlossomFilter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
65% of files have more coverage

Code metrics

16
44
6
1
170
102
18
0.41
7.33
6
3

Classes

Class Line # Actions
BlossomFilter 66 44 0% 18 66
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 java.io.IOException;
37    import javax.jcr.Node;
38    import javax.jcr.RepositoryException;
39    import javax.jcr.Session;
40    import javax.servlet.FilterChain;
41    import javax.servlet.FilterConfig;
42    import javax.servlet.ServletException;
43    import javax.servlet.http.HttpServletRequest;
44    import javax.servlet.http.HttpServletResponse;
45   
46    import org.slf4j.Logger;
47    import org.slf4j.LoggerFactory;
48    import org.springframework.core.NestedIOException;
49    import org.springframework.web.servlet.ModelAndView;
50   
51    import info.magnolia.cms.core.AggregationState;
52    import info.magnolia.cms.filters.OncePerRequestAbstractMgnlFilter;
53    import info.magnolia.context.MgnlContext;
54    import info.magnolia.jcr.util.NodeTypes;
55    import info.magnolia.objectfactory.Components;
56    import info.magnolia.rendering.engine.RenderingEngine;
57    import info.magnolia.rendering.engine.ResponseOutputProvider;
58    import info.magnolia.repository.RepositoryConstants;
59   
60    /**
61    * Filter that detects pre-execution requests, sets up a context and passes the request into Magnolia for rendering.
62    * The context is discovered by BlossomHandlerMapping and rendering is performed appropriately.
63    *
64    * @since 0.5
65    */
 
66    public class BlossomFilter extends OncePerRequestAbstractMgnlFilter {
67   
68    public static final String PREEXECUTION_CONTEXT_PARAMETER_NAME = "_pecid";
69   
70    private final Logger logger = LoggerFactory.getLogger(getClass());
71   
 
72  0 toggle @Override
73    public void init(FilterConfig filterConfig) throws ServletException {
74    }
75   
 
76  0 toggle @Override
77    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
78   
79  0 String preexecutionUuid = request.getParameter(PREEXECUTION_CONTEXT_PARAMETER_NAME);
80  0 if (preexecutionUuid == null) {
81    // If this is not a pre-execution request then let it pass through.
82  0 chain.doFilter(request, response);
83  0 return;
84    }
85   
86    // Find the target content.
87  0 Node content;
88  0 try {
89  0 Session session = MgnlContext.getSystemContext().getJCRSession(RepositoryConstants.WEBSITE);
90  0 content = session.getNodeByIdentifier(preexecutionUuid);
91    } catch (RepositoryException e) {
92  0 logger.error("Content for pre-execution [" + preexecutionUuid + "] not found in repository");
93  0 return;
94    }
95   
96    // Create and expose a PreexecutionContext so that the BlossomHandlerMapping can read it down the line.
97  0 PreexecutionContext preexecutionContext = new PreexecutionContext(preexecutionUuid);
98  0 PreexecutionContextHolder.put(request, preexecutionContext);
99  0 try {
100   
101  0 executeHandler(content, response);
102   
103    // If the handler was rendered directly then we're done.
104  0 ModelAndView mv = preexecutionContext.getModelAndView();
105  0 if (mv == null || mv.wasCleared()) {
106  0 return;
107    }
108   
109    // Continue into Magnolia, the pre-execution result will get rendered down the line.
110  0 chain.doFilter(request, response);
111   
112    } catch (RepositoryException e) {
113  0 throw new ServletException(e);
114    } finally {
115  0 PreexecutionContextHolder.remove(request);
116    }
117    }
118   
 
119  0 toggle private void executeHandler(Node content, HttpServletResponse response) throws IOException, RepositoryException {
120   
121  0 AggregationState state = getAggregationStateSafely();
122  0 Node orgMainContent = null;
123  0 Node orgCurrentContent = null;
124   
125  0 if (state != null) {
126  0 orgMainContent = state.getMainContentNode();
127  0 orgCurrentContent = state.getCurrentContentNode();
128   
129  0 state.setCurrentContentNode(content);
130  0 if (orgMainContent == null) {
131  0 state.setMainContentNode(findMainContent(content));
132    }
133    }
134  0 try {
135   
136  0 try {
137  0 Components.getComponent(RenderingEngine.class).render(content, new ResponseOutputProvider(response));
138    } catch (info.magnolia.rendering.engine.RenderException e) {
139  0 throw new NestedIOException("Error pre-executing handler", e);
140    }
141   
142    } finally {
143  0 if (state != null) {
144  0 state.setMainContentNode(orgMainContent);
145  0 state.setCurrentContentNode(orgCurrentContent);
146    }
147    }
148    }
149   
 
150  0 toggle protected Node findMainContent(Node content) throws RepositoryException {
151  0 while (content.getDepth() != 0) {
152  0 if (content.getPrimaryNodeType().getName().equals(NodeTypes.Content.NAME)) {
153  0 return content;
154    }
155  0 content = content.getParent();
156    }
157  0 return null;
158    }
159   
 
160  0 toggle protected AggregationState getAggregationStateSafely() {
161  0 if (MgnlContext.isWebContext()) {
162  0 return MgnlContext.getAggregationState();
163    }
164  0 return null;
165    }
166   
 
167  0 toggle @Override
168    public void destroy() {
169    }
170    }