All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.granite.ui.components.HtmlResponse Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
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.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(XSSAPI xss, I18n i18n, Locale locale) {
        this.xss = xss;
        this.i18n = i18n;
        this.locale = locale;
    }

    public void onModified(String path) {
        onChange("^", path);
    }

    public void onCreated(String path) {
        onChange("+", path);
    }

    public void onDeleted(String path) {
        if (path != null) {
            onChange("-", path);
        }
    }

    public void onMoved(String srcPath, String dstPath) {
        onChange(">", srcPath, dstPath);
    }

    public void onCopied(String srcPath, String dstPath) {
        onChange("*", srcPath, dstPath);
    }

    public void onChange(String type, String... arguments) {
        changes.add(new Change(type, arguments));
    }

    public void setDescription(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(String href, 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(String rel, String href, String text) {
        links.add(new Link(rel, href, text));
    }

    @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("
  1. "); writeChange(out, change); out.write("
  2. \n"); } out.write("
\n"); out.write("
\n"); } out.write("
\n"); if (!links.isEmpty()) { write(out, "h2", i18n.get("Links"), true); out.write("\n"); } out.write("\n"); out.write("\n"); } private void writeDefinition(Writer out, String rel, String term, 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"); } private void writeChange(Writer out, 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(Writer out, Link link) throws IOException { out.write(""); out.write(xss.encodeForHTML(link.text)); out.write(""); } private void write(Writer out, String tag, String text, boolean newline) throws IOException { out.write("<" + tag + ">" + xss.encodeForHTML(text) + ""); if (newline) { out.write("\n"); } } private class Change { String type; String[] arguments; public Change(String type, String... arguments) { this.type = type; this.arguments = arguments; } } 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; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy