ContentServlet.java
5.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentElement
* com.adobe.cq.dam.cfm.ContentFragment
* com.adobe.cq.dam.cfm.ContentFragmentException
* com.adobe.cq.dam.cfm.ContentVariation
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.apache.sling.xss.XSSAPI
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl.servlets;
import com.adobe.cq.dam.cfm.ContentElement;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.ContentVariation;
import com.adobe.cq.dam.cfm.impl.transformer.ContentTypeConverter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.xss.XSSAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(selectors={"cfm.content"}, extensions={"json", "html"}, resourceTypes={"dam:Asset"})
public class ContentServlet
extends SlingSafeMethodsServlet {
private final Logger log;
@Reference
private XSSAPI xss;
public ContentServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Iterator elements;
String htmlContent;
Resource assetRsc = request.getResource();
ContentFragment cf = (ContentFragment)assetRsc.adaptTo(ContentFragment.class);
if (cf == null) {
response.sendError(400, "Resource '" + assetRsc.getName() + "' is no content fragment.");
return;
}
String elementParam = request.getParameter("element");
String variationParam = request.getParameter("variation");
ContentElement element = elementParam != null ? cf.getElement(elementParam) : ((elements = cf.getElements()).hasNext() ? (ContentElement)elements.next() : null);
String content = "";
String contentType = null;
if (element != null) {
if (variationParam != null) {
ContentVariation variation = element.getVariation(variationParam);
if (variation != null) {
content = variation.getContent();
contentType = variation.getContentType();
}
} else {
content = element.getContent();
contentType = element.getContentType();
}
}
String extension = request.getRequestPathInfo().getExtension();
String responseType = "application/json";
boolean isHTML = false;
if ("html".equals(extension)) {
responseType = "text/html";
isHTML = true;
}
ContentTypeConverter converter = new ContentTypeConverter();
try {
htmlContent = converter.convertToHTML(content, contentType, this.xss);
}
catch (ContentFragmentException cfe) {
response.sendError(500, "Could not create HTML for content fragment.");
this.log.error("Could not convert text of type '" + contentType + "' to HTML.", (Throwable)cfe);
return;
}
response.setStatus(200);
response.setContentType(responseType);
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
if (isHTML) {
out.print(htmlContent);
} else {
JSONWriter jw = new JSONWriter((Writer)out);
try {
jw.object();
jw.key("content").value((Object)content);
jw.key("contentType").value((Object)contentType);
jw.key("htmlContent").value((Object)htmlContent);
jw.endObject();
}
catch (JSONException je) {
throw new IOException("Could not output JSON", (Throwable)je);
}
}
}
protected void bindXss(XSSAPI xSSAPI) {
this.xss = xSSAPI;
}
protected void unbindXss(XSSAPI xSSAPI) {
if (this.xss == xSSAPI) {
this.xss = null;
}
}
}