String charset = servletResponse.getCharacterEncoding(); if (charset == null) { charset = DEFAULT_CHARSET; }  if (LOG.isDebugEnabled()) { LOG.debug("Use charset '" + charset + "' for writer of image '" + uri + "'."); }  Writer writer = null; try { writer = new OutputStreamWriter(output, charset);  } catch (UnsupportedEncodingException e) { LOG.error("Unsupported encoding '" + charset + "'.", e); }
        private void getContent() {            if (bufferInitialized) {                return;            }            bufferInitialized = true;            File fileTemp;            try {                fileTemp = File.createTempFile(TEMP_FILE_PREFIX,                        TEMP_FILE_SUFFIX);            } catch (IOException ex) {                LOG.error("Failed to call request " + uri + ".", ex);                errored = true;                return;            }            try {                OutputStream output = null;                if (fileTemp != null) {                    if (LOG.isDebugEnabled()) {                        LOG.debug("Use temp file='"                                + fileTemp.getAbsolutePath()                                + "' to store response of image request '"                                + uri + "'.");                    }                    try {                        output = new FileOutputStream(fileTemp);                    } catch (FileNotFoundException ex) {                        LOG.error(                                "Can not write into file '" + fileTemp + "'.",                                ex);                    }                }                if (output == null) {                    output = new ByteArrayOutputStream(BUFFER_DEFAULT_SIZE);                    if (LOG.isDebugEnabled()) {                        LOG.debug("Use memory byte array (size="                                + BUFFER_DEFAULT_SIZE                                + ") to store response of image request '"                                + uri + "'.");                    }                }                String charset = servletResponse.getCharacterEncoding();                if (charset == null) {                    charset = DEFAULT_CHARSET;                }                if (LOG.isDebugEnabled()) {                    LOG.debug("Use charset '" + charset                            + "' for writer of image '" + uri + "'.");                }                Writer writer = null;                try {                    writer = new OutputStreamWriter(output, charset);                } catch (UnsupportedEncodingException e) {                    LOG.error("Unsupported encoding '" + charset + "'.", e);                }                HttpServletRequestWrapper request = new HttpServletRequestWrapper(                        servletRequest);                IncludeHttpServletResponse response = new IncludeHttpServletResponse(                        servletResponse, output, writer);                String requestURI = "/" + uri;                if (LOG.isDebugEnabled()) {                    LOG.debug("Send request to get image content of '"                            + requestURI + "'.");                }                RequestDispatcher requestDispatcher = servletContext                        .getRequestDispatcher(requestURI);                if (requestDispatcher == null) {                    LOG.error("Can not get request dispatcher for url '"                            + requestURI + "'.");                    errored = true;                    return;                }                try {                    requestDispatcher.include(request, response);                } catch (Exception ex) {                    LOG.error("Failed to call request " + uri + ".", ex);                    errored = true;                    return;                }                if (writer != null) {                    try {                        writer.close();                    } catch (IOException ex) {                        LOG.error("Can not close writer !", ex);                    }                }                try {                    output.close();                } catch (IOException ex) {                    LOG.error("Can not close buffer !", ex);                }                if (response.getStatus() != HttpServletResponse.SC_OK) {                    LOG.error("Invalid response status: "                            + response.getStatus());                    errored = true;                    return;                }                this.lastModified = response.getLastModified();                this.contentLength = response.getContentLength();                this.contentType = response.getContentType();                if (LOG.isDebugEnabled()) {                    LOG                            .debug("Resource '"                                    + uri                                    + "' contentType='"                                    + contentType                                    + "' contentLength="                                    + contentLength                                    + " lastModified="                                    + ((lastModified > 0) ? new Date(                                            lastModified).toString() : String                                            .valueOf(lastModified)));                }                if (fileTemp != null) {                    bufferFile = fileTemp;                    bufferFile.deleteOnExit();                } else {                    bufferArray = ((ByteArrayOutputStream) output)                            .toByteArray();                }            } finally {                if (errored && fileTemp != null) {                    fileTemp.delete();                }            }        }