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:de.itsvs.cwtrpc.security.RpcRedirectStrategy.java

public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
        throws IOException {
    final String responseText;

    responseText = createResponseText(request, response, url);

    response.reset();
    addNoCacheResponseHeaders(request, response);
    if (isError()) {
        if (log.isDebugEnabled()) {
            log.debug("Instead of redirecting to url '" + url + "' error " + getStatusCode()
                    + " will be sent to client");
        }/* ww w.  j  a  va 2 s.co  m*/
        if (responseText.length() > 0) {
            response.sendError(getStatusCode(), responseText);
        } else {
            response.sendError(getStatusCode());
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Instead of redirecting to url '" + url + "' status code will be set to "
                    + getStatusCode());
        }
        response.setContentType(CONTENT_TYPE);
        response.setCharacterEncoding(CHARACTER_ENCODING);
        response.setStatus(getStatusCode());
        writeResponseText(request, response, CHARACTER_ENCODING, url);
        response.flushBuffer();
    }
}

From source file:com.palantir.stash.stashbot.servlet.BuildSuccessReportingServlet.java

private void printOutput(HttpServletRequest req, HttpServletResponse res) throws IOException {
    res.reset();
    res.setStatus(200);/*from w  w  w . j  a v  a2s .c  o  m*/
    res.setContentType("text/plain;charset=UTF-8");
    Writer w = res.getWriter();
    w.append("Status Updated");
    w.close();
}

From source file:com.migo.controller.SysGeneratorController.java

/**
 * ??// w w w . j  av  a  2 s .  c o m
 */
@SysLog("??")
@RequestMapping("/code")
@RequiresPermissions("sys:generator:code")
public void code(String tables, HttpServletResponse response) throws IOException {
    String[] tableNames = new String[] {};
    tableNames = JSON.parseArray(tables).toArray(tableNames);

    byte[] data = sysGeneratorService.generatorCode(tableNames);

    response.reset();
    response.setHeader("Content-Disposition", "attachment; filename=\"migo.zip\"");
    response.addHeader("Content-Length", "" + data.length);
    response.setContentType("application/octet-stream; charset=UTF-8");

    IOUtils.write(data, response.getOutputStream());
}

From source file:org.fenixedu.academic.ui.struts.action.student.candidacy.FirstTimeEnrolmentDocuments.java

public ActionForward showSummaryFile(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) {
    CandidacySummaryFile file = ((StudentCandidacy) getDomainObject(request, "candidacyID")).getSummaryFile();

    response.reset();
    try {/*from  w w  w  .j a  v  a2  s . c  om*/
        response.getOutputStream().write(file.getContent());
        response.setContentLength(file.getContent().length);
        response.setContentType("application/pdf");
        response.flushBuffer();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }

    return null;
}

From source file:org.beangle.web.io.SplitStreamDownloader.java

@Override
public void download(HttpServletRequest request, HttpServletResponse response, InputStream input, String name,
        String display) {//from  ww  w  .  j  a  v a  2 s.  c om
    String attach = getAttachName(name, display);
    response.reset();
    addContent(request, response, attach);
    response.setHeader("Accept-Ranges", "bytes");
    response.setHeader("connection", "Keep-Alive");
    int length = 0;
    long start = 0L;
    long begin = 0L;
    long stop = 0L;
    StopWatch watch = new StopWatch();
    watch.start();
    try {
        length = input.available();
        stop = length - 1;
        response.setContentLength(length);
        String rangestr = request.getHeader("Range");
        if (null != rangestr) {
            String[] readlength = StringUtils.substringAfter(rangestr, "bytes=").split("-");
            start = Long.parseLong(readlength[0]);
            if (readlength.length > 1 && StringUtils.isNotEmpty(readlength[1])) {
                stop = Long.parseLong(readlength[1]);
            }
            if (start != 0) {
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
                String crange = "bytes " + start + "-" + stop + "/" + length;
                response.setHeader("Content-Range", crange);
            }
        }
        OutputStream output = response.getOutputStream();
        input.skip(start);
        begin = start;
        int size = 4 * 1024;
        byte[] buffer = new byte[size];
        for (int step = maxStep(start, stop, size); step > 0; step = maxStep(start, stop, size)) {
            int readed = input.read(buffer, 0, step);
            if (readed == -1)
                break;
            output.write(buffer, 0, readed);
            start += readed;
        }
    } catch (IOException e) {
    } catch (Exception e) {
        logger.warn("download file error " + attach, e);
    } finally {
        IOUtils.closeQuietly(input);
        if (logger.isDebugEnabled()) {
            String percent = null;
            if (length == 0) {
                percent = "100%";
            } else {
                percent = ((int) (((start - begin) * 1.0 / length) * 10000)) / 100.0f + "%";
            }
            long time = watch.getTime();
            int rate = 0;
            if (start - begin > 0) {
                rate = (int) (((start - begin) * 1.0 / time * 1000) / 1024);
            }
            logger.debug("{}({}-{}/{}) download {}[{}] in {} ms with {} KB/s",
                    array(attach, begin, stop, length, start - begin, percent, time, rate));
        }
    }
}

From source file:com.sshdemo.common.report.manage.web.ParameterSetAction.java

private void buildChart() {
    PrintWriter pw = null;// w w  w  .jav a  2s.c o m
    InputStream in = null;
    try {
        ChartReport chart = reportFac.findChartReportById(reportId);

        HttpServletResponse response = ServletActionContext.getResponse();
        pw = response.getWriter();

        response.reset();// 
        response.setContentLength(0);
        byte[] bytes = chartFactory.export(chart, paraMap);
        response.setContentLength(bytes.length);
        response.setHeader("Content-Type", "image/png");
        in = new ByteArrayInputStream(bytes);
        int len = 0;
        while ((len = in.read()) > -1) {
            pw.write(len);
        }
        pw.flush();
    } catch (Exception e) {
        // log.error(e.toString());
    } finally {
        if (pw != null) {
            try {
                pw.close();
                pw = null;
            } catch (Exception e) {
            }
        }
        if (in != null) {
            try {
                in.close();
                in = null;
            } catch (Exception e) {
            }
        }
    }
}

From source file:org.apache.kylin.rest.controller.BasicController.java

protected void setDownloadResponse(String downloadFile, final HttpServletResponse response) {
    File file = new File(downloadFile);
    try (InputStream fileInputStream = new FileInputStream(file);
            OutputStream output = response.getOutputStream();) {
        response.reset();
        response.setContentType("application/octet-stream");
        response.setContentLength((int) (file.length()));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        IOUtils.copyLarge(fileInputStream, output);
        output.flush();/* w  ww . j  ava  2  s  .  co  m*/
    } catch (IOException e) {
        throw new InternalErrorException("Failed to download file: " + e.getMessage(), e);
    }
}

From source file:org.apache.kylin.rest.controller.DiagnosisController.java

private void setDownloadResponse(String downloadFile, final HttpServletResponse response) {
    File file = new File(downloadFile);
    try (InputStream fileInputStream = new FileInputStream(file);
            OutputStream output = response.getOutputStream();) {
        response.reset();
        response.setContentType("application/octet-stream");
        response.setContentLength((int) (file.length()));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        IOUtils.copyLarge(fileInputStream, output);
        output.flush();//  www .j  a  v a2s.  c om
    } catch (IOException e) {
        throw new InternalErrorException("Failed to create download for diagnosis. " + e.getMessage(), e);
    }
}

From source file:com.fh.controller.upload.FileUploadController.java

/**
 * //from   www .j a v a  2  s  .  com
 * 
 * @param response
 * @param request
 * @param realName
 * @param name
 */
@RequestMapping("/download/{realName}/{name}")
public void download(HttpServletResponse response, HttpServletRequest request, @PathVariable String realName,
        @PathVariable String name) {
    OutputStream os = null;
    response.reset();
    realName = request.getSession().getServletContext().getRealPath("/") + "upload" + File.separator + realName;
    response.setHeader("Content-Disposition", "attachment; filename=" + name);
    response.setContentType("application/octet-stream; charset=utf-8");
    try {
        os = response.getOutputStream();
        os.write(FileUtils.readFileToByteArray(new File(realName)));
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:de.itsvs.cwtrpc.security.AbstractRpcFailureHandler.java

protected void writeUnexpectedException(HttpServletRequest request, HttpServletResponse response,
        Throwable exception) throws IOException {
    if (CwtRpcUtils.getRpcSessionInvalidationPolicy(request).isInvalidateOnUnexpectedException()) {
        invalidateSession(request);// w w w  .  jav  a 2  s  . co  m
    }

    response.reset();
    addNoCacheResponseHeaders(request, response);
    RPCServletUtils.writeResponseForUnexpectedFailure(getServletContext(), response, exception);
    response.flushBuffer();
}