ContentServlet.java 5.04 KB
/*
 * 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.ContentVariation
 *  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.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  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.ContentVariation;
import com.adobe.cq.dam.cfm.impl.transformer.ContentTypeConverter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
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.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.xss.XSSAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(selectors={"cfm.content"}, extensions={"json", "html"}, resourceTypes={"dam:Asset"})
public class ContentServlet
extends SlingSafeMethodsServlet {
    private final Logger log;
    @Reference
    private XSSAPI xss;

    public ContentServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Iterator elements;
        String htmlContent;
        Resource assetRsc = request.getResource();
        ContentFragment cf = (ContentFragment)assetRsc.adaptTo(ContentFragment.class);
        if (cf == null) {
            response.sendError(400, "Resource '" + assetRsc.getName() + "' is no content fragment.");
            return;
        }
        String elementParam = request.getParameter("element");
        String variationParam = request.getParameter("variation");
        ContentElement element = elementParam != null ? cf.getElement(elementParam) : ((elements = cf.getElements()).hasNext() ? (ContentElement)elements.next() : null);
        String content = "";
        String contentType = null;
        if (element != null) {
            if (variationParam != null) {
                ContentVariation variation = element.getVariation(variationParam);
                if (variation != null) {
                    content = variation.getContent();
                    contentType = variation.getContentType();
                }
            } else {
                content = element.getContent();
                contentType = element.getContentType();
            }
        }
        String extension = request.getRequestPathInfo().getExtension();
        String responseType = "application/json";
        boolean isHTML = false;
        if ("html".equals(extension)) {
            responseType = "text/html";
            isHTML = true;
        }
        ContentTypeConverter converter = new ContentTypeConverter();
        try {
            htmlContent = converter.convertToHTML(content, contentType, this.xss);
        }
        catch (ContentFragmentException cfe) {
            response.sendError(500, "Could not create HTML for content fragment.");
            this.log.error("Could not convert text of type '" + contentType + "' to HTML.", (Throwable)cfe);
            return;
        }
        response.setStatus(200);
        response.setContentType(responseType);
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        if (isHTML) {
            out.print(htmlContent);
        } else {
            JSONWriter jw = new JSONWriter((Writer)out);
            try {
                jw.object();
                jw.key("content").value((Object)content);
                jw.key("contentType").value((Object)contentType);
                jw.key("htmlContent").value((Object)htmlContent);
                jw.endObject();
            }
            catch (JSONException je) {
                throw new IOException("Could not output JSON", (Throwable)je);
            }
        }
    }

    protected void bindXss(XSSAPI xSSAPI) {
        this.xss = xSSAPI;
    }

    protected void unbindXss(XSSAPI xSSAPI) {
        if (this.xss == xSSAPI) {
            this.xss = null;
        }
    }
}