1 13 package info.magnolia.cms; 14 15 import info.magnolia.cms.beans.config.Template; 16 import info.magnolia.cms.beans.runtime.File; 17 import info.magnolia.cms.core.Content; 18 import info.magnolia.cms.core.HierarchyManager; 19 import info.magnolia.cms.core.NodeData; 20 import info.magnolia.cms.core.Path; 21 import info.magnolia.cms.security.SessionAccessControl; 22 23 import java.text.MessageFormat ; 24 25 import javax.jcr.PathNotFoundException; 26 import javax.jcr.RepositoryException; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.log4j.Logger; 32 33 34 41 public class Aggregator { 42 43 public static final String ACTPAGE = "static_actpage"; 45 public static final String CURRENT_ACTPAGE = "actpage"; 47 public static final String FILE = "file"; 49 public static final String HANDLE = "handle"; 51 public static final String EXTENSION = "extension"; 53 public static final String HIERARCHY_MANAGER = "hierarchyManager"; 55 public static final String REQUEST_RECEIVER = "requestReceiver"; 57 public static final String DIRECT_REQUEST_RECEIVER = "/ResourceDispatcher"; 59 62 private static Logger log = Logger.getLogger(Aggregator.class); 63 64 private static final int ATOM = 2; 65 66 private HttpServletRequest request; 67 68 private Content requestedPage; 69 70 private Content subContentNode; 71 72 private NodeData requestedData; 73 74 private HierarchyManager hierarchyManager; 75 76 private String uri; 77 78 private String extension; 79 80 private String requestReceiver; 81 82 86 public Aggregator(HttpServletRequest req, HttpServletResponse response) { 87 this.request = req; 88 } 89 90 95 private void getRequestedContent() throws PathNotFoundException, RepositoryException { 96 this.requestedPage = this.hierarchyManager.getContent(this.uri); 97 } 98 99 104 private void getRequestedContent(int type) throws PathNotFoundException, RepositoryException { 105 this.requestedData = this.hierarchyManager.getNodeData(this.uri); 106 try { 107 this.subContentNode = this.hierarchyManager.getContent(this.uri + "_properties"); } 109 catch (PathNotFoundException e) { 110 if (log.isDebugEnabled()) { 111 log.debug("Path not found: " + e.getMessage()); } 113 } 114 } 115 116 119 private void parseURI() { 120 this.uri = StringUtils.substringBeforeLast(Path.getURI(this.request), "."); this.extension = StringUtils.substringAfterLast(Path.getURI(this.request), "."); } 123 124 129 public boolean collect() throws PathNotFoundException, RepositoryException { 130 boolean success = true; 131 this.hierarchyManager = SessionAccessControl.getHierarchyManager(this.request); 132 this.parseURI(); 133 if (this.hierarchyManager.isNodeData(this.uri)) { 134 this.getRequestedContent(Aggregator.ATOM); 135 this.setRequestReceiver(Aggregator.ATOM); 136 } 137 else if (this.hierarchyManager.isPage(this.uri)) { 138 this.getRequestedContent(); 139 this.setRequestReceiver(false); 140 } 141 else { 142 int lastIndexOfSlash = this.uri.lastIndexOf("/"); 145 if (lastIndexOfSlash > 0) { 146 this.uri = StringUtils.substringBeforeLast(this.uri, "/"); try { 148 this.getRequestedContent(Aggregator.ATOM); 149 this.setRequestReceiver(Aggregator.ATOM); 150 } 151 catch (RepositoryException e) { 152 this.setRequestReceiver(true); 153 success = false; 154 } 155 } 156 else { 157 this.setRequestReceiver(true); 158 success = false; 159 } 160 } 161 this.updateRequest(); 162 return success; 163 } 164 165 169 private void setRequestReceiver(int type) { 170 try { 171 String templateName = this.subContentNode.getNodeData("nodeDataTemplate").getString(); if (StringUtils.isEmpty(templateName)) { 173 this.setRequestReceiver(true); 174 return; 175 } 176 this.requestReceiver = Template.getInfo(templateName).getPath(this.extension); 177 } 178 catch (Exception e) { 179 this.setRequestReceiver(true); 180 return; 181 } 182 } 183 184 187 private void setRequestReceiver(boolean direct) { 188 if (direct) { 189 this.requestReceiver = Aggregator.DIRECT_REQUEST_RECEIVER; 190 } 191 else { 192 try { 193 String templateName = this.requestedPage.getMetaData().getTemplate(); 194 195 if (StringUtils.isBlank(templateName)) { 196 log.error(MessageFormat.format("No template configured for page [{1}].", new Object []{this.requestedPage.getHandle()})); 198 } 199 200 Template template = Template.getInfo(templateName); 201 202 if (template == null) { 203 204 log.error(MessageFormat.format("Template [{0}] for page [{1}] not found.", new Object []{templateName, this.requestedPage.getHandle()})); 206 207 return; 208 } 209 210 this.requestReceiver = template.getPath(this.extension); 211 } 212 catch (Exception e) { 213 log.error("Failed to set request receiver: " + e.getMessage(), e); } 215 } 216 } 217 218 221 private void updateRequest() { 222 if (this.requestedPage != null) { 223 this.request.setAttribute(Aggregator.ACTPAGE, this.requestedPage); 224 this.request.setAttribute(Aggregator.CURRENT_ACTPAGE, this.requestedPage); 225 } 226 if ((this.requestedData != null) && (this.subContentNode != null)) { 227 File file = new File(); 228 file.setProperties(this.subContentNode); 229 file.setNodeData(this.requestedData); 230 this.request.setAttribute(Aggregator.FILE, file); 231 } 232 this.request.setAttribute(Aggregator.HANDLE, this.uri); 233 this.request.setAttribute(Aggregator.EXTENSION, this.extension); 234 this.request.setAttribute(Aggregator.HIERARCHY_MANAGER, this.hierarchyManager); 235 this.request.setAttribute(Aggregator.REQUEST_RECEIVER, this.requestReceiver); 236 } 237 } 238 | Popular Tags |