Example usage for javax.servlet ServletOutputStream flush

List of usage examples for javax.servlet ServletOutputStream flush

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:com.predic8.membrane.servlet.embedded.HopsServletHandler.java

@SuppressWarnings("deprecation")
protected void writeResponse(Response res) throws Exception {
    response.setStatus(res.getStatusCode(), res.getStatusMessage());
    for (HeaderField header : res.getHeader().getAllHeaderFields()) {
        if (header.getHeaderName().equals(Header.TRANSFER_ENCODING)) {
            continue;
        }//from   w w  w  .j  a  va 2  s  .  c  om
        response.addHeader(header.getHeaderName().toString(), header.getValue());
    }

    ServletOutputStream out = response.getOutputStream();
    res.getBody().write(new PlainBodyTransferrer(out));
    out.flush();

    response.flushBuffer();

    exchange.setTimeResSent(System.currentTimeMillis());
    exchange.collectStatistics();
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * // w ww  .  j a v a  2  s  . com
 *
 * @param attachFile 
 * @param fileName   ????
 * @throws IOException
 */
public static void write(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response??????com???/*from   w  w w.ja  va 2s.  c  o m*/
 *
 * @param contentType
 * @param attachFile
 * @throws IOException
 */
public static void write(String contentType, byte[] attachFile) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.setContentType(contentType);
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.example.cognitive.personality.DemoServlet.java

/**
 * Create and POST a request to the Personality Insights service
 * /* w ww  . j av a  2s .  c o m*/
 * @param req
 *            the Http Servlet request
 * @param resp
 *            the Http Servlet pesponse
 * @throws ServletException
 *             the servlet exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    logger.info("doPost");

    req.setCharacterEncoding("UTF-8");
    // create the request
    String text = req.getParameter("text");

    try {
        URI profileURI = new URI(baseURL + "/v2/profile").normalize();

        Request profileRequest = Request.Post(profileURI).addHeader("Accept", "application/json")
                .bodyString(text, ContentType.TEXT_PLAIN);

        Executor executor = Executor.newInstance().auth(username, password);
        Response response = executor.execute(profileRequest);
        HttpResponse httpResponse = response.returnResponse();
        resp.setStatus(httpResponse.getStatusLine().getStatusCode());

        ServletOutputStream servletOutputStream = resp.getOutputStream();
        httpResponse.getEntity().writeTo(servletOutputStream);
        servletOutputStream.flush();
        servletOutputStream.close();

    } catch (Exception e) {
        logger.log(Level.SEVERE, "Service error: " + e.getMessage(), e);
        resp.setStatus(HttpStatus.SC_BAD_GATEWAY);
    }
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response?/*from ww  w.  j  a  va2 s . c  o m*/
 * ???10mdownloadCall
 *
 * @param attachFile
 * @throws IOException
 */
public static void download(byte[] attachFile, String fileName) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        resp.addHeader("Content-Disposition",
                "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
        resp.addHeader("Content-Length", "" + attachFile.length);
        //            resp.setContentType("application/octet-stream");
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.denimgroup.threadfix.webapp.controller.DocumentController.java

@RequestMapping(value = "/{docId}/download", method = RequestMethod.POST)
public String downloadDocument(Model model, @PathVariable("orgId") Integer orgId,
        @PathVariable("appId") Integer appId, @PathVariable("docId") Integer docId, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, IOException {

    if (!permissionService.isAuthorized(Permission.READ_ACCESS, orgId, appId)) {
        return "403";
    }/*from  ww w.j  av a2s  . com*/

    Document document = null;
    if (docId != null) {
        document = documentService.loadDocument(docId);
    }

    if (document == null) {
        if (orgId != null && appId != null)
            return "redirect:/organizations/" + orgId + "/applications/" + appId + "/documents";
        else if (orgId != null)
            return "redirect:/organizations/" + orgId;
        else
            return "redirect:/";
    }
    response.setHeader("Content-Disposition",
            "attachment; filename=\"" + document.getName() + "." + document.getType() + "\"");
    response.setContentType(document.getContentType());
    InputStream in = document.getFile().getBinaryStream();
    ServletOutputStream out = response.getOutputStream();
    IOUtils.copy(in, out);
    in.close();
    out.flush();
    out.close();

    return null;
}

From source file:org.efs.openreports.actions.admin.ReportUploadAction.java

@Override
public String execute() {
    try {/*from  w  w w .  ja v a 2s  .c  o m*/
        if ("upload".equals(command)) {
            if (reportFile != null) {
                File destinationFile = new File(directoryProvider.getReportDirectory() + reportFileFileName);

                try {
                    if (destinationFile.exists()) {
                        int revisionCount = reportProvider.getReportTemplate(reportFileFileName)
                                .getRevisionCount();
                        File versionedFile = new File(directoryProvider.getReportDirectory()
                                + reportFileFileName + "." + revisionCount);
                        FileUtils.copyFile(destinationFile, versionedFile);
                    }

                    FileUtils.copyFile(reportFile, destinationFile);
                } catch (IOException ioe) {
                    addActionError(ioe.toString());
                    return SUCCESS;
                }
            } else {
                addActionError("Invalid File.");
            }
        }

        if ("download".equals(command)) {
            String templateFileName = revision;

            // if there is a revision at the end of the file name, strip it off
            if (StringUtils.countMatches(templateFileName, ".") > 1) {
                templateFileName = revision.substring(0, revision.lastIndexOf("."));
            }

            File templateFile = new File(directoryProvider.getReportDirectory() + revision);
            byte[] template = FileUtils.readFileToByteArray(templateFile);

            HttpServletResponse response = ServletActionContext.getResponse();
            response.setHeader("Content-disposition", "inline; filename=" + templateFileName);
            response.setContentType("application/octet-stream");
            response.setContentLength(template.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(template, 0, template.length);
            out.flush();
            out.close();
        }

        if ("revert".equals(command)) {
            String templateFileName = revision.substring(0, revision.lastIndexOf("."));

            File revisionFile = new File(directoryProvider.getReportDirectory() + revision);
            File currentFile = new File(directoryProvider.getReportDirectory() + templateFileName);

            // create a new revision from the current version
            int revisionCount = reportProvider.getReportTemplate(templateFileName).getRevisionCount();
            File versionedFile = new File(
                    directoryProvider.getReportDirectory() + templateFileName + "." + revisionCount);
            FileUtils.copyFile(currentFile, versionedFile);

            // copy the selected revision to the current version
            FileUtils.copyFile(revisionFile, currentFile);
        }

        reportTemplates = reportProvider.getReportTemplates();
    } catch (Exception pe) {
        addActionError(pe.getMessage());
    }

    return SUCCESS;
}

From source file:com.denimgroup.threadfix.webapp.controller.DocumentController.java

@RequestMapping(value = "/{docId}/view", method = RequestMethod.GET)
public String detailDocument(Model model, @PathVariable("orgId") Integer orgId,
        @PathVariable("appId") Integer appId, @PathVariable("docId") Integer docId, HttpServletRequest request,
        HttpServletResponse response) throws SQLException, IOException {

    if (!permissionService.isAuthorized(Permission.READ_ACCESS, orgId, appId)) {
        return "403";
    }/*from  w w  w  . ja va 2  s.c  o m*/

    Document document = null;
    if (docId != null) {
        document = documentService.loadDocument(docId);
    }

    if (document == null) {
        if (orgId != null && appId != null)
            return "redirect:/organizations/" + orgId + "/applications/" + appId + "/documents";
        else if (orgId != null)
            return "redirect:/organizations/" + orgId;
        else
            return "redirect:/";
    }

    String contentType = document.getContentType();
    //      if (contentType == null || contentType.contains("htm") || contentType.contains("js")) {
    //         contentType = "text/plain";
    //      }
    response.setContentType(contentType);
    if (contentType.equals(documentService.getContentTypeService().getDefaultType()) || contentType == null) {
        response.addHeader("Content-Disposition",
                "attachment; filename=\"" + document.getName() + "." + document.getType() + "\"");
        response.setContentType("application/octet-stream");
    }
    response.addHeader("X-Content-Type-Options", "nosniff");
    InputStream in = document.getFile().getBinaryStream();
    ServletOutputStream out = response.getOutputStream();
    IOUtils.copy(in, out);
    in.close();
    out.flush();
    out.close();

    return null;
}

From source file:com.seer.datacruncher.profiler.spring.ProfilerLoadController.java

public ModelAndView getTableNames(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession();

    ObjectMapper mapper = new ObjectMapper();

    ServletOutputStream out;
    response.setContentType("application/text");
    out = response.getOutputStream();/*w  ww . j  a va 2 s.co  m*/

    out.write(mapper.writeValueAsBytes(session.getAttribute("tableNames")));
    out.flush();
    out.close();

    return null;
}