1 13 package info.magnolia.cms.filters; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.beans.config.URI2RepositoryManager; 17 import info.magnolia.cms.beans.config.URI2RepositoryMapping; 18 import info.magnolia.cms.core.Aggregator; 19 import info.magnolia.cms.core.Content; 20 import info.magnolia.cms.core.HierarchyManager; 21 import info.magnolia.cms.core.Path; 22 import info.magnolia.cms.security.AccessDeniedException; 23 import info.magnolia.cms.security.Permission; 24 import info.magnolia.cms.util.ExclusiveWrite; 25 import info.magnolia.cms.util.Resource; 26 import info.magnolia.context.MgnlContext; 27 28 import java.io.IOException ; 29 30 import javax.jcr.PathNotFoundException; 31 import javax.jcr.RepositoryException; 32 import javax.servlet.Filter ; 33 import javax.servlet.FilterChain ; 34 import javax.servlet.FilterConfig ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletRequest ; 37 import javax.servlet.ServletResponse ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.http.HttpSession ; 41 42 import org.apache.commons.lang.BooleanUtils; 43 import org.apache.commons.lang.StringUtils; 44 import org.slf4j.Logger; 45 import org.slf4j.LoggerFactory; 46 47 48 53 public class MgnlInterceptFilter implements Filter { 54 55 58 public static final String INTERCEPT = "mgnlIntercept"; 60 63 private static final String ACTION_NODE_SORT = "NODE_SORT"; 65 68 private static final String ACTION_NODE_DELETE = "NODE_DELETE"; 70 73 private static final String ACTION_PREVIEW = "PREVIEW"; 75 78 private static final String PARAM_REPOSITORY = "mgnlRepository"; 80 83 private static final String PARAM_PATH = "mgnlPath"; 85 88 private static final String PARAM_PATH_SORT_ABOVE = "mgnlPathSortAbove"; 90 93 private static final String PARAM_PATH_SELECTED = "mgnlPathSelected"; 95 98 private static Logger log = LoggerFactory.getLogger(MgnlInterceptFilter.class); 99 100 103 public void init(FilterConfig filterConfig) throws ServletException { 104 } 106 107 110 public void destroy() { 111 } 113 114 118 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException , 119 ServletException { 120 121 HttpServletRequest request = (HttpServletRequest ) req; 122 HttpServletResponse response = (HttpServletResponse ) resp; 123 124 if (isAuthorized(request, response)) { 126 if (req.getParameter(INTERCEPT) != null) { 127 MgnlInterceptFilter.setHandleAndMapping(request); 128 this.intercept(request, response); 129 } 130 } 131 132 filterChain.doFilter(req, resp); 133 } 134 135 private static void setHandleAndMapping(HttpServletRequest request) { 136 String uri = Path.getURI(request); 137 int firstDotPos = StringUtils.indexOf(uri, '.', StringUtils.lastIndexOf(uri, '/')); 138 String handle; 139 String selector; 140 String extension; 141 if (firstDotPos > -1) { 142 int lastDotPos = StringUtils.lastIndexOf(uri, '.'); 143 handle = StringUtils.substring(uri, 0, firstDotPos); 144 selector = StringUtils.substring(uri, firstDotPos + 1, lastDotPos); 145 extension = StringUtils.substring(uri, lastDotPos + 1); 146 } 147 else { 148 handle = uri; 150 selector = ""; 151 extension = ""; 152 } 153 154 URI2RepositoryMapping mapping = URI2RepositoryManager.getInstance().getMapping(uri); 155 156 handle = mapping.getHandle(handle); 158 159 request.setAttribute(Aggregator.REPOSITORY, mapping.getRepository()); 160 request.setAttribute(Aggregator.MAPPING, mapping); 161 request.setAttribute(Aggregator.HANDLE, handle); 162 request.setAttribute(Aggregator.SELECTOR, selector); 163 request.setAttribute(Aggregator.EXTENSION, extension); 164 } 165 166 170 public void intercept(HttpServletRequest request, HttpServletResponse response) { 171 String action = request.getParameter(INTERCEPT); 172 String repository = request.getParameter(PARAM_REPOSITORY); 173 String nodePath = request.getParameter(PARAM_PATH); 174 String handle = (String ) request.getAttribute(Aggregator.HANDLE); 175 176 if (repository == null) { 177 repository = (String ) request.getAttribute(Aggregator.REPOSITORY); 178 } 179 180 if (repository == null) { 181 repository = ContentRepository.WEBSITE; 182 } 183 184 HierarchyManager hm = MgnlContext.getHierarchyManager(repository); 185 synchronized (ExclusiveWrite.getInstance()) { 186 if (action.equals(ACTION_PREVIEW)) { 187 String preview = request.getParameter(Resource.MGNL_PREVIEW_ATTRIBUTE); 189 if (preview != null) { 190 191 HttpSession httpsession = request.getSession(true); 193 if (BooleanUtils.toBoolean(preview)) { 194 httpsession.setAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE, Boolean.TRUE); 195 } 196 else { 197 httpsession.removeAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE); 198 } 199 } 200 } 201 else if (action.equals(ACTION_NODE_DELETE)) { 202 try { 204 Content page = hm.getContent(handle); 205 page.updateMetaData(); 206 hm.delete(nodePath); 207 hm.save(); 208 } 209 catch (RepositoryException e) { 210 log.error("Exception caught: " + e.getMessage(), e); } 212 } 213 else if (action.equals(ACTION_NODE_SORT)) { 214 try { 216 String pathSelected = request.getParameter(PARAM_PATH_SELECTED); 217 String pathSortAbove = request.getParameter(PARAM_PATH_SORT_ABOVE); 218 String pathParent = StringUtils.substringBeforeLast(pathSelected, "/"); String srcName = StringUtils.substringAfterLast(pathSelected, "/"); 220 String destName = StringUtils.substringAfterLast(pathSortAbove, "/"); 221 if (StringUtils.equalsIgnoreCase(destName, "mgnlNew")) { 222 destName = null; 223 } 224 hm.getContent(pathParent).orderBefore(srcName, destName); 225 hm.save(); 226 } 227 catch (RepositoryException e) { 228 if (log.isDebugEnabled()) { 229 log.debug("Exception caught: " + e.getMessage(), e); } 231 } 232 } 233 } 234 } 235 236 243 protected boolean isAuthorized(HttpServletRequest req, HttpServletResponse res) throws IOException { 244 if (MgnlContext.getAccessManager(ContentRepository.WEBSITE) != null) { 245 String path = StringUtils.substringBefore(Path.getURI(req), "."); if (!MgnlContext.getAccessManager(ContentRepository.WEBSITE).isGranted(path, Permission.READ)) { 247 res.sendError(HttpServletResponse.SC_FORBIDDEN); 248 } 249 } 250 return true; 251 } 252 253 } | Popular Tags |