Search in sources :

Example 91 with ServletOutputStream

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

the class AtlasAPIV2ServerEmulator method respondWithJson.

private static void respondWithJson(HttpServletResponse resp, Object entity, int statusCode) throws IOException {
    resp.setContentType("application/json");
    resp.setStatus(statusCode);
    final ServletOutputStream out = resp.getOutputStream();
    new ObjectMapper().writer().writeValue(out, entity);
    out.flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 92 with ServletOutputStream

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

the class TestHttpClient method respondWithJson.

private static void respondWithJson(HttpServletResponse resp, Object entity, int statusCode) throws IOException {
    resp.setContentType("application/json");
    resp.setStatus(statusCode);
    final ServletOutputStream out = resp.getOutputStream();
    new ObjectMapper().writer().writeValue(out, entity);
    out.flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 93 with ServletOutputStream

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

the class TestHttpClient method respondWithText.

private static void respondWithText(HttpServletResponse resp, String result, int statusCode) throws IOException {
    resp.setContentType("text/plain");
    resp.setStatus(statusCode);
    final ServletOutputStream out = resp.getOutputStream();
    out.write(result.getBytes());
    out.flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 94 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jphp by jphp-compiler.

the class PHttpDownloadFileHandler method __invoke.

@Signature
public boolean __invoke(Environment env, PHttpServerRequest request, PHttpServerResponse response) throws Throwable {
    HttpServletResponse res = response.getResponse();
    if (!file.isFile()) {
        response.write(StringMemory.valueOf("File (" + fileName + ") not found."));
        res.setStatus(404);
        return false;
    }
    res.setStatus(200);
    res.setContentType(contentType);
    res.setContentLengthLong(file.length());
    if (fileName.isEmpty()) {
        res.addHeader("Content-Disposition", "attachment");
    } else {
        res.addHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, env.getDefaultCharset().name()) + "\"");
    }
    ServletOutputStream outputStream = res.getOutputStream();
    InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
    byte[] buffer = new byte[128 * 1024];
    while (true) {
        int bytesRead = inputStream.read(buffer);
        if (bytesRead < 0) {
            break;
        }
        outputStream.write(buffer, 0, bytesRead);
    }
    inputStream.close();
    outputStream.close();
    request.end();
    return true;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) FileInputStream(java.io.FileInputStream) Signature(php.runtime.annotation.Reflection.Signature)

Example 95 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project support-core-plugin by jenkinsci.

the class SupportAction method doGenerateAllBundles.

@RequirePOST
public void doGenerateAllBundles(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
    final Jenkins instance = Jenkins.getInstance();
    instance.getAuthorizationStrategy().getACL(instance).checkPermission(CREATE_BUNDLE);
    JSONObject json = req.getSubmittedForm();
    if (!json.has("components")) {
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    logger.fine("Parsing request...");
    Set<String> remove = new HashSet<String>();
    for (Selection s : req.bindJSONToList(Selection.class, json.get("components"))) {
        if (!s.isSelected()) {
            logger.log(Level.FINER, "Excluding ''{0}'' from list of components to include", s.getName());
            remove.add(s.getName());
        }
    }
    logger.fine("Selecting components...");
    final List<Component> components = new ArrayList<Component>(getComponents());
    for (Iterator<Component> iterator = components.iterator(); iterator.hasNext(); ) {
        Component c = iterator.next();
        if (remove.contains(c.getId()) || !c.isEnabled()) {
            iterator.remove();
        }
    }
    final SupportPlugin supportPlugin = SupportPlugin.getInstance();
    if (supportPlugin != null) {
        supportPlugin.setExcludedComponents(remove);
    }
    logger.fine("Preparing response...");
    rsp.setContentType("application/zip");
    rsp.addHeader("Content-Disposition", "inline; filename=" + SupportPlugin.getBundleFileName() + ";");
    final ServletOutputStream servletOutputStream = rsp.getOutputStream();
    try {
        SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication());
        try {
            SecurityContext old = ACL.impersonate(ACL.SYSTEM);
            try {
                SupportPlugin.writeBundle(servletOutputStream, components);
            } catch (IOException e) {
                logger.log(Level.FINE, e.getMessage(), e);
            } finally {
                SecurityContextHolder.setContext(old);
            }
        } finally {
            SupportPlugin.clearRequesterAuthentication();
        }
    } finally {
        logger.fine("Response completed");
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Jenkins(jenkins.model.Jenkins) JSONObject(net.sf.json.JSONObject) SecurityContext(org.acegisecurity.context.SecurityContext) Component(com.cloudbees.jenkins.support.api.Component) HashSet(java.util.HashSet) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

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