AbstractFeed.java
9.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.servlet.RequestDispatcher
* javax.servlet.ServletException
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceNotFoundException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.jcr.resource.JcrPropertyMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.commons.feed;
import com.day.cq.commons.SimpleXml;
import com.day.cq.commons.feed.Feed;
import com.day.cq.commons.feed.StringResponseWrapper;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceNotFoundException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.jcr.resource.JcrPropertyMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractFeed
implements Feed {
final Logger log = LoggerFactory.getLogger(Feed.class);
static final String[] textNodes = new String[3];
SimpleXml xml;
SlingHttpServletRequest request;
SlingHttpServletResponse response;
String title;
String description;
String tags;
String language;
String author;
String publishedDate;
String nodePath;
String baseUrl;
long fileSize;
String mimeType;
public AbstractFeed(Resource res, SlingHttpServletRequest req, SlingHttpServletResponse resp) throws RepositoryException {
this.request = req;
this.response = resp;
if (res == null) {
res = this.request.getResource();
}
if (ResourceUtil.isNonExistingResource((Resource)res)) {
throw new ResourceNotFoundException("No data to render.");
}
Node node = (Node)res.adaptTo(Node.class);
if (node == null) {
throw new RepositoryException("No node found for resource: " + res.getPath());
}
Node propNode = node;
this.nodePath = node.getPath();
if (node.getName().equals("jcr:content")) {
this.nodePath = node.getParent().getPath();
} else if (node.hasNode("jcr:content")) {
propNode = node.getNode("jcr:content");
}
JcrPropertyMap props = new JcrPropertyMap(propNode);
if (node.isNodeType("nt:file")) {
this.fileSize = props.get("jcr:data", Property.class) != null ? ((Property)props.get("jcr:data", Property.class)).getLength() : 0;
this.mimeType = (String)props.get("jcr:mimeType", (Object)"application/octet-stream");
} else {
this.mimeType = "text/html";
}
this.baseUrl = this.getUrlPrefix();
this.baseUrl = this.baseUrl + Text.getRelativeParent((String)this.nodePath, (int)1);
this.title = (String)props.get("jcr:title", (Object)node.getName());
this.description = (String)props.get("jcr:description", (Object)"");
this.language = (String)props.get("jcr:language", (Object)"");
this.author = (String)props.get("jcr:createdBy", (Object)"");
this.publishedDate = this.formatDate((Calendar)props.get("jcr:created", props.get("cq:lastModified", (Object)Calendar.getInstance())));
this.tags = Text.implode((String[])((String[])props.get("cq:tags", (Object)new String[0])), (String)",");
this.title = Text.escapeXml((String)this.title);
this.author = Text.escapeXml((String)this.author);
this.tags = Text.escapeXml((String)this.tags);
}
@Override
public String getContentType() {
return "text/xml";
}
@Override
public String getCharacterEncoding() {
return "utf-8";
}
@Override
public void printEntry(Resource res) throws IOException {
this.initXml();
try {
this.request.setAttribute("com.day.cq.wcm.api.components.ComponentContext/bypass", (Object)"true");
StringResponseWrapper wrapper = new StringResponseWrapper(this.response);
this.request.getRequestDispatcher(this.getFeedEntryPath(res)).include((ServletRequest)this.request, (ServletResponse)wrapper);
this.xml.getWriter().print(wrapper.getString());
}
catch (ServletException se) {
throw new IOException(se.getMessage());
}
}
@Override
public void printChildEntries() throws IOException {
this.printEntries(this.request.getResourceResolver().listChildren(this.request.getResource()));
}
@Override
public void printChildEntries(int max) throws IOException {
Iterator iter = this.request.getResourceResolver().listChildren(this.request.getResource());
ArrayList<Resource> children = new ArrayList<Resource>();
while (iter.hasNext()) {
Resource res = (Resource)iter.next();
try {
if (((Node)res.adaptTo(Node.class)).getName().equals("jcr:content")) {
continue;
}
}
catch (RepositoryException re) {
throw new IOException(re.getMessage());
}
children.add(res);
}
this.printEntries(children.iterator(), max);
}
@Override
public void printEntries(Iterator<Resource> iter) throws IOException {
this.printEntries(iter, 0);
}
@Override
public void printEntries(Iterator<Resource> iter, int max) throws IOException {
int i = 0;
while (iter.hasNext()) {
this.printEntry(iter.next());
if (max <= 0 || ++i <= max) continue;
break;
}
}
@Override
public void printFooter() throws IOException {
this.initXml();
this.xml.closeDocument();
}
void initXml() throws IOException {
if (this.xml == null) {
this.xml = new SimpleXml(this.response.getWriter());
}
}
String getFeedEntryPath(Resource res) {
return res.getPath() + ".feedentry.xml";
}
String formatDate(Calendar calendar) {
try {
calendar.setTimeZone(new SimpleTimeZone(0, "UTC"));
return String.format("%1$tFT%1$tTZ", calendar);
}
catch (Exception e) {
return "";
}
}
boolean isFile() {
return this.fileSize > 0;
}
String getFileSize() {
return "" + this.fileSize + "";
}
String getMimeType() {
return this.mimeType;
}
String getTitle() {
return this.title;
}
String getSummary() {
return this.description;
}
String getDescription() {
return this.description;
}
String getTags() {
return this.tags;
}
String getLanguage() {
return this.language;
}
String getAuthorName() {
return this.author;
}
String getAuthorEmail() {
return !"".equals(this.author) ? this.author + "@" + this.request.getServerName() : "";
}
String getPublishedDate() {
return this.publishedDate;
}
String getNodePath() {
return this.nodePath;
}
String getUrlPrefix() {
StringBuffer url = new StringBuffer();
url.append(this.request.getScheme());
url.append("://");
url.append(this.request.getServerName());
if (this.request.getServerPort() != 80) {
url.append(":");
url.append(this.request.getServerPort());
}
url.append(this.request.getContextPath());
return url.toString();
}
String getHtmlLink() {
return this.getUrlPrefix() + this.getNodePath() + ".html";
}
String getFeedLink() {
return this.getUrlPrefix() + this.getNodePath() + ".feed.xml";
}
String getFeedEntryLink() {
return this.getUrlPrefix() + this.getNodePath() + ".feedentry.xml";
}
String getCommentsLink() {
return this.getUrlPrefix() + this.getNodePath() + ".html#comments";
}
String getFileLink() {
return this.getUrlPrefix() + this.getNodePath();
}
String getBaseUrl() {
return this.baseUrl;
}
String getGeneratorName() {
return "CQ5";
}
String getGeneratorVersion() {
return "5.2";
}
String getGeneratorLink() {
return "http://www.day.com/communique";
}
static {
AbstractFeed.textNodes[0] = "par/text";
AbstractFeed.textNodes[1] = "par/textimage";
AbstractFeed.textNodes[2] = "blogentry";
}
}