Example usage for javax.servlet.http HttpServletResponse reset

List of usage examples for javax.servlet.http HttpServletResponse reset

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse reset.

Prototype

public void reset();

Source Link

Document

Clears any data that exists in the buffer as well as the status code, headers.

Usage

From source file:org.apache.cocoon.servlet.RequestUtil.java

private static void manageException(HttpServletRequest req, HttpServletResponse res, Environment env,
        String uri, int errorStatus, String title, String message, String description, Exception e,
        ServletSettings settings, boolean verbose, Object sender) throws IOException {
    if (settings.isManageExceptions()) {
        if (env != null) {
            env.tryResetResponse();/*from   w  ww . j  av  a2  s .co  m*/
        } else {
            res.reset();
        }

        String type = Notifying.FATAL_NOTIFICATION;
        Map extraDescriptions = null;

        if (errorStatus == HttpServletResponse.SC_NOT_FOUND) {
            type = "resource-not-found";
            // Do not show the exception stacktrace for such common errors.
            e = null;
        } else {
            extraDescriptions = new HashMap(2);
            extraDescriptions.put(Notifying.EXTRA_REQUESTURI, req.getRequestURI());
            if (uri != null) {
                extraDescriptions.put("Request URI", uri);
            }

            // Do not show exception stack trace when log level is WARN or above. Show only message.
            if (verbose) {
                Throwable t = DefaultNotifyingBuilder.getRootCause(e);
                if (t != null) {
                    extraDescriptions.put(Notifying.EXTRA_CAUSE, t.getMessage());
                }

                e = null;
            }
        }

        Notifying n = new DefaultNotifyingBuilder().build(sender, e, type, title, "Cocoon Servlet", message,
                description, extraDescriptions);

        res.setContentType("text/html");
        res.setStatus(errorStatus);
        Notifier.notify(n, res.getOutputStream(), "text/html");
    } else {
        res.sendError(errorStatus, title);
        res.flushBuffer();
    }
}

From source file:com.cisco.ca.cstg.pdi.utils.Util.java

/**
 * This method is used to download files from specified path
 * @param response/*  www  .  j av  a2 s  .  c om*/
 * @param archiveFile
 */
public static void downloadArchiveFile(HttpServletResponse response, File archiveFile) {
    if (archiveFile.isFile()) {
        response.reset();
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + archiveFile.getName() + "\"");
        FileInputStream is = null;
        ServletOutputStream op = null;
        try {
            op = response.getOutputStream();
            double dLength = archiveFile.length();
            int iLength = 0;
            int num_read = 0;

            if (dLength >= Integer.MIN_VALUE && dLength <= Integer.MAX_VALUE) {
                iLength = (int) dLength;
            }
            byte[] arBytes = new byte[iLength];
            is = new FileInputStream(archiveFile);

            while (num_read < iLength) {
                int count = is.read(arBytes, num_read, iLength - num_read);
                if (count < 0) {
                    throw new IOException("end of stream reached");
                }
                num_read += count;
            }
            op.write(arBytes);
            op.flush();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
            if (null != op) {
                try {
                    op.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        }
    }
}

From source file:org.zht.framework.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {
    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;/*from  ww w  .jav a 2  s  .  c o  m*/
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {
        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.apache.myfaces.tomahawk.util.ErrorPageWriter.java

public static void handleThrowable(FacesContext facesContext, Throwable ex)
        throws ServletException, IOException {

    prepareExceptionStack(ex);/* w  w w.j a  v a 2s.  c  om*/

    Object response = facesContext.getExternalContext().getResponse();
    if (response instanceof HttpServletResponse) {
        HttpServletResponse httpResp = (HttpServletResponse) response;
        if (!httpResp.isCommitted()) {
            httpResp.reset();
            httpResp.setContentType("text/html; charset=UTF-8");
            Writer writer = httpResp.getWriter();

            debugHtml(writer, facesContext, ex);

            log.error("An exception occurred", ex);
        } else {
            throwException(ex);
        }
    } else {
        throwException(ex);
    }
}

From source file:org.apache.myfaces.tomahawk.util.ErrorPageWriter.java

public static void handleExceptionList(FacesContext facesContext, List exceptionList)
        throws ServletException, IOException {
    for (int i = 0; i < exceptionList.size(); i++) {
        prepareExceptionStack((Exception) exceptionList.get(i));
    }/*  ww  w  . jav  a 2  s. c  o m*/

    Object response = facesContext.getExternalContext().getResponse();
    if (response instanceof HttpServletResponse) {
        HttpServletResponse httpResp = (HttpServletResponse) response;
        if (!httpResp.isCommitted()) {
            httpResp.reset();
            httpResp.setContentType("text/html; charset=UTF-8");
            Writer writer = httpResp.getWriter();

            debugHtml(writer, facesContext, exceptionList);

            for (int i = 0; i < exceptionList.size(); i++) {
                log.error("An exception occurred", (Exception) exceptionList.get(i));
            }
        } else {
            throwException((Exception) exceptionList.get(0));
        }
    } else {
        throwException((Exception) exceptionList.get(0));
    }
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {

    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;//from  ww  w.j  a  v a2s.co m
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.eryansky.modules.disk.utils.DiskUtils.java

/**
 * // w w w.j  a  v  a2s . c om
 * @param request
 * @param response
 * @param inputStream ?
 * @param displayName ??
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream,
        String displayName) throws IOException {
    response.reset();
    WebUtils.setNoCacheHeader(response);
    String contentType = "application/x-download";
    if (StringUtils.isNotBlank(displayName)) {
        if (displayName.endsWith(".doc")) {
            contentType = "application/msword";
        } else if (displayName.endsWith(".docx")) {
            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if (displayName.endsWith(".xls")) {
            contentType = "application/vnd.ms-excel";
        } else if (displayName.endsWith(".xlsx")) {
            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        } else if (displayName.endsWith(".ppt")) {
            contentType = "application/vnd.ms-powerpoint";
        } else if (displayName.endsWith(".pptx")) {
            contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        } else if (displayName.endsWith(".pdf")) {
            contentType = "application/pdf";
        } else if (displayName.endsWith(".jpg") || displayName.endsWith(".jpeg")) {
            contentType = "image/jpeg";
        } else if (displayName.endsWith(".gif")) {
            contentType = "image/gif";
        } else if (displayName.endsWith(".bmp")) {
            contentType = "image/bmp";
        }
    }

    response.setContentType(contentType);
    response.setContentLength((int) inputStream.available());

    //        String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    //        displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayName);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(inputStream);
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.beanfuse.utils.web.DownloadHelper.java

public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inStream,
        String name, String display) {
    String attch_name = "";
    byte[] b = new byte[1024];
    int len = 0;// w w w  .ja va  2  s.  c  om
    try {
        String ext = StringUtils.substringAfterLast(name, ".");
        if (StringUtils.isBlank(display)) {
            attch_name = getAttachName(name);
        } else {
            attch_name = display;
            if (!attch_name.endsWith("." + ext)) {
                attch_name += "." + ext;
            }
        }
        response.reset();
        String contentType = response.getContentType();
        if (null == contentType) {
            if (StringUtils.isEmpty(ext)) {
                contentType = "application/x-msdownload";
            } else {
                contentType = contentTypes.getProperty(ext, "application/x-msdownload");
            }
            response.setContentType(contentType);
            logger.debug("set content type {} for {}", contentType, attch_name);
        }
        response.addHeader("Content-Disposition",
                "attachment; filename=\"" + encodeAttachName(request, attch_name) + "\"");
        while ((len = inStream.read(b)) > 0) {
            response.getOutputStream().write(b, 0, len);
        }
        inStream.close();
    } catch (Exception e) {
        logger.warn("download file error=" + attch_name, e);
    }
}

From source file:org.zht.framework.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (ZStrUtil.isEmpty(displayName)) {
        displayName = file.getName();//  w ww .  j  a v  a 2  s. c  o  m
    }
    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {
        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from w  w  w.j a  v a2s. c om
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}