1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
63
64
65 public class InterceptFilter extends AbstractMgnlFilter {
66
67 private static final Logger log = LoggerFactory.getLogger(InterceptFilter.class);
68
69
70
71
72 public static final String INTERCEPT = "mgnlIntercept";
73
74
75
76
77 private static final String ACTION_NODE_SORT = "NODE_SORT";
78
79
80
81
82 private static final String ACTION_NODE_DELETE = "NODE_DELETE";
83
84
85
86
87 private static final String ACTION_PREVIEW = "PREVIEW";
88
89
90
91
92 public static final String PARAM_REPOSITORY = "mgnlRepository";
93
94
95
96
97 public static final String PARAM_PATH = "mgnlPath";
98
99
100
101
102 private static final String PARAM_PATH_TARGET = "mgnlPathTarget";
103
104
105
106
107 private static final String PARAM_PATH_SELECTED = "mgnlPathSelected";
108
109
110
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
135 if (MgnlContext.getParameter(MGNL_PREVIEW_ATTRIBUTE) != null) {
136 return Boolean.parseBoolean(MgnlContext.getParameter(MGNL_PREVIEW_ATTRIBUTE));
137 }
138
139
140 final Boolean value = (Boolean) MgnlContext.getAttribute(MGNL_PREVIEW_ATTRIBUTE, Context.SESSION_SCOPE);
141 return BooleanUtils.toBoolean(value);
142 }
143
144
145
146
147
148
149
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
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
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
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 }