BufferedResponse.java 2.94 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletOutputStream
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper
 */
package com.adobe.cq.mcm.campaign.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.servlet.ServletOutputStream;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;

public class BufferedResponse
extends SlingHttpServletResponseWrapper {
    private BufferingOutputStream os;
    private BufferingWriter pw;

    public BufferedResponse(SlingHttpServletResponse response) {
        super(response);
    }

    public PrintWriter getWriter() throws IOException {
        if (this.pw == null) {
            if (this.os != null) {
                throw new IllegalStateException("Output stream already obtained.");
            }
            this.pw = new BufferingWriter(new StringWriter());
        }
        return this.pw;
    }

    public ServletOutputStream getOutputStream() throws IOException {
        if (this.os == null) {
            if (this.pw != null) {
                throw new IllegalStateException("Print writer already obtained.");
            }
            this.os = new BufferingOutputStream();
        }
        return this.os;
    }

    public String getString() throws UnsupportedEncodingException {
        if (this.pw != null) {
            return this.pw.getString();
        }
        if (this.os != null) {
            return this.os.getString();
        }
        return "";
    }

    public void forward() throws IOException {
        SlingHttpServletResponse wrapped = this.getSlingResponse();
        String toForward = this.pw.getString();
        PrintWriter writer = wrapped.getWriter();
        writer.print(toForward);
        writer.flush();
    }

    class BufferingWriter
    extends PrintWriter {
        private StringWriter actualWriter;

        public BufferingWriter(StringWriter actualWriter) {
            super(actualWriter);
            this.actualWriter = actualWriter;
        }

        public String getString() {
            return this.actualWriter.toString();
        }
    }

    class BufferingOutputStream
    extends ServletOutputStream {
        private ByteArrayOutputStream bos;

        public BufferingOutputStream() {
            this.bos = new ByteArrayOutputStream();
        }

        public void write(int b) throws IOException {
            this.bos.write(b);
        }

        public String getString() throws UnsupportedEncodingException {
            return this.bos.toString(BufferedResponse.this.getCharacterEncoding());
        }

        public void close() throws IOException {
            this.bos.close();
            super.close();
        }
    }

}