Search in sources :

Example 1 with ServletOutputStream

use of jakarta.servlet.ServletOutputStream in project atmosphere by Atmosphere.

the class SSEAtmosphereInterceptorTest method testDataWriter.

@Test
public void testDataWriter() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ServletResponse resp = Mockito.mock(HttpServletResponse.class);
    Mockito.when(resp.getOutputStream()).thenReturn(new ServletOutputStream() {

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setWriteListener(WriteListener writeListener) {
        }

        @Override
        public void write(int b) throws IOException {
            baos.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            baos.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            baos.write(b, off, len);
        }
    });
    AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
    request.header(HeaderConfig.X_ATMOSPHERE_TRANSPORT, "SSE");
    AtmosphereResponse response = AtmosphereResponseImpl.newInstance(request);
    response.request(request);
    response.setResponse(resp);
    AtmosphereResourceImpl resource = new AtmosphereResourceImpl();
    resource.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, Mockito.mock(AsyncSupport.class), null);
    resource.suspend();
    SSEAtmosphereInterceptor interceptor = new SSEAtmosphereInterceptor();
    interceptor.configure(config);
    interceptor.inspect(resource);
    // no newline
    response.write("Good Morning".getBytes());
    assertEquals(baos.toString(), "data:Good Morning\r\n\r\n");
    baos.reset();
    // \n
    response.write("Hello World!\nHave a nice day!".getBytes());
    assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
    baos.reset();
    // \r
    response.write("Hello World!\rHave a nice day!".getBytes());
    assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
    baos.reset();
    // \r\n
    response.write("Hello World!\r\nHave a nice day!".getBytes());
    assertEquals(baos.toString(), "data:Hello World!\r\ndata:Have a nice day!\r\n\r\n");
}
Also used : ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtmosphereResponse(org.atmosphere.cpr.AtmosphereResponse) ServletOutputStream(jakarta.servlet.ServletOutputStream) AsyncSupport(org.atmosphere.cpr.AsyncSupport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AtmosphereRequest(org.atmosphere.cpr.AtmosphereRequest) AtmosphereResourceImpl(org.atmosphere.cpr.AtmosphereResourceImpl) WriteListener(jakarta.servlet.WriteListener) Test(org.testng.annotations.Test)

Example 2 with ServletOutputStream

use of jakarta.servlet.ServletOutputStream in project tomcat by apache.

the class HttpServlet method doTrace.

/**
 * Called by the server (via the <code>service</code> method)
 * to allow a servlet to handle a TRACE request.
 *
 * A TRACE returns the headers sent with the TRACE
 * request to the client, so that they can be used in
 * debugging. There's no need to override this method.
 *
 * @param req   the {@link HttpServletRequest} object that
 *                  contains the request the client made of
 *                  the servlet
 *
 * @param resp  the {@link HttpServletResponse} object that
 *                  contains the response the servlet returns
 *                  to the client
 *
 * @exception IOException   if an input or output error occurs
 *                              while the servlet is handling the
 *                              TRACE request
 *
 * @exception ServletException  if the request for the
 *                                  TRACE cannot be handled
 */
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int responseLength;
    String CRLF = "\r\n";
    StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI()).append(" ").append(req.getProtocol());
    Enumeration<String> reqHeaderEnum = req.getHeaderNames();
    while (reqHeaderEnum.hasMoreElements()) {
        String headerName = reqHeaderEnum.nextElement();
        buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));
    }
    buffer.append(CRLF);
    responseLength = buffer.length();
    resp.setContentType("message/http");
    resp.setContentLength(responseLength);
    ServletOutputStream out = resp.getOutputStream();
    out.print(buffer.toString());
    out.close();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream)

Example 3 with ServletOutputStream

use of jakarta.servlet.ServletOutputStream in project spring-framework by spring-projects.

the class AbstractXlsView method renderWorkbook.

/**
 * The actual render step: taking the POI {@link Workbook} and rendering
 * it to the given response.
 * @param workbook the POI Workbook to render
 * @param response current HTTP response
 * @throws IOException when thrown by I/O methods that we're delegating to
 */
protected void renderWorkbook(Workbook workbook, HttpServletResponse response) throws IOException {
    ServletOutputStream out = response.getOutputStream();
    workbook.write(out);
    workbook.close();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream)

Example 4 with ServletOutputStream

use of jakarta.servlet.ServletOutputStream in project spring-framework by spring-projects.

the class AbstractView method writeToResponse.

/**
 * Write the given temporary OutputStream to the HTTP response.
 * @param response current HTTP response
 * @param baos the temporary OutputStream to write
 * @throws IOException if writing/flushing failed
 */
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException {
    // Write content type and also length (determined via byte array).
    response.setContentType(getContentType());
    response.setContentLength(baos.size());
    // Flush byte array to servlet output stream.
    ServletOutputStream out = response.getOutputStream();
    baos.writeTo(out);
    out.flush();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream)

Example 5 with ServletOutputStream

use of jakarta.servlet.ServletOutputStream in project spring-framework by spring-projects.

the class SockJsServiceTests method handleInfoGetWildflyNPE.

// SPR-11919
@Test
public void handleInfoGetWildflyNPE() throws IOException {
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    ServletOutputStream ous = mock(ServletOutputStream.class);
    given(mockResponse.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).willThrow(NullPointerException.class);
    given(mockResponse.getOutputStream()).willReturn(ous);
    this.response = new ServletServerHttpResponse(mockResponse);
    handleRequest("GET", "/echo/info", HttpStatus.OK);
    verify(mockResponse, times(1)).getOutputStream();
}
Also used : ServletOutputStream(jakarta.servlet.ServletOutputStream) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ServletOutputStream (jakarta.servlet.ServletOutputStream)13 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)5 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 ServletResponse (jakarta.servlet.ServletResponse)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ResponseFacade (org.apache.catalina.connector.ResponseFacade)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 WireFeedOutput (com.rometools.rome.io.WireFeedOutput)1 ServletResponseWrapper (jakarta.servlet.ServletResponseWrapper)1 WriteListener (jakarta.servlet.WriteListener)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1