Skip to content

Instantly share code, notes, and snippets.

@vijayakumar-psg587
Created January 11, 2019 06:06
Show Gist options
  • Save vijayakumar-psg587/89e9be71f5d14f9a373391133b6f888f to your computer and use it in GitHub Desktop.
Save vijayakumar-psg587/89e9be71f5d14f9a373391133b6f888f to your computer and use it in GitHub Desktop.
DumpFilter
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
@Component
public class DumpFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
System.out.println(httpRequest.getRequestURI());
System.out.println(httpRequest.getRequestURI().toLowerCase().endsWith("/v2/api-docs"));
ByteArrayPrinter pw = new ByteArrayPrinter();
// here i create a response wrapper
HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) {
@Override
public PrintWriter getWriter() {
return pw.getWriter();
}
@Override
public ServletOutputStream getOutputStream() {
return pw.getStream();
}
};
System.out.println("before chaingin");
// i get the response data from the stream
chain.doFilter(httpRequest, wrappedResp);
byte[] bytes = pw.toByteArray();
String respBody = new String(bytes);
System.out.println("in if" + respBody);
if (httpRequest.getParameter("group") != null) {
// RequestWrapper wrappedRequest = new RequestWrapper(httpRequest);
// wrappedRequest.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
// FileCopyUtils.copy(
// new FileInputStream(new File(System.getProperty("user.dir") + "/static/swagger.json")),
// response.getOutputStream());
byte[] newByte = pw.toByteArray();
String newString = new String(newByte);
System.out.println("new string:" + newString);
// i make a modification in the stream
String s = newString.replaceAll("basePath", "Vijayy");
System.out.println("printing s" + s);
// here i try to write to a file with the modification
try (FileOutputStream fos = new FileOutputStream(
new File(System.getProperty("user.dir") + "/static/openAPI.json"))) {
System.out.println("comin in if");
fos.write(s.getBytes());
// after writing it I read the file data and output it
// i actually modify the response data with just "basePath" as "Vijay" dyanmically.
// so in the actual openAPI spec I can do the modification of <Json> element
// this works actually. I will run it now
FileCopyUtils.copy(
new FileInputStream(new File(System.getProperty("user.dir") + "/static/openAPI.json")),
response.getOutputStream());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
// FileCopyUtils.copy(, out);
// IOUtils.copy(new ByteArrayInputStream(newString.getBytes()), response.getOutputStream());
} else {
chain.doFilter(httpRequest, wrappedResp);
response.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
response.setContentLength(respBody.length());
response.getOutputStream().write(respBody.getBytes());
}
// List<String> s13 = Stream.of(respBody).filter((s1) -> s1.contains("<Json>"))
// .map((sample) -> Arrays.asList(sample.split(" ")))
// .flatMap((listString) -> {
// StringBuffer sb = new StringBuffer();
// listString.forEach(item -> {
//
// sb.append(item);
//
// });
// return Stream.of(sb.toString().trim().replace("<Json>", "").replace("</Json>", ""));
// }).collect(Collectors.toList());
// s13.forEach(item -> System.out.println("items :" + item));
// String s14 = String.join("", s13);
return;
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment