View Javadoc

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