Search in sources :

Example 86 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project leopard by tanhaichao.

the class FlushServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String category = request.getParameter("category");
    System.out.println("size:" + response.getBufferSize());
    System.out.println("response:" + response.getClass());
    // response.setBufferSize(10);
    System.out.println("size:" + response.getBufferSize());
    // response.setHeader("Transfer-Encoding", "chunked");
    response.setContentType("text/html; charset=UTF-8");
    // response.setContentLength(10);
    ServletOutputStream out = response.getOutputStream();
    out.println("<!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->");
    out.println("<!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->");
    out.println("<!--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->");
    out.println("start");
    for (int i = 0; i < 10; i++) {
        String line = i + ":linelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelinelineline time:" + new Date().toString() + "\n";
        out.println(line + "");
        out.flush();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    // out.flush();
    out.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) Date(java.util.Date)

Example 87 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project leopard by tanhaichao.

the class FileCacheView method createView.

protected JstlView createView(String viewName) {
    return new JstlView("/WEB-INF/jsp/" + viewName + ".jsp") {

        @Override
        protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
            ServletOutputStream output = response.getOutputStream();
            ServletContext servletContext = request.getServletContext();
            super.setServletContext(servletContext);
            CacheResponseWrapper cacheResponseWrapper = new CacheResponseWrapper(response);
            // System.err.println("renderMergedOutputModel");
            super.renderMergedOutputModel(model, request, cacheResponseWrapper);
            byte[] data = cacheResponseWrapper.getResponseData();
            String content = new String(data);
            response(content);
            output.write(data);
            output.flush();
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) JstlView(org.springframework.web.servlet.view.JstlView) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) Map(java.util.Map)

Example 88 with ServletOutputStream

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

the class JSAPIWriter method writeJavaScript.

public void writeJavaScript(String base, HttpServletRequest req, HttpServletResponse resp, Map<String, ServiceRegistry> serviceRegistries) throws IOException {
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Starting JS API client");
    }
    // RESTEASY-776
    // before writing generated javascript, we generate Etag and compare it with client request.
    // If nothing changed, we send back 304 Not Modified for client browser to use cached js.
    String ifNoneMatch = req.getHeader("If-None-Match");
    String etag = generateEtag(serviceRegistries);
    resp.setHeader("Etag", etag);
    if (ifNoneMatch != null && ifNoneMatch.equals(etag)) {
        resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) {
        String uri = base;
        if (entry.getKey() != null)
            uri += entry.getKey();
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(new BufferedWriter(stringWriter));
        writeJavaScript(uri, writer, entry.getValue());
        writer.flush();
        writer.close();
        if (clientIsGzipSupported(req)) {
            ByteArrayOutputStream compressedContent = new ByteArrayOutputStream();
            GZIPOutputStream gzipstream = new GZIPOutputStream(compressedContent);
            gzipstream.write(stringWriter.toString().getBytes(Charset.forName("utf-8")));
            gzipstream.finish();
            // get the compressed content
            byte[] compressedBytes = compressedContent.toByteArray();
            // set appropriate HTTP headers
            resp.setContentLength(compressedBytes.length);
            resp.addHeader("Content-Encoding", "gzip");
            ServletOutputStream output = resp.getOutputStream();
            output.write(compressedBytes);
            output.flush();
            output.close();
        } else {
            ServletOutputStream output = resp.getOutputStream();
            byte[] bytes = stringWriter.toString().getBytes(Charset.forName("utf-8"));
            resp.setContentLength(bytes.length);
            output.write(bytes);
            output.flush();
            output.close();
        }
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Map(java.util.Map)

Example 89 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project pentaho-kettle by pentaho.

the class AllocateServerSocketServletTest method testAllocateServerSocketServletEncodesParametersForHmtlResponse.

@Test
@PrepareForTest({ Encode.class })
public void testAllocateServerSocketServletEncodesParametersForHmtlResponse() throws ServletException, IOException {
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    SocketPortAllocation mockSocketPortAllocation = mock(SocketPortAllocation.class);
    PowerMockito.spy(Encode.class);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ServletOutputStream servletOutputStream = new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            byteArrayOutputStream.write(b);
        }
    };
    when(mockRequest.getContextPath()).thenReturn(AllocateServerSocketServlet.CONTEXT_PATH);
    when(mockRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    when(mockResponse.getOutputStream()).thenReturn(servletOutputStream);
    when(mockTransformationMap.allocateServerSocketPort(anyInt(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(mockSocketPortAllocation);
    allocateServerSocketServlet.doGet(mockRequest, mockResponse);
    String response = byteArrayOutputStream.toString();
    // Pull out dynamic part of body, remove hardcoded html
    String dynamicBody = ServletTestUtils.getInsideOfTag("BODY", response).replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("<H1>.+</H1>", "").replaceAll("--> port", "");
    assertFalse(ServletTestUtils.hasBadText(dynamicBody));
    PowerMockito.verifyStatic(atLeastOnce());
    Encode.forHtml(anyString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 90 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project pentaho-kettle by pentaho.

the class AllocateServerSocketServletIT method testAllocateServerSocketServletEncodesParametersForHmtlResponse.

@Test
public void testAllocateServerSocketServletEncodesParametersForHmtlResponse() throws ServletException, IOException {
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    SocketPortAllocation mockSocketPortAllocation = mock(SocketPortAllocation.class);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ServletOutputStream servletOutputStream = new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            byteArrayOutputStream.write(b);
        }
    };
    when(mockRequest.getContextPath()).thenReturn(AllocateServerSocketServlet.CONTEXT_PATH);
    when(mockRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING);
    when(mockResponse.getOutputStream()).thenReturn(servletOutputStream);
    when(mockTransformationMap.allocateServerSocketPort(anyInt(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(mockSocketPortAllocation);
    allocateServerSocketServlet.doGet(mockRequest, mockResponse);
    String response = byteArrayOutputStream.toString();
    // Pull out dynamic part of body, remove hardcoded html
    String dynamicBody = ServletTestUtils.getInsideOfTag("BODY", response).replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("<H1>.+</H1>", "").replaceAll("--> port", "");
    assertFalse(ServletTestUtils.hasBadText(dynamicBody));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

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