HtmlResponse.java
6.55 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.i18n.LocaleUtil
* com.adobe.granite.xss.XSSAPI
* com.day.cq.i18n.I18n
* javax.servlet.http.HttpServletResponse
* org.apache.sling.servlets.post.AbstractPostResponse
*/
package com.adobe.granite.ui.components;
import com.adobe.granite.i18n.LocaleUtil;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.servlets.post.AbstractPostResponse;
public class HtmlResponse
extends AbstractPostResponse {
private static final String PN_DESCRIPTION = "description";
private final XSSAPI xss;
private final I18n i18n;
private final Locale locale;
private final List<Change> changes = new ArrayList<Change>();
private final List<Link> links = new ArrayList<Link>();
public HtmlResponse(XSSAPI xss, I18n i18n, Locale locale) {
this.xss = xss;
this.i18n = i18n;
this.locale = locale;
}
public void onModified(String path) {
this.onChange("^", path);
}
public void onCreated(String path) {
this.onChange("+", path);
}
public void onDeleted(String path) {
if (path != null) {
this.onChange("-", path);
}
}
public void onMoved(String srcPath, String dstPath) {
this.onChange(">", srcPath, dstPath);
}
public void onCopied(String srcPath, String dstPath) {
this.onChange("*", srcPath, dstPath);
}
public /* varargs */ void onChange(String type, String ... arguments) {
this.changes.add(new Change(type, arguments));
}
public void setDescription(String description) {
this.setProperty("description", (Object)description);
}
public void setGeneralError(int code) {
this.setStatus(code, this.i18n.get("The server has problem processing your request."));
this.setTitle(this.i18n.get("Error"));
this.setDescription(this.i18n.get("The server has problem processing your request."));
}
public void addRedirectLink(String href, String text) {
this.addLink("foundation-form-response-redirect", href, text);
}
public void addLink(String rel, String href, String text) {
this.links.add(new Link(rel, href, text));
}
protected void doSend(HttpServletResponse res) throws IOException {
res.setContentType("text/html");
res.setCharacterEncoding("UTF-8");
PrintWriter out = res.getWriter();
out.write("<!DOCTYPE html>\n");
out.write("<html lang='" + LocaleUtil.toRFC4646((Locale)this.locale) + "'>\n");
out.write("<head>\n");
Object title = this.getProperty("title");
if (title != null) {
this.write(out, "title", title.toString(), true);
}
out.write("</head>\n");
out.write("<body>\n");
if (title != null) {
this.write(out, "h1", title.toString(), true);
}
out.write("<dl>\n");
this.writeDefinition(out, "foundation-form-response-status-code", this.i18n.get("Status"), this.getProperty("status.code"));
this.writeDefinition(out, "foundation-form-response-status-message", this.i18n.get("Message"), this.getProperty("status.message"));
this.writeDefinition(out, "foundation-form-response-path", this.i18n.get("Path"), this.getProperty("path"));
this.writeDefinition(out, "foundation-form-response-title", this.i18n.get("Title"), title);
this.writeDefinition(out, "foundation-form-response-description", this.i18n.get("Description"), this.getProperty("description"));
if (!this.changes.isEmpty()) {
out.write("<dt class='foundation-form-response-changelog'>");
out.write(this.i18n.get("Change Log"));
out.write("</dt>\n");
out.write("<dd>\n");
out.write("<ol>\n");
for (Change change : this.changes) {
out.write("<li>");
this.writeChange(out, change);
out.write("</li>\n");
}
out.write("</ol>\n");
out.write("</dd>\n");
}
out.write("</dl>\n");
if (!this.links.isEmpty()) {
this.write(out, "h2", this.i18n.get("Links"), true);
out.write("<ul class='foundation-form-response-links'>\n");
for (Link link : this.links) {
out.write("<li>");
this.writeLink(out, link);
out.write("</li>\n");
}
out.write("</ul>\n");
}
out.write("</body>\n");
out.write("</html>\n");
}
private void writeDefinition(Writer out, String rel, String term, Object value) throws IOException {
if (value == null) {
return;
}
out.write("<dt class='" + this.xss.encodeForHTMLAttr(rel) + "'>");
out.write(this.xss.encodeForHTML(term));
out.write("</dt>\n");
out.write("<dd>");
out.write(this.xss.filterHTML(value.toString()));
out.write("</dd>\n");
}
private void writeChange(Writer out, Change change) throws IOException {
this.write(out, "span", change.type, false);
out.write("(");
for (int i = 0; i < change.arguments.length; ++i) {
if (i > 0) {
out.write(", ");
}
this.write(out, "span", change.arguments[i], false);
}
out.write(")");
}
private void writeLink(Writer out, Link link) throws IOException {
out.write("<a class='" + this.xss.encodeForHTMLAttr(link.rel) + "' href='" + this.xss.getValidHref(link.href) + "'>");
out.write(this.xss.encodeForHTML(link.text));
out.write("</a>");
}
private void write(Writer out, String tag, String text, boolean newline) throws IOException {
out.write("<" + tag + ">" + this.xss.encodeForHTML(text) + "</" + tag + ">");
if (newline) {
out.write("\n");
}
}
private class Link {
String rel;
String href;
String text;
public Link(String rel, String href, String text) {
this.rel = rel;
this.href = href;
this.text = text;
}
}
private class Change {
String type;
String[] arguments;
public /* varargs */ Change(String type, String ... arguments) {
this.type = type;
this.arguments = arguments;
}
}
}