/*
 * #%L
 * ACS AEM Commons Bundle
 * %%
 * Copyright (C) 2015 Adobe
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.adobe.acs.commons.httpcache.engine;

import com.adobe.acs.commons.httpcache.engine.impl.TeePrintWriter;
import com.adobe.acs.commons.httpcache.engine.impl.TeeServletOutputStream;
import com.adobe.acs.commons.httpcache.exception.HttpCacheDataStreamException;
import com.adobe.acs.commons.httpcache.store.TempSink;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Wrapper for <code>SlingHttpServletResponse</code>. Wrapped to get hold of the copy of servlet response stream.
 */
public class HttpCacheServletResponseWrapper extends SlingHttpServletResponseWrapper {
    private static final Logger log = LoggerFactory.getLogger(HttpServletResponseWrapper.class);

    private PrintWriter printWriter;
    private ServletOutputStream servletOutputStream;
    private final TempSink tempSink;

    public HttpCacheServletResponseWrapper(SlingHttpServletResponse wrappedResponse, TempSink tempSink) throws
            IOException {
        super(wrappedResponse);
        this.tempSink = tempSink;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (this.printWriter != null) {
            throw new IllegalStateException("Cannot invoke getOutputStream() once getWriter() has been called.");
        } else if (this.servletOutputStream == null) {
            try {
                this.servletOutputStream = new TeeServletOutputStream(super.getOutputStream(), tempSink
                        .createOutputStream());
            } catch (HttpCacheDataStreamException e) {
                log.error("Temp sink is unable to provide an output stream.");
            }
        }

        return this.servletOutputStream;

    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (this.servletOutputStream != null) {
            throw new IllegalStateException("Cannot invoke getWriter() once getOutputStream() has been called.");
        } else if (this.printWriter == null) {
            try {
                this.printWriter = new TeePrintWriter(super.getWriter(), new PrintWriter(tempSink.createOutputStream
                        ()));
            } catch (HttpCacheDataStreamException e) {
                log.error("Temp sink is unable to provide an output stream.");
            }
        }

        return this.printWriter;
    }

    public TempSink getTempSink() {
        return tempSink;
    }
}

@@ -1282,21 +1282,79 @@
 java.io.
-Print
+OutputStreamWriter;%0Aimport java.io.PrintWriter;%0Aimport java.io.
 Writer;%0A
@@ -2959,32 +2959,167 @@
           try %7B%0A
+                final Writer tempWriter = new OutputStreamWriter(tempSink.createOutputStream(), getResponse().getCharacterEncoding());%0A
                 
@@ -3199,58 +3199,14 @@
 temp
-Sink.createOutputStream%0A                        ()
+Writer
 ));%0A

use correct character encoding. related to #641