![JAR search and dependency download from the Maven repository](/logo.png)
com.adobe.granite.ui.components.HtmlResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
/*************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
* Copyright 2013 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
**************************************************************************/
package com.adobe.granite.ui.components;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.servlets.post.AbstractPostResponse;
import com.adobe.granite.i18n.LocaleUtil;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
/**
* The post response which produces HTML containing metadata, such as status
* code, title, so that the client can parse it.
*/
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 changes = new ArrayList();
private final List links = new ArrayList();
public HtmlResponse(@Nonnull XSSAPI xss, @Nonnull I18n i18n, @Nonnull Locale locale) {
this.xss = xss;
this.i18n = i18n;
this.locale = locale;
}
@Override
public void onModified(String path) {
onChange("^", path);
}
@Override
public void onCreated(String path) {
onChange("+", path);
}
@Override
public void onDeleted(String path) {
if (path != null) {
onChange("-", path);
}
}
@Override
public void onMoved(String srcPath, String dstPath) {
onChange(">", srcPath, dstPath);
}
@Override
public void onCopied(String srcPath, String dstPath) {
onChange("*", srcPath, dstPath);
}
@SuppressWarnings("null")
@Override
public void onChange(String type, String... arguments) {
changes.add(new Change(type, arguments));
}
public void setDescription(@CheckForNull String description) {
setProperty(PN_DESCRIPTION, description);
}
/**
* Sets the general purpose error message using the given status code. The title
* and description will be set automatically.
*
* @param code
* the status code
*/
public void setGeneralError(int code) {
setStatus(code, i18n.get("The server has problem processing your request."));
setTitle(i18n.get("Error"));
setDescription(i18n.get("The server has problem processing your request."));
}
/**
* Adds a redirect link to indicate where the client should go after the post.
* Note that you SHOULD call this method once, otherwise would be a duplicate.
*
* @param href
* the link
* @param text
* the link text
*/
public void addRedirectLink(@Nonnull String href, @Nonnull String text) {
addLink("foundation-form-response-redirect", href, text);
}
/**
* Adds a link to the response. It is for the server to give affordance to the
* client.
*
* @param rel
* relationship attribute
* @param href
* the link
* @param text
* the link text
*/
public void addLink(@Nonnull String rel, @Nonnull String href, @Nonnull String text) {
links.add(new Link(rel, href, text));
}
@SuppressWarnings("null")
@Override
protected void doSend(HttpServletResponse res) throws IOException {
res.setContentType("text/html");
res.setCharacterEncoding("UTF-8");
Writer out = res.getWriter();
out.write("\n");
out.write("\n");
out.write("\n");
Object title = getProperty(PN_TITLE);
if (title != null) {
write(out, "title", title.toString(), true);
}
out.write("\n");
out.write("\n");
if (title != null) {
write(out, "h1", title.toString(), true);
}
out.write("\n");
writeDefinition(out, "foundation-form-response-status-code", i18n.get("Status"), getProperty(PN_STATUS_CODE));
writeDefinition(out, "foundation-form-response-status-message", i18n.get("Message"),
getProperty(PN_STATUS_MESSAGE));
writeDefinition(out, "foundation-form-response-path", i18n.get("Path"), getProperty(PN_PATH));
writeDefinition(out, "foundation-form-response-title", i18n.get("Title"), title);
writeDefinition(out, "foundation-form-response-description", i18n.get("Description"),
getProperty(PN_DESCRIPTION));
// TODO add other properties
if (!changes.isEmpty()) {
out.write("- ");
out.write(i18n.get("Change Log"));
out.write("
\n");
out.write("- \n");
out.write("
\n");
for (Change change : changes) {
out.write("- ");
writeChange(out, change);
out.write("
\n");
}
out.write("
\n");
out.write(" \n");
}
out.write("
\n");
if (!links.isEmpty()) {
write(out, "h2", i18n.get("Links"), true);
out.write("\n");
for (Link link : links) {
out.write("- ");
writeLink(out, link);
out.write("
\n");
}
out.write("
\n");
}
out.write("\n");
out.write("\n");
}
private void writeDefinition(@Nonnull Writer out, @Nonnull String rel, @Nonnull String term,
@CheckForNull Object value) throws IOException {
if (value == null) {
return;
}
out.write("");
out.write(xss.encodeForHTML(term));
out.write(" \n");
out.write("");
out.write(xss.filterHTML(value.toString()));
out.write(" \n");
}
@SuppressWarnings("null")
private void writeChange(@Nonnull Writer out, @Nonnull Change change) throws IOException {
write(out, "span", change.type, false);
out.write("(");
for (int i = 0; i < change.arguments.length; i++) {
if (i > 0) {
out.write(", ");
}
write(out, "span", change.arguments[i], false);
}
out.write(")");
}
private void writeLink(@Nonnull Writer out, @Nonnull Link link) throws IOException {
out.write("");
out.write(xss.encodeForHTML(link.text));
out.write("");
}
private void write(@Nonnull Writer out, @Nonnull String tag, @Nonnull String text, boolean newline)
throws IOException {
out.write("<" + tag + ">" + xss.encodeForHTML(text) + "" + tag + ">");
if (newline) {
out.write("\n");
}
}
private class Change {
@Nonnull
String type;
@Nonnull
String[] arguments;
public Change(@Nonnull String type, @Nonnull String... arguments) {
this.type = type;
this.arguments = arguments;
}
}
private class Link {
@Nonnull
String rel;
@Nonnull
String href;
@Nonnull
String text;
public Link(@Nonnull String rel, @Nonnull String href, @Nonnull String text) {
this.rel = rel;
this.href = href;
this.text = text;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy