View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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.cms.filters;
35  
36  import info.magnolia.cms.core.AggregationState;
37  import info.magnolia.context.Context;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.jcr.util.MetaDataUtil;
40  import info.magnolia.jcr.util.NodeUtil;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.io.IOException;
44  
45  import javax.jcr.LoginException;
46  import javax.jcr.Node;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Session;
49  import javax.servlet.FilterChain;
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  import org.apache.commons.lang.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Handle intercepted administrative requests.
60   * 
61   * @version $Id$
62   */
63  public class InterceptFilter extends AbstractMgnlFilter {
64  
65      private static final Logger log = LoggerFactory.getLogger(InterceptFilter.class);
66  
67      /**
68       * Request parameter: the INTERCEPT holds the name of an administrative action to perform.
69       */
70      public static final String INTERCEPT = "mgnlIntercept";
71  
72      /**
73       * Action: sort a paragraph.
74       */
75      private static final String ACTION_NODE_SORT = "NODE_SORT";
76  
77      /**
78       * Action: delete a paragraph.
79       */
80      private static final String ACTION_NODE_DELETE = "NODE_DELETE";
81  
82      /**
83       * Action: preview a page.
84       */
85      private static final String ACTION_PREVIEW = "PREVIEW";
86  
87      /**
88       * request parameter: repository name.
89       */
90      public static final String PARAM_REPOSITORY = "mgnlRepository";
91  
92      /**
93       * request parameter: node path, used for paragraph deletion.
94       */
95      public static final String PARAM_PATH = "mgnlPath";
96  
97      /**
98       * request parameter: sort-above paragraph.
99       */
100     private static final String PARAM_PATH_TARGET = "mgnlPathTarget";
101 
102     /**
103      * request parameter: selected paragraph.
104      */
105     private static final String PARAM_PATH_SELECTED = "mgnlPathSelected";
106 
107     /**
108      * Attribute used for enabling the preview mode.
109      */
110     public static final String MGNL_PREVIEW_ATTRIBUTE = "mgnlPreview";
111 
112     @Override
113     public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
114 
115         if (request.getParameter(INTERCEPT) != null) {
116             try {
117                 this.intercept(request, response);
118             } catch (LoginException e) {
119                 throw new ServletException(e);
120             } catch (RepositoryException e) {
121                 throw new ServletException(e);
122             }
123         }
124 
125         final AggregationState aggregationState = MgnlContext.getAggregationState();
126         aggregationState.setPreviewMode(previewMode());
127 
128         chain.doFilter(request, response);
129     }
130 
131     protected boolean previewMode() {
132         if (MgnlContext.getParameter(MGNL_PREVIEW_ATTRIBUTE) != null) {
133             return Boolean.parseBoolean(MgnlContext.getParameter(MGNL_PREVIEW_ATTRIBUTE));
134         }
135         return true;
136     }
137 
138     /**
139      * Request and Response here is same as received by the original page so it includes all post/get data. Sub action
140      * could be called from here once this action finishes, it will continue loading the requested page.
141      * 
142      * @throws RepositoryException
143      * @throws LoginException
144      */
145     public void intercept(HttpServletRequest request, HttpServletResponse response) throws LoginException, RepositoryException {
146         final AggregationState aggregationState = MgnlContext.getAggregationState();
147         String action = request.getParameter(INTERCEPT);
148         String repository = request.getParameter(PARAM_REPOSITORY);
149         String nodePath = request.getParameter(PARAM_PATH);
150         String handle = aggregationState.getHandle();
151         String channel = aggregationState.getChannel().getName();
152 
153         if (repository == null) {
154             repository = aggregationState.getRepository();
155         }
156 
157         if (repository == null) {
158             repository = RepositoryConstants.WEBSITE;
159         }
160 
161         final Session session = MgnlContext.getJCRSession(repository);
162 
163         if (ACTION_PREVIEW.equals(action)) {
164             // preview mode (button in main bar)
165             String preview = request.getParameter(MGNL_PREVIEW_ATTRIBUTE);
166             log.debug("preview request parameter value is {} ", preview);
167             if (preview != null) {
168                 if (Boolean.parseBoolean(preview)) {
169                     MgnlContext.setAttribute(MGNL_PREVIEW_ATTRIBUTE, Boolean.TRUE, Context.SESSION_SCOPE);
170                     MgnlContext.setAttribute(MultiChannelFilter.ENFORCE_CHANNEL_PARAMETER, channel, Context.SESSION_SCOPE);
171                 } else {
172                     MgnlContext.removeAttribute(MGNL_PREVIEW_ATTRIBUTE, Context.SESSION_SCOPE);
173                     MgnlContext.removeAttribute(MultiChannelFilter.ENFORCE_CHANNEL_PARAMETER, Context.SESSION_SCOPE);
174                 }
175             } else {
176                 MgnlContext.removeAttribute(MGNL_PREVIEW_ATTRIBUTE, Context.SESSION_SCOPE);
177                 MgnlContext.removeAttribute(MultiChannelFilter.ENFORCE_CHANNEL_PARAMETER, Context.SESSION_SCOPE);
178             }
179         } else if (ACTION_NODE_DELETE.equals(action)) {
180             // delete paragraph
181             try {
182                 Node page = session.getNode(handle);
183                 session.removeItem(nodePath);
184                 MetaDataUtil.updateMetaData(page);
185                 session.save();
186             } catch (RepositoryException e) {
187                 log.error("Exception caught: {}", e.getMessage(), e);
188             }
189         } else if (ACTION_NODE_SORT.equals(action)) {
190             // sort paragraphs
191             try {
192                 String pathSelected = request.getParameter(PARAM_PATH_SELECTED);
193                 String pathTarget = request.getParameter(PARAM_PATH_TARGET);
194                 String pathParent = StringUtils.substringBeforeLast(pathSelected, "/");
195                 String srcName = StringUtils.substringAfterLast(pathSelected, "/");
196                 String destName = StringUtils.substringAfterLast(pathTarget, "/");
197                 String order = StringUtils.defaultIfEmpty(request.getParameter("order"), "before");
198                 if (StringUtils.equalsIgnoreCase(destName, "mgnlNew")) {
199                     destName = null;
200                 }
201                 Node parent = session.getNode(pathParent + srcName);
202 
203                 if ("before".equals(order)) {
204                     NodeUtil.orderBefore(parent, destName);
205                 } else {
206                     NodeUtil.orderAfter(parent, destName);
207                 }
208 
209                 Node page = session.getNode(handle);
210                 MetaDataUtil.updateMetaData(page);
211                 session.save();
212             } catch (RepositoryException e) {
213                 log.error("Exception caught: {}", e.getMessage(), e);
214             }
215         } else {
216             log.warn("Unknown action {}", action);
217         }
218     }
219 }