KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > Aggregator


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
24
25 import javax.jcr.PathNotFoundException;
26 import javax.jcr.RepositoryException;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32
33
34 /**
35  * Class Aggregator is responsible to identify the request and gather content for the requested <code>Content </code>
36  * object. its also a responsibilty of this class to place the aggregated object to the proper place in this context its
37  * a HttpServletRequest which will hold this Content object for further processing.
38  * @author Sameer Charles
39  * @version 2.0
40  */

41 public class Aggregator {
42
43     public static final String JavaDoc ACTPAGE = "static_actpage"; //$NON-NLS-1$
44

45     public static final String JavaDoc CURRENT_ACTPAGE = "actpage"; //$NON-NLS-1$
46

47     public static final String JavaDoc FILE = "file"; //$NON-NLS-1$
48

49     public static final String JavaDoc HANDLE = "handle"; //$NON-NLS-1$
50

51     public static final String JavaDoc EXTENSION = "extension"; //$NON-NLS-1$
52

53     public static final String JavaDoc HIERARCHY_MANAGER = "hierarchyManager"; //$NON-NLS-1$
54

55     public static final String JavaDoc REQUEST_RECEIVER = "requestReceiver"; //$NON-NLS-1$
56

57     public static final String JavaDoc DIRECT_REQUEST_RECEIVER = "/ResourceDispatcher"; //$NON-NLS-1$
58

59     /**
60      * Logger.
61      */

62     private static Logger log = Logger.getLogger(Aggregator.class);
63
64     private static final int ATOM = 2;
65
66     private HttpServletRequest JavaDoc 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 JavaDoc uri;
77
78     private String JavaDoc extension;
79
80     private String JavaDoc requestReceiver;
81
82     /**
83      * constructor
84      * @param req HttpServletRequest as received by the servlet engine
85      */

86     public Aggregator(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc response) {
87         this.request = req;
88     }
89
90     /**
91      * Update requested page of the current request.
92      * @throws PathNotFoundException
93      * @throws RepositoryException
94      */

95     private void getRequestedContent() throws PathNotFoundException, RepositoryException {
96         this.requestedPage = this.hierarchyManager.getContent(this.uri);
97     }
98
99     /**
100      * Update requested content of the current request.
101      * @throws PathNotFoundException
102      * @throws RepositoryException
103      */

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"); //$NON-NLS-1$
108
}
109         catch (PathNotFoundException e) {
110             if (log.isDebugEnabled()) {
111                 log.debug("Path not found: " + e.getMessage()); //$NON-NLS-1$
112
}
113         }
114     }
115
116     /**
117      * Parse uri to get exact Content path.
118      */

119     private void parseURI() {
120         this.uri = StringUtils.substringBeforeLast(Path.getURI(this.request), "."); //$NON-NLS-1$
121
this.extension = StringUtils.substringAfterLast(Path.getURI(this.request), "."); //$NON-NLS-1$
122
}
123
124     /**
125      * Collect content from the pre configured repository and attach it to the HttpServletRequest.
126      * @throws PathNotFoundException
127      * @throws RepositoryException
128      */

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             // check again, resource might have different name
143
int lastIndexOfSlash = this.uri.lastIndexOf("/"); //$NON-NLS-1$
144

145             if (lastIndexOfSlash > 0) {
146                 this.uri = StringUtils.substringBeforeLast(this.uri, "/"); //$NON-NLS-1$
147
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     /**
166      * Set the template responsible to handle this request.
167      * @param type
168      */

169     private void setRequestReceiver(int type) {
170         try {
171             String JavaDoc templateName = this.subContentNode.getNodeData("nodeDataTemplate").getString(); //$NON-NLS-1$
172
if (StringUtils.isEmpty(templateName)) {
173                 this.setRequestReceiver(true);
174                 return;
175             }
176             this.requestReceiver = Template.getInfo(templateName).getPath(this.extension);
177         }
178         catch (Exception JavaDoc e) {
179             this.setRequestReceiver(true);
180             return;
181         }
182     }
183
184     /**
185      * Set the servlet responsible to handle direct resource request.
186      */

187     private void setRequestReceiver(boolean direct) {
188         if (direct) {
189             this.requestReceiver = Aggregator.DIRECT_REQUEST_RECEIVER;
190         }
191         else {
192             try {
193                 String JavaDoc templateName = this.requestedPage.getMetaData().getTemplate();
194
195                 if (StringUtils.isBlank(templateName)) {
196                     log.error(MessageFormat.format("No template configured for page [{1}].", //$NON-NLS-1$
197
new Object JavaDoc[]{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.", //$NON-NLS-1$
205
new Object JavaDoc[]{templateName, this.requestedPage.getHandle()}));
206
207                     return;
208                 }
209
210                 this.requestReceiver = template.getPath(this.extension);
211             }
212             catch (Exception JavaDoc e) {
213                 log.error("Failed to set request receiver: " + e.getMessage(), e); //$NON-NLS-1$
214
}
215         }
216     }
217
218     /**
219      * Attach all collected information to the HttpServletRequest.
220      */

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