1 13 package info.magnolia.cms.servlets; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.ContentHandler; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.core.ItemType; 20 import info.magnolia.cms.core.MetaData; 21 import info.magnolia.cms.core.Path; 22 import info.magnolia.cms.security.AccessDeniedException; 23 import info.magnolia.cms.security.SessionAccessControl; 24 import info.magnolia.cms.util.Resource; 25 26 import java.util.Iterator ; 27 28 import javax.jcr.PathNotFoundException; 29 import javax.jcr.RepositoryException; 30 import javax.servlet.http.HttpServlet ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.commons.lang.BooleanUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 39 42 public class RequestInterceptor extends HttpServlet { 43 44 47 private static final String ACTION_NODE_SORT = "NODE_SORT"; 49 52 private static final String ACTION_NODE_DELETE = "NODE_DELETE"; 54 57 private static final String ACTION_PREVIEW = "PREVIEW"; 59 62 private static final String PARAM_REPOSITORY = "mgnlRepository"; 64 67 private static final String PARAM_PATH = "mgnlPath"; 69 72 private static final String PARAM_PATH_SORT_ABOVE = "mgnlPathSortAbove"; 74 77 private static final String PARAM_PATH_SELECTED = "mgnlPathSelected"; 79 82 private static final long serialVersionUID = 222L; 83 84 87 private static Logger log = Logger.getLogger(RequestInterceptor.class); 88 89 93 public void doGet(HttpServletRequest request, HttpServletResponse response) { 94 95 String action = request.getParameter(EntryServlet.INTERCEPT); 96 String repository = request.getParameter(PARAM_REPOSITORY); 97 if (repository == null) { 98 repository = ContentRepository.WEBSITE; 99 } 100 HierarchyManager hm = SessionAccessControl.getHierarchyManager(request, repository); 101 if (action.equals(ACTION_PREVIEW)) { 102 String preview = request.getParameter(Resource.MGNL_PREVIEW_ATTRIBUTE); 104 if (preview != null) { 105 if (BooleanUtils.toBoolean(preview)) { 106 request.getSession().setAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE, Boolean.TRUE); 107 } 108 else { 109 request.getSession().removeAttribute(Resource.MGNL_PREVIEW_ATTRIBUTE); 110 } 111 } 112 } 113 else if (action.equals(ACTION_NODE_DELETE)) { 114 try { 116 String path = request.getParameter(PARAM_PATH); 117 updatePageMetaData(request, hm); 119 hm.delete(path); 120 hm.save(); 121 } 122 catch (RepositoryException e) { 123 log.error("Exception caught: " + e.getMessage(), e); } 125 } 126 else if (action.equals(ACTION_NODE_SORT)) { 127 try { 129 String pathSelected = request.getParameter(PARAM_PATH_SELECTED); 130 String pathSortAbove = request.getParameter(PARAM_PATH_SORT_ABOVE); 131 String pathParent = StringUtils.substringBeforeLast(pathSelected, "/"); Iterator it = hm.getContent(pathParent).getChildren( 133 ItemType.CONTENTNODE.getSystemName(), 134 ContentHandler.SORT_BY_SEQUENCE).iterator(); 135 long seqPos0 = 0; 136 long seqPos1 = 0; 137 while (it.hasNext()) { 138 Content c = (Content) it.next(); 139 if (c.getHandle().equals(pathSortAbove)) { 140 seqPos1 = c.getMetaData().getSequencePosition(); 141 break; 142 } 143 seqPos0 = c.getMetaData().getSequencePosition(); 144 } 145 Content nodeSelected = hm.getContent(pathSelected); 146 if (seqPos0 == 0) { 147 nodeSelected 149 .getMetaData() 150 .setSequencePosition(seqPos1 - (MetaData.SEQUENCE_POS_COEFFICIENT * 1000)); 151 } 152 else if (seqPos1 == 0) { 153 nodeSelected.getMetaData().setSequencePosition(); 155 } 156 else { 157 nodeSelected.getMetaData().setSequencePosition((seqPos0 + seqPos1) / 2); 159 } 160 161 this.updatePageMetaData(request, hm); 162 hm.save(); 163 } 164 catch (RepositoryException e) { 165 log.debug("Exception caught: " + e.getMessage(), e); } 167 } 168 } 169 170 177 private void updatePageMetaData(HttpServletRequest request, HierarchyManager hm) throws PathNotFoundException, 178 RepositoryException, AccessDeniedException { 179 String pagePath = StringUtils.substringBeforeLast(Path.getURI(request), "."); Content page = hm.getContent(pagePath); 181 page.updateMetaData(request); 182 } 183 } 184 | Popular Tags |