@Override
    public PrintWriter getWriter() throws IOException {
        if (writer != null) {
            return writer;
        }
        if (cache != null) {
            throw new IllegalStateException("getOutputStream() has already been called!");
        }

        cache = new CacheResponseStream(response.getOutputStream());
        String encoding = null;
        if (response.getCharacterEncoding() != null && Charset.isSupported(response.getCharacterEncoding())) {
            encoding = response.getCharacterEncoding();
        } else {
            encoding = ISO_8859_1;
        }
        writer = new PrintWriter(new OutputStreamWriter(cache, encoding));
        return writer;
    }