ContentPostServlet.java
7.2 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
/*
* 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.ContentFragmentManager
* com.adobe.cq.dam.cfm.ContentVariation
* com.adobe.cq.dam.cfm.VariationTemplate
* 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.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* 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.ContentFragmentManager;
import com.adobe.cq.dam.cfm.ContentVariation;
import com.adobe.cq.dam.cfm.VariationTemplate;
import com.adobe.cq.dam.cfm.impl.transformer.ContentTypeConverter;
import java.io.IOException;
import java.io.PrintWriter;
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.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.xss.XSSAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(selectors={"cfm.content"}, resourceTypes={"dam:Asset"}, extensions={"json"}, methods={"POST"})
public class ContentPostServlet
extends SlingAllMethodsServlet {
private final Logger log;
@Reference
private ContentFragmentManager fragmentManager;
@Reference
private XSSAPI xss;
public ContentPostServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ContentElement element;
String content = request.getParameter("content");
String contentType = request.getParameter("contentType");
String targetTypeOp = request.getParameter(":targetType");
if (content != null && contentType != null && targetTypeOp != null) {
ContentTypeConverter converter = new ContentTypeConverter();
try {
String html = converter.convertToHTML(content, contentType, this.xss);
content = converter.convertToType(html, targetTypeOp);
contentType = targetTypeOp;
}
catch (ContentFragmentException cfe) {
response.sendError(500, "Could not convert to requested content type.");
this.log.error("Could not convert type '" + contentType + "' to '" + targetTypeOp + "'.", (Throwable)cfe);
return;
}
}
Resource assetRsc = request.getResource();
ResourceResolver resolver = request.getResourceResolver();
ContentFragment fragment = (ContentFragment)assetRsc.adaptTo(ContentFragment.class);
if (fragment == null) {
response.sendError(400, "Resource '" + assetRsc.getName() + "' is no content fragment.");
return;
}
String elementParam = request.getParameter("element");
String variationParam = request.getParameter("variation");
if (elementParam != null) {
element = fragment.getElement(elementParam);
} else {
Iterator elements = fragment.getElements();
ContentElement contentElement = element = elements.hasNext() ? (ContentElement)elements.next() : null;
}
if (element == null) {
response.sendError(404, "No element '" + elementParam + "' defined.");
return;
}
boolean isError = false;
String errMsg = null;
if (variationParam != null) {
String name = variationParam;
ContentVariation variation = element.getVariation(variationParam);
if (variation == null) {
String description;
String title = request.getParameter("title");
if (title == null) {
title = variationParam;
name = null;
}
description = (description = request.getParameter("description")) != null ? description : "";
try {
fragment.createVariation(name, title, description);
}
catch (ContentFragmentException cfe) {
isError = true;
errMsg = "Could not create variation '" + variationParam + "'.";
}
} else {
if (content == null || contentType == null) {
response.sendError(400, "No content and/or type specified.");
return;
}
try {
variation.setContent(content, contentType);
}
catch (ContentFragmentException cfe) {
isError = true;
errMsg = "Could not update variation on server.";
}
}
} else {
if (content == null || contentType == null) {
response.sendError(400, "No content and/or type specified.");
return;
}
try {
element.setContent(content, contentType);
}
catch (ContentFragmentException cfe) {
isError = true;
errMsg = "Could not update element on server.";
}
}
if (!isError) {
try {
resolver.commit();
}
catch (PersistenceException pe) {
isError = true;
errMsg = "Could not persist changes.";
}
}
if (isError) {
resolver.refresh();
response.sendError(500, errMsg);
return;
}
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("{\"success\":true}");
}
protected void bindFragmentManager(ContentFragmentManager contentFragmentManager) {
this.fragmentManager = contentFragmentManager;
}
protected void unbindFragmentManager(ContentFragmentManager contentFragmentManager) {
if (this.fragmentManager == contentFragmentManager) {
this.fragmentManager = null;
}
}
protected void bindXss(XSSAPI xSSAPI) {
this.xss = xSSAPI;
}
protected void unbindXss(XSSAPI xSSAPI) {
if (this.xss == xSSAPI) {
this.xss = null;
}
}
}