use of javax.servlet.http.HttpServletResponseWrapper in project dubbo-faker by moyada.
the class SwaggerFilter method doFilter.
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest hrequest = (HttpServletRequest) servletRequest;
String requestURI = hrequest.getRequestURI();
if ("/api-docs".equals(requestURI)) {
// RequestDispatcher dispatcher = servletRequest.getRequestDispatcher("/swagger-ui.html");
// dispatcher.forward(servletRequest, servletResponse);
HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
wrapper.sendRedirect("/swagger-ui.html");
return;
}
filterChain.doFilter(servletRequest, servletResponse);
}
use of javax.servlet.http.HttpServletResponseWrapper 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());
}
}
Aggregations