Source code: com/obinary/cms/Aggregator.java
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-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17 package com.obinary.cms;
18
19
20 import com.obinary.cms.core.Content;
21 import com.obinary.cms.core.HierarchyManager;
22 import com.obinary.cms.core.Atom;
23 import com.obinary.cms.beans.TemplateInfo;
24 import com.obinary.cms.beans.File;
25 import com.obinary.cms.beans.ServerInfo;
26
27 import javax.jcr.Ticket;
28 import javax.jcr.Node;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.ElementNotFoundException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34
35 /**
36 * Date: Apr 28, 2003
37 * Time: 11:20:59 AM
38 * @author Sameer Charles
39 * @version 1.0
40 */
41
42
43 /**
44 * <p>
45 * Class Aggregator is responsible to identify the request and gather content
46 * for the requested <b>Content</b> object.
47 * its also a responsibilty of this class to place the aggregated object to the proper place
48 * in this context its a HttpServletRequest which will hold this Content object for further
49 * processing.
50 * </p>
51 *
52 */
53
54
55 public class Aggregator {
56
57 private static final int CONTAINER = 1;
58 private static final int ATOM = 2;
59
60 public static final String ACTPAGE = "static_actpage";
61 public static final String CURRENT_ACTPAGE = "actpage";
62 public static final String FILE = "file";
63 public static final String HANDLE = "handle";
64 public static final String HIERARCHY_MANAGER = "hierarchyManager";
65 public static final String REQUEST_RECEIVER = "requestReceiver";
66 public static final String DIRECT_REQUEST_RECEIVER = "/ResourceDispatcher";
67
68
69 private Ticket ticket;
70 private HttpServletRequest request;
71 private HttpServletResponse response;
72 private Content requestedPage;
73 private Content atomContainer;
74 private Atom requestedAtom;
75 private HierarchyManager hierarchyManager;
76 private String URI;
77 private String extension;
78 private String requestReceiver;
79
80
81
82
83
84 /**
85 * constructor
86 *
87 * @param t Ticket which will be used for further aggregation
88 * @param req HttpServletRequest as received by the servlet engine
89 */
90 public Aggregator(Ticket t, HttpServletRequest req, HttpServletResponse response) {
91 this.ticket = t;
92 this.request = req;
93 this.response = response;
94 }
95
96
97
98 /**
99 * <p>update requested page of the current request</p>
100 *
101 * @throws ElementNotFoundException
102 * @throws RepositoryException
103 */
104 private void getRequestedContent() throws ElementNotFoundException, RepositoryException {
105 this.requestedPage = this.hierarchyManager.getPage(this.URI);
106 }
107
108
109
110 /**
111 * <p>update requested content of the current request</p>
112 *
113 * @throws ElementNotFoundException
114 * @throws RepositoryException
115 */
116 private void getRequestedContent(int type) throws ElementNotFoundException, RepositoryException {
117 this.requestedAtom = this.hierarchyManager.getAtom(this.URI);
118 try {
119 this.atomContainer = this.hierarchyManager.getContent(this.URI+"_properties");
120 } catch (ElementNotFoundException e) {}
121 }
122
123
124
125 /**
126 * <p>parse uri to get exact Content path</p>
127 *
128 */
129 private void parseURI() {
130 int lastIndexOfDot = this.request.getRequestURI().lastIndexOf(".");
131 try {
132 this.URI = this.request.getRequestURI().substring(0,lastIndexOfDot);
133 this.extension = this.request.getRequestURI().substring(lastIndexOfDot+1);
134 } catch (Exception e) {
135 this.URI = this.request.getRequestURI();
136 }
137 }
138
139
140
141 /**
142 * <p>Collect content from the pre configured repository and attach it to the
143 * HttpServletRequest. </p>
144 *
145 *
146 * @throws ElementNotFoundException
147 * @throws RepositoryException
148 */
149 public boolean collect() throws ElementNotFoundException, RepositoryException {
150 Node root = this.ticket.getRootNode();
151 this.hierarchyManager = new HierarchyManager(this.request);
152 this.hierarchyManager.setStartPage(root);
153 this.parseURI();
154 if (this.hierarchyManager.isAtom(this.URI)) {
155 this.getRequestedContent(Aggregator.ATOM);
156 this.setRequestReceiver(Aggregator.ATOM);
157 } else if (this.hierarchyManager.isPage(this.URI)) {
158 this.getRequestedContent();
159 this.setRequestReceiver();
160 } else {
161 this.setRequestReceiver(true);
162 }
163 this.updateRequest();
164 return true;
165 }
166
167
168
169 /**
170 *
171 * @return URI mapping as in ServerInfo
172 */
173 private String getURIMap() {
174 return ServerInfo.getURIMapping(this.request.getRequestURI());
175 }
176
177
178
179 /**
180 * <p>set the template responsible to handle this request</p>
181 *
182 */
183 private void setRequestReceiver() {
184 String templateName = this.requestedPage.getMetaData().getTemplate();
185 try {
186 this.requestReceiver = TemplateInfo.getInfo(templateName).getPath(this.extension);
187 } catch (Exception e) {e.printStackTrace(); }
188 }
189
190
191
192 /**
193 * <p>set the template responsible to handle this request</p>
194 *
195 * @param type
196 */
197 private void setRequestReceiver(int type) {
198 try {
199 String templateName = this.atomContainer.getAtom("atomTemplate").getString();
200 if (templateName.equals("")) {
201 this.setRequestReceiver(true);
202 return;
203 }
204 this.requestReceiver = TemplateInfo.getInfo(templateName).getPath(this.extension);
205 } catch (Exception e) {
206 this.setRequestReceiver(true);
207 return;
208 }
209 }
210
211
212
213 /**
214 * <p>set the serlet responsible to handle direct resource request</p>
215 *
216 */
217 private void setRequestReceiver(boolean direct) {
218 this.requestReceiver = Aggregator.DIRECT_REQUEST_RECEIVER;
219 }
220
221
222
223 /**
224 * <p>Attach all collected information to the HttpServletRequest</p>
225 *
226 */
227 private void updateRequest() {
228 if (this.requestedPage != null) {
229 this.request.setAttribute(Aggregator.ACTPAGE,this.requestedPage);
230 this.request.setAttribute(Aggregator.CURRENT_ACTPAGE,this.requestedPage);
231 }
232 if ((this.requestedAtom!=null) && (this.atomContainer !=null)) {
233 File file = new File();
234 file.setProperties(this.atomContainer);
235 file.setAtom(this.requestedAtom);
236 this.request.setAttribute(Aggregator.FILE,file);
237 }
238 this.request.setAttribute(Aggregator.HANDLE,this.URI);
239 this.request.setAttribute(Aggregator.HIERARCHY_MANAGER,this.hierarchyManager);
240 this.request.setAttribute(Aggregator.REQUEST_RECEIVER,this.requestReceiver);
241 }
242
243
244
245 }