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");
}
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();
}
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();
}
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();
}
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();
}
Aggregations