1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.core;
35
36 import info.magnolia.cms.beans.runtime.File;
37 import info.magnolia.context.MgnlContext;
38
39 import java.io.UnsupportedEncodingException;
40 import java.net.URLDecoder;
41 import java.util.Locale;
42
43 import org.apache.commons.lang.StringUtils;
44
45
46
47
48
49
50 public class AggregationState {
51 private String characterEncoding;
52 private String originalURI;
53 private String originalURL;
54 private String originalBrowserURI;
55 private String originalBrowserURL;
56 private String currentURI;
57 private String queryString;
58 private String extension;
59 private File file;
60 private String handle;
61 private Content mainContent;
62 private Content currentContent;
63 private String repository;
64 private String selector;
65 private String[] selectors = new String[0];
66 private String templateName;
67 private Locale locale;
68 private boolean isPreviewMode;
69 private Channel channel = new Channel();
70
71 public void setOriginalURI(String originalURI) {
72 final String strippedURI = stripContextPathIfExists(originalURI);
73 if (this.originalURI != null && !this.originalURI.equals(strippedURI)) {
74 throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
75 }
76 this.originalURI = strippedURI;
77 }
78
79 public void setOriginalBrowserURI(String originalBrowserURI) {
80 final String strippedURI = stripContextPathIfExists(originalBrowserURI);
81 if (this.originalBrowserURI != null && !this.originalBrowserURI.equals(strippedURI)) {
82 throw new IllegalStateException("Original URI can only be set once ! Existing value is \"" + this.originalURI + "\", tried to replace it with \"" + strippedURI + "\"");
83 }
84 this.originalBrowserURI= strippedURI;
85 }
86
87 public void setCurrentURI(String currentURI) {
88 this.currentURI = stripContextPathIfExists(currentURI);
89 }
90
91 public void setQueryString(String queryString) {
92 this.queryString = queryString;
93 }
94
95
96
97
98 public String getQueryString() {
99 return queryString;
100 }
101
102
103
104
105
106 public String getCurrentURI() {
107 return currentURI;
108 }
109
110 public String getCharacterEncoding() {
111 if (characterEncoding == null) {
112 throw new IllegalStateException("Character encoding hasn't been setup in AggregationState yet !");
113 }
114 return characterEncoding;
115 }
116
117
118
119
120
121
122
123 public String getOriginalURI() {
124 return originalURI;
125 }
126
127 public String getOriginalURL() {
128 return originalURL;
129 }
130
131 public void setOriginalURL(String originalURL) {
132 this.originalURL = originalURL;
133 }
134
135 public String getOriginalBrowserURI() {
136 return originalBrowserURI;
137 }
138
139 public String getOriginalBrowserURL() {
140 return originalBrowserURL;
141 }
142
143 public void setOriginalBrowserURL(String originalBrowserURL) {
144 this.originalBrowserURL = originalBrowserURL;
145 }
146
147 public void setCharacterEncoding(String characterEncoding) {
148 this.characterEncoding = characterEncoding;
149 }
150
151 public String getExtension() {
152 return extension;
153 }
154
155 public void setExtension(String extension) {
156 this.extension = extension;
157 }
158
159 public File getFile() {
160 return file;
161 }
162
163 public void setFile(File file) {
164 this.file = file;
165 }
166
167 public String getHandle() {
168 return handle;
169 }
170
171 public void setHandle(String handle) {
172 this.handle = handle;
173 }
174
175 public Content getMainContent() {
176 return mainContent;
177 }
178
179 public void setMainContent(Content mainContent) {
180 this.mainContent = mainContent;
181 }
182
183 public Content getCurrentContent() {
184 return currentContent;
185 }
186
187 public void setCurrentContent(Content currentContent) {
188 this.currentContent = currentContent;
189 }
190
191 public String getRepository() {
192 return repository;
193 }
194
195 public void setRepository(String repository) {
196 this.repository = repository;
197 }
198
199
200
201
202
203
204
205
206
207 public String getSelector() {
208 return selector;
209 }
210
211
212
213
214
215 public void setSelector(String selector) {
216 try {
217 this.selector = URLDecoder.decode(selector, getCharacterEncoding());
218 }
219 catch (UnsupportedEncodingException e) {
220 this.selector = selector;
221 }
222
223 if(StringUtils.isNotEmpty(selector)) {
224 selectors = this.selector.split(Path.SELECTOR_DELIMITER);
225 }
226 for(String sel : selectors) {
227 final String[] splitSelector = sel.split("=");
228 if(splitSelector.length == 2) {
229 MgnlContext.setAttribute(splitSelector[0], splitSelector[1]);
230 }
231 }
232 }
233
234 public String getTemplateName() {
235 return templateName;
236 }
237
238 public void setTemplateName(String templateName) {
239 this.templateName = templateName;
240 }
241
242
243
244
245
246 public Locale getLocale() {
247 if (locale == null) {
248 return MgnlContext.getLocale();
249 }
250
251 return locale;
252 }
253
254
255
256
257 public void setLocale(Locale locale) {
258 this.locale = locale;
259 }
260
261 public boolean isPreviewMode() {
262 return isPreviewMode;
263 }
264
265 public void setPreviewMode(boolean previewMode) {
266 isPreviewMode = previewMode;
267 }
268
269 public Channel getChannel() {
270 return channel;
271 }
272
273 public void setChannel(Channel channel) {
274 this.channel = channel;
275 }
276
277
278
279
280
281
282 protected String stripContextPathIfExists(String uri) {
283
284 String contextPath = MgnlContext.getContextPath();
285 if (uri != null && uri.startsWith(contextPath + "/")) {
286 return StringUtils.removeStart(uri, contextPath);
287 }
288 return uri;
289 }
290
291
292
293
294 public void resetURIs() {
295 this.originalURI = null;
296 this.originalURL = null;
297 this.originalBrowserURI = null;
298 this.originalBrowserURL = null;
299 this.currentURI = null;
300 }
301
302
303
304
305
306
307
308 public String[] getSelectors() {
309 return selectors;
310 }
311 }