Response.java 6.75 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.http.Cookie
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang.StringUtils
 *  org.slf4j.Logger
 */
package com.adobe.cq.dam.s7imaging.impl.gfx;

import com.adobe.cq.dam.s7imaging.impl.gfx.S7ImageServerRenderer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

public class Response
implements HttpServletResponse {
    private final Logger log;
    private final OutputStream out;
    private final ServletOutputStream servletOut;
    private final String assetPath;
    private final String modifiers;
    private final S7ImageServerRenderer.Pipe pipe;
    private int status = -1;
    private int contentLength = -1;
    private String contentType;

    public Response(S7ImageServerRenderer.Pipe pipe, String assetPath, String modifiers, Logger log) {
        this.pipe = pipe;
        this.out = pipe.outputStream();
        this.assetPath = assetPath;
        this.modifiers = modifiers;
        this.log = log;
        this.servletOut = new ErrorRecordingServletOutputStream();
    }

    public void close() {
        IOUtils.closeQuietly((OutputStream)this.servletOut);
    }

    public ServletOutputStream getOutputStream() throws IOException {
        return this.servletOut;
    }

    public void setStatus(int sc) {
        this.log.debug("platformserver returned status   : {}", (Object)sc);
        this.status = sc;
    }

    public void setContentType(String type) {
        this.log.debug("platformserver set content type  : {}", (Object)type);
        this.contentType = type;
    }

    public void setContentLength(int len) {
        this.log.debug("platformserver set content length: {}", (Object)len);
        this.contentLength = len;
    }

    public void setDateHeader(String name, long date) {
        if (this.log.isDebugEnabled()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(date);
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            this.log.debug("platformserver set date header   : {}: {}", (Object)name, (Object)dateFormat.format(calendar.getTime()));
        }
    }

    public void setHeader(String name, String value) {
        this.log.debug("platformserver set header        : {}: {}", (Object)name, (Object)value);
    }

    public void addCookie(Cookie cookie) {
    }

    public boolean containsHeader(String name) {
        return false;
    }

    public String encodeURL(String url) {
        return null;
    }

    public String encodeRedirectURL(String url) {
        return null;
    }

    @Deprecated
    public String encodeUrl(String url) {
        return null;
    }

    @Deprecated
    public String encodeRedirectUrl(String url) {
        return null;
    }

    public void sendError(int sc, String msg) throws IOException {
    }

    public void sendError(int sc) throws IOException {
    }

    public void sendRedirect(String location) throws IOException {
    }

    public void addDateHeader(String name, long date) {
    }

    public void addHeader(String name, String value) {
    }

    public void setIntHeader(String name, int value) {
    }

    public void addIntHeader(String name, int value) {
    }

    @Deprecated
    public void setStatus(int sc, String sm) {
    }

    public String getCharacterEncoding() {
        return null;
    }

    public String getContentType() {
        return null;
    }

    public PrintWriter getWriter() throws IOException {
        return new PrintWriter((OutputStream)this.servletOut);
    }

    public void setCharacterEncoding(String charset) {
    }

    public void setBufferSize(int size) {
    }

    public int getBufferSize() {
        return 0;
    }

    public void flushBuffer() throws IOException {
    }

    public void resetBuffer() {
    }

    public boolean isCommitted() {
        return false;
    }

    public void reset() {
    }

    public void setLocale(Locale loc) {
    }

    public Locale getLocale() {
        return null;
    }

    private class ErrorRecordingServletOutputStream
    extends ServletOutputStream {
        private static final int MAX_ERROR_BODY_SIZE = 4096;
        private ByteArrayOutputStream errorBody;

        private ErrorRecordingServletOutputStream() {
        }

        public void write(int b) throws IOException {
            if (Response.this.status != 200) {
                if (Response.this.contentType != null && Response.this.contentType.startsWith("text/")) {
                    if (this.errorBody == null) {
                        if (Response.this.contentLength < 0) {
                            Response.this.contentLength = 4096;
                        } else if (Response.this.contentLength > 4096) {
                            Response.this.contentLength = 4096;
                        }
                        this.errorBody = new ByteArrayOutputStream(Response.this.contentLength);
                    }
                    if (this.errorBody.size() < Response.this.contentLength) {
                        this.errorBody.write(b);
                    }
                }
            } else {
                Response.this.out.write(b);
            }
        }

        public void close() throws IOException {
            if (Response.this.status != 200) {
                String msg;
                if (this.errorBody != null) {
                    String errorMessage = StringUtils.trim((String)new String(this.errorBody.toByteArray(), "utf-8"));
                    msg = MessageFormat.format("Platform Server could not render image, returned status {0} for asset {1} with modifiers {2}, error message: {3}", Response.this.status, Response.this.assetPath, Response.this.modifiers, errorMessage);
                } else {
                    msg = MessageFormat.format("Platform Server could not render image, returned status {0} for asset {1} with modifiers {2}, no error message", Response.this.status, Response.this.assetPath, Response.this.modifiers);
                }
                Response.this.pipe.sendException(new IOException(msg));
            }
            Response.this.out.close();
        }
    }

}