String servletPath = request.getServletPath();
String servletPath = (String) request .getAttribute("javax.servlet.include.servlet_path"); if (servletPath == null) { servletPath = request.getServletPath(); }
    protected void doGet(HttpServletRequest request,            HttpServletResponse response) throws ServletException, IOException {        String url = request.getRequestURI();        String contextPath = request.getContextPath();        if (contextPath != null) {            url = url.substring(contextPath.length());        }        String servletPath = request.getServletPath();        if (servletPath != null) {            url = url.substring(servletPath.length());        }        // Retire le nom de notre servlet        int idx = url.indexOf('/');        if (idx >= 0) {            url = url.substring(idx + 1);        }        // Recherche la commande        idx = url.indexOf('/');        if (idx < 0) {            throw new ServletException("Bad uri. (can not find command into '"                    + request.getRequestURI() + "')");        }        String cmd = url.substring(0, idx);        String imageURL = url.substring(idx + 1);        idx = imageURL.indexOf('?');        if (idx >= 0) {            imageURL = imageURL.substring(0, idx);        }        idx = imageURL.indexOf('#');        if (idx >= 0) {            imageURL = imageURL.substring(0, idx);        }        if (LOG.isTraceEnabled()) {            LOG.trace("Image request: requestURI='" + request.getRequestURI()                    + "' contextPath='" + contextPath + "' servletPath='"                    + servletPath + "' imageUrl='" + imageURL + "'.");        }        if (imageURL.length() < 1) {            throw new ServletException("Bad uri. (no image resource into '"                    + request.getRequestURI() + "')");        }        cmd = URLDecoder.decode(cmd, URL_DECODER_CHARSET);        imageURL = URLFormCodec.decodeURL(imageURL);        if (LOG.isDebugEnabled()) {            LOG.debug("Image request: cmd=" + cmd + " imageURL=" + imageURL);        }        IBufferedImage bufferedImage;        synchronized (imagesCache) {            bufferedImage = imagesCache.searchInCache(cmd, imageURL);            if (bufferedImage == null) {                bufferedImage = createNewBufferedImage(url);                imagesCache.storeIntoCache(cmd, imageURL, bufferedImage);            }        }        if (LOG.isTraceEnabled()) {            LOG.trace("Process bufferedImage: " + bufferedImage);        }        synchronized (bufferedImage) {            if (bufferedImage.isInitialized() == false) {                LOG.debug("Buffered image '" + bufferedImage.getName()                        + "' is not initialized !");                try {                    initializeBufferedImage(bufferedImage, cmd, imageURL,                            request, response);                } catch (ServletException ex) {                    bufferedImage.setErrored();                    throw ex;                }            }        }        if (bufferedImage.isErrored()) {            if (LOG.isDebugEnabled()) {                LOG.debug("Buffered image is errored !");            }            // XXX Peut etre a revoir pour mettre une date d'expiration            // autre            // ....            response.setStatus(HttpServletResponse.SC_NOT_FOUND);            if (LOG.isDebugEnabled()) {                LOG.debug("Set no cache into response.");            }            ParametredHttpServlet.setNoCache(response);            return;        }        String redirection = bufferedImage.getRedirection();        if (redirection != null) {            if (contextPath.endsWith("/") == false) {                redirection = "/" + redirection;            }            if (LOG.isDebugEnabled()) {                LOG.debug("Send temporary redirection to '" + redirection                        + "'.");            }            response.sendRedirect(contextPath + redirection);            response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);            return;        }        boolean different = noCache;        String etag = bufferedImage.getETag();        if (different == false && etag != null) {            String ifETag = request.getHeader(HTTP_IF_NONE_MATCH);            if (ifETag != null) {                if (etag.equals(ifETag)) {                    if (LOG.isDebugEnabled()) {                        LOG                                .debug("Client sent the same ETag, send a NOT MODIFIED response.");                    }                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);                    return;                }                different = true;            }        }        String hash = bufferedImage.getHash();        if (different == false && hash != null) {            String isHash = request.getHeader(HTTP_IF_NOT_HASH);            if (isHash != null) {                if (hash.equals(isHash)) {                    if (LOG.isDebugEnabled()) {                        LOG                                .debug("Client sent the same HashCode, send a NOT MODIFIED response.");                    }                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);                    return;                }                different = true;            }        }        long modificationDate = bufferedImage.getModificationDate();        if (different == false) {            long ifModifiedSince = request                    .getDateHeader(HTTP_IF_MODIFIED_SINCE);            if (ifModifiedSince > 0) {                if (ifModifiedSince >= modificationDate) {                    if (LOG.isDebugEnabled()) {                        LOG                                .debug("Client sent a valid date for last modification, send a NOT MODIFIED response.");                    }                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);                    return;                }            }        }        String contentType = bufferedImage.getContentType();        response.setContentType(contentType);        if (noCache) {            setNoCache(response);        } else {            if (modificationDate > 0) {                response.setDateHeader(HTTP_LAST_MODIFIED, modificationDate);            }            ExpirationDate expirationDate = null;            if (expirationDate == null) {                expirationDate = getDefaultExpirationDate();            }            expirationDate.sendExpires(response);        }        if (etag != null) {            response.setHeader(HTTP_ETAG, etag);        }        if (hash != null) {            response.setHeader(HTTP_HASH, hash);        }        response.setContentLength(bufferedImage.getSize());        if (request.getMethod().equals(HEAD_HTTP_METHOD)) {            if (LOG.isDebugEnabled()) {                LOG.debug("Head method requested, dont send the data.");            }            return;        }        OutputStream outputStream = response.getOutputStream();        int bufferSize = response.getBufferSize();        if (bufferSize < 1 || bufferSize > MAX_BUFFER_SIZE) {            bufferSize = MAX_BUFFER_SIZE;        }        byte buf[] = new byte[bufferSize];        InputStream inputStream = bufferedImage.getContent();        try {            for (;;) {                int ret = inputStream.read(buf);                if (ret < 1) {                    break;                }                outputStream.write(buf, 0, ret);            }        } finally {            try {                inputStream.close();            } catch (IOException ex) {                LOG.error(ex);            }        }    }