Search in sources :

Example 76 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project ratpack by ratpack.

the class PatchedSampleServer method directResponse.

private String directResponse(HttpServletResponse httpResp, String response) throws IOException {
    ServletOutputStream os = httpResp.getOutputStream();
    os.write(response.getBytes());
    os.close();
    return null;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 77 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project ranger by apache.

the class ServiceDBStore method writeJson.

private void writeJson(List<RangerPolicy> policies, String jsonFileName, HttpServletResponse response) throws JSONException, IOException {
    response.setContentType("text/json");
    response.setHeader("Content-Disposition", "attachment; filename=" + jsonFileName);
    ServletOutputStream out = null;
    RangerExportPolicyList rangerExportPolicyList = new RangerExportPolicyList();
    putMetaDataInfo(rangerExportPolicyList);
    rangerExportPolicyList.setPolicies(policies);
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(rangerExportPolicyList, RangerExportPolicyList.class);
    try {
        out = response.getOutputStream();
        response.setStatus(HttpServletResponse.SC_OK);
        IOUtils.write(json, out, "UTF-8");
    } catch (Exception e) {
        LOG.error("Error while exporting json file " + jsonFileName, e);
    } finally {
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (Exception ex) {
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) GsonBuilder(com.google.gson.GsonBuilder) RangerExportPolicyList(org.apache.ranger.view.RangerExportPolicyList) Gson(com.google.gson.Gson) VXString(org.apache.ranger.view.VXString) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JSONException(org.codehaus.jettison.json.JSONException)

Example 78 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project hutool by looly.

the class ServletUtil method write.

/**
 * 返回数据给客户端
 *
 * @param response 响应对象{@link HttpServletResponse}
 * @param in 需要返回客户端的内容
 * @param bufferSize 缓存大小
 */
public static void write(HttpServletResponse response, InputStream in, int bufferSize) {
    ServletOutputStream out = null;
    try {
        out = response.getOutputStream();
        IoUtil.copy(in, out, bufferSize);
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(out);
        IoUtil.close(in);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException)

Example 79 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project incubator-dubbo-ops by apache.

the class Restful method execute.

public void execute(Map<String, Object> context) throws Exception {
    Result result = new Result();
    if (request.getParameter("url") != null) {
        url = URL.valueOf(URL.decode(request.getParameter("url")));
    }
    if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
        User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
        currentUser = user;
        operator = user.getUsername();
        role = user.getRole();
        context.put(WebConstants.CURRENT_USER_KEY, user);
    }
    operatorAddress = (String) context.get("clientid");
    if (operatorAddress == null || operatorAddress.isEmpty()) {
        operatorAddress = (String) context.get("request.remoteHost");
    }
    context.put("operator", operator);
    context.put("operatorAddress", operatorAddress);
    String jsonResult = null;
    try {
        result = doExecute(context);
        result.setStatus("OK");
    } catch (IllegalArgumentException t) {
        result.setStatus("ERROR");
        result.setCode(3);
        result.setMessage(t.getMessage());
    }// }
     catch (Throwable t) {
        result.setStatus("ERROR");
        result.setCode(1);
        result.setMessage(t.getMessage());
    }
    response.setContentType("application/javascript");
    ServletOutputStream os = response.getOutputStream();
    try {
        jsonResult = JSON.toJSONString(result);
        os.print(jsonResult);
    } catch (Exception e) {
        response.setStatus(500);
        os.print(e.getMessage());
    } finally {
        os.flush();
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 80 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project summer-bean by cn-cerc.

the class ExportPdf method export.

public void export() throws IOException, DocumentException {
    Template template = this.getTemplate();
    // 清空输出流
    response.reset();
    if ("file".equals(template.getOutputDevice())) {
        // 设置相应内容的编码格式
        response.setCharacterEncoding("UTF-8");
        String fname = new String(template.getFileName().getBytes(), "ISO-8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + fname + ".pdf");
        // 定义输出类型
        response.setContentType("application/pdf");
    }
    // 第一步
    Document doc = template.getDocument();
    // 第二步
    ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(doc, pdfStream);
    // 第三步
    if (template.getHeaderFooter() != null) {
        writer.setPageEvent(template.getHeaderFooter());
    } else if (template.getHeader() != null) {
        ReportHeaderFooter headerFooter = new ReportHeaderFooter();
        headerFooter.setHeader(template.getHeader());
        writer.setBoxSize("art", doc.getPageSize());
        writer.setPageEvent(headerFooter);
    }
    doc.open();
    // 第四步
    doc.addAuthor("地藤系统");
    doc.addSubject("地藤系统报表文件");
    doc.addCreationDate();
    template.output(doc, writer);
    // 设置是否自动显示打印对话框
    if ("printer".equals(template.getOutputDevice())) {
        // writer.addJavaScript("this.print(false);", false);
        writer.addJavaScript("this.print({bUI: true, bSilent: true,bShrinkToFit:true});", false);
    // document.add(new Chunk("Silent Auto Print"));
    }
    // 第五步
    doc.close();
    // 第六步
    response.setContentType("application/pdf");
    response.setContentLength(pdfStream.size());
    ServletOutputStream out = response.getOutputStream();
    pdfStream.writeTo(out);
    out.flush();
    response.flushBuffer();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.itextpdf.text.Document)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)509 IOException (java.io.IOException)212 HttpServletResponse (javax.servlet.http.HttpServletResponse)147 Test (org.junit.Test)112 HttpServletRequest (javax.servlet.http.HttpServletRequest)109 ServletException (javax.servlet.ServletException)91 InputStream (java.io.InputStream)62 File (java.io.File)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 FileInputStream (java.io.FileInputStream)40 CountDownLatch (java.util.concurrent.CountDownLatch)27 WriteListener (javax.servlet.WriteListener)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 PrintWriter (java.io.PrintWriter)26 HttpServlet (javax.servlet.http.HttpServlet)25 AsyncContext (javax.servlet.AsyncContext)23 ServletInputStream (javax.servlet.ServletInputStream)22 ArrayList (java.util.ArrayList)21 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)20 Date (java.util.Date)18