use of javax.servlet.WriteListener in project quickstart by wildfly.
the class JSONPRequestFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (!(request instanceof HttpServletRequest)) {
throw new ServletException("Only HttpServletRequest requests are supported");
}
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
// extract the callback method from the request query parameters
String callback = getCallbackMethod(httpRequest);
if (!isJSONPRequest(callback)) {
// Request is not a JSONP request move on
chain.doFilter(request, response);
} else {
// Need to check if the callback method is safe
if (!SAFE_PRN.matcher(callback).matches()) {
throw new ServletException("JSONP Callback method '" + CALLBACK_METHOD + "' parameter not valid function");
}
// Will stream updated response
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
// Create a custom response wrapper to adding in the padding
HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(httpResponse) {
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
byteStream.write(b);
}
@Override
public boolean isReady() {
// The stream is always ready
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
// Nothing to do
}
};
}
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(byteStream);
}
};
// Process the rest of the filter chain, including the JAX-RS request
chain.doFilter(request, responseWrapper);
// Override response content and encoding
response.setContentType(CONTENT_TYPE);
response.setCharacterEncoding("UTF-8");
// Write the padded updates to the output stream.
response.getOutputStream().write((callback + "(").getBytes());
response.getOutputStream().write(byteStream.toByteArray());
response.getOutputStream().write(");".getBytes());
}
}
use of javax.servlet.WriteListener in project tutorials by eugenp.
the class AsyncServlet method doGet.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteBuffer content = ByteBuffer.wrap(HEAVY_RESOURCE.getBytes(StandardCharsets.UTF_8));
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
while (out.isReady()) {
if (!content.hasRemaining()) {
response.setStatus(200);
async.complete();
return;
}
out.write(content.get());
}
}
@Override
public void onError(Throwable t) {
getServletContext().log("Async Error", t);
async.complete();
}
});
}
use of javax.servlet.WriteListener in project zalenium by zalando.
the class DashboardCleanupServletTest method initMocksAndService.
@Before
public void initMocksAndService() throws IOException {
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
private StringBuilder stringBuilder = new StringBuilder();
@Override
public boolean isReady() {
System.out.println("isReady");
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
System.out.println("setWriteListener");
}
@Override
public void write(int b) {
this.stringBuilder.append((char) b);
}
public String toString() {
return stringBuilder.toString();
}
});
dashboardCleanupServlet = new DashboardCleanupServlet();
// Temporal folder for dashboard files
TestUtils.ensureRequiredFilesExistForCleanup(temporaryFolder);
}
use of javax.servlet.WriteListener in project zalenium by zalando.
the class DashboardInformationServletTest method initMocksAndService.
@Before
public void initMocksAndService() throws IOException {
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
private StringBuilder stringBuilder = new StringBuilder();
@Override
public boolean isReady() {
System.out.println("isReady");
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
System.out.println("setWriteListener");
}
@Override
public void write(int b) {
this.stringBuilder.append((char) b);
}
public String toString() {
return stringBuilder.toString();
}
});
dashboardInformationServlet = new DashboardInformationServlet();
}
use of javax.servlet.WriteListener in project killbill by killbill.
the class PluginResource method serviceViaOSGIPlugin.
private Response serviceViaOSGIPlugin(final HttpServletRequest request, final InputStream inputStream, @Nullable final MultivaluedMap<String, String> formData, final HttpServletResponse response, final ServletContext servletContext, final ServletConfig servletConfig, final UriInfo uriInfo) throws ServletException, IOException {
prepareOSGIRequest(request, servletContext, servletConfig);
final ServletRequest req = new OSGIServletRequestWrapper(request, inputStream, formData, uriInfo.getQueryParameters());
// The real ServletOutputStream is a HttpOutput, which we don't want to give to plugins.
// Jooby for instance would commit the underlying HTTP channel (via ServletServletResponse#send),
// meaning that any further headers (e.g. Profiling) that we would add would not be returned.
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final OSGIServletResponseWrapper res = new OSGIServletResponseWrapper(response, new ServletOutputStream() {
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(final WriteListener writeListener) {
throw new UnsupportedOperationException();
}
@Override
public void write(final int b) throws IOException {
byteArrayOutputStream.write(b);
}
});
osgiServlet.service(req, res);
if (response.getStatus() >= 400) {
log.warn("{} responded {}", request.getPathInfo(), response.getStatus());
}
return Response.status(response.getStatus()).entity(new String(byteArrayOutputStream.toByteArray(), "UTF-8")).build();
}
Aggregations