SaveHTMLOfferContentCommand.java
8.92 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* com.day.cq.retriever.RetrieverService
* com.day.cq.retriever.RetrieverStorage
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl.servlets.commands;
import com.day.cq.analytics.testandtarget.TestandtargetCommand;
import com.day.cq.analytics.testandtarget.TestandtargetService;
import com.day.cq.analytics.testandtarget.util.OfferHelper;
import com.day.cq.i18n.I18n;
import com.day.cq.retriever.RetrieverService;
import com.day.cq.retriever.RetrieverStorage;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
@Component
@Service
public class SaveHTMLOfferContentCommand
implements TestandtargetCommand {
private static final Logger log = LoggerFactory.getLogger(SaveHTMLOfferContentCommand.class);
private static final String PARAM_CFG = "cfgpath";
private static final String PARAM_PATH = "path";
private static final String PARAM_FOLDERID = "folderid";
private static final String PARAM_CONTENT = "content";
public static final String PN_LASTPUBLISHED = "cq:tntLastPublished";
public static final String PN_FOLDERID = "cq:tntFolderId";
@Reference
private ConfigurationManagerFactory configurationManagerFactory;
@Reference
private TestandtargetService service;
@Reference
private RetrieverService retriever;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
@Override
public String getName() {
return "saveHTMLOfferContent";
}
@Override
public HtmlResponse performCommand(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
String configPath = request.getParameter("cfgpath");
String path = request.getParameter("path");
String folderId = request.getParameter("folderid");
String content = request.getParameter("content");
JSONWriter out = new JSONWriter((Writer)response.getWriter());
try {
Configuration config = null;
if (configPath != null) {
config = this.configurationManagerFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(configPath);
}
if (config == null) {
String msg = I18n.get((HttpServletRequest)request, (String)"Configuration '{0}' not found", (String)null, (Object[])new Object[]{configPath});
out.object();
out.key("error").value((Object)msg);
out.endObject();
return null;
}
Resource offerResource = request.getResourceResolver().getResource(path);
String offerName = OfferHelper.getOfferName(path);
String offerContent = content != null ? content : this.getOfferContent(request, offerResource);
Calendar lastpublished = GregorianCalendar.getInstance();
if (offerContent != null) {
this.service.createHTMLOffer(config, offerName, folderId, offerContent);
PageManager pageMgr = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
Page offerPage = pageMgr.getPage(offerResource.getPath());
Node pageContent = (Node)offerPage.getContentResource().adaptTo(Node.class);
pageContent.setProperty("cq:tntFolderId", folderId);
pageContent.setProperty("cq:tntLastPublished", lastpublished);
OfferHelper.setConfigurationPath(configPath, offerPage);
((Node)offerPage.adaptTo(Node.class)).getSession().save();
out.object();
out.key("success").value(true);
out.key("folderid").value((Object)folderId);
out.key("lastpublished").value((Object)this.sdf.format(lastpublished.getTime()));
out.endObject();
} else {
out.object();
out.key("error").value((Object)I18n.get((HttpServletRequest)request, (String)"Paragraph is empty. Create content first."));
out.endObject();
}
}
catch (Exception e) {
log.error(e.getMessage(), (Throwable)e);
try {
out.object();
out.key("error").value((Object)e.getMessage());
out.endObject();
}
catch (JSONException je) {
log.error(je.getMessage(), (Throwable)je);
}
}
return null;
}
private String getOfferContent(SlingHttpServletRequest request, Resource offerResource) throws Exception {
Resource par = request.getResourceResolver().getResource(offerResource.getPath() + "/jcr:content/par");
if (par != null) {
String baseUri = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
String uri = baseUri + offerResource.getPath() + "/jcr:content/par.html";
OfferHtmlStorage storage = new OfferHtmlStorage();
this.retriever.retrieve(uri, null, (RetrieverStorage)storage);
return storage.getContent();
}
return null;
}
protected void bindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configurationManagerFactory = configurationManagerFactory;
}
protected void unbindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configurationManagerFactory == configurationManagerFactory) {
this.configurationManagerFactory = null;
}
}
protected void bindService(TestandtargetService testandtargetService) {
this.service = testandtargetService;
}
protected void unbindService(TestandtargetService testandtargetService) {
if (this.service == testandtargetService) {
this.service = null;
}
}
protected void bindRetriever(RetrieverService retrieverService) {
this.retriever = retrieverService;
}
protected void unbindRetriever(RetrieverService retrieverService) {
if (this.retriever == retrieverService) {
this.retriever = null;
}
}
private class OfferHtmlStorage
implements RetrieverStorage {
private String content;
private OfferHtmlStorage() {
}
public String getContent() {
return this.content;
}
public void storeContent(String content) throws IOException, RepositoryException {
this.content = content;
}
public String maybeDownloadResource(String elementName, String attributeName, String resourceUri, Attributes attributes) throws IOException, RepositoryException {
return null;
}
}
}