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.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
60
61
62
63 public class InterceptFilter extends AbstractMgnlFilter {
64
65 private static final Logger log = LoggerFactory.getLogger(InterceptFilter.class);
66
67
68
69
70 public static final String INTERCEPT = "mgnlIntercept";
71
72
73
74
75 private static final String ACTION_NODE_SORT = "NODE_SORT";
76
77
78
79
80 private static final String ACTION_NODE_DELETE = "NODE_DELETE";
81
82
83
84
85 private static final String ACTION_PREVIEW = "PREVIEW";
86
87
88
89
90 public static final String PARAM_REPOSITORY = "mgnlRepository";
91
92
93
94
95 public static final String PARAM_PATH = "mgnlPath";
96
97
98
99
100 private static final String PARAM_PATH_TARGET = "mgnlPathTarget";
101
102
103
104
105 private static final String PARAM_PATH_SELECTED = "mgnlPathSelected";
106
107
108
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
140
141
142
143
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
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
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
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 }