我需要帮助。我正在尝试更新流。但它不起作用。 当前请求具有Imp对象的列表,通过使用jsonobject将imp列表替换为对象。之后,当我尝试发送Servlet进行过滤时,它会采用旧的。
public class ReadHttpServletRequest extends HttpServletRequestWrapper {
private final static Logger log = LoggerFactory.getLogger(Cross.class);
private String body = "";
private String header = "";
private final String IMP = "imp";
/**
* Constructs a request object wrapping the given request.
*
* @param request The request to wrap
* @throws IllegalArgumentException if the request is null
*/
public ReadHttpServletRequest(HttpServletRequest request) throws IOException {
super(request);
BufferedReader bufferedReader = request.getReader();
String line;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
setBody(stringBuffer.toString());
// fetch header
this.setHeader(request.getHeader("x-openrtb-version"));
log.info("Old:- "+"header " + getHeader() + ", body " + getBody());
}
@Override
public ServletInputStream getInputStream() throws IOException {
// update the stream
// getBody()
final ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(getBody().getBytes());
return new ServletInputStream() {
@Override
public int read() throws IOException {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() { return false; }
@Override
public boolean isReady() { return false; }
@Override
public void setReadListener(ReadListener listener) {}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public void setBody(String body) { this.body = body; }
public void setHeader(String header) { this.header = header; }
public String getBody() {
return this.body;
}
public String getHeader() { return this.header; }
private String getUpdateRequestBody(String body) {
JSONObject jsonRequest = (JSONObject) JSONSerializer.toJSON(body);
/**
* Find the imp's list and convert
* into the json-object then add into
* the request as update's
* */
if(jsonRequest.getJSONArray(IMP).size() > 0) {
JSONObject impObject = jsonRequest.getJSONArray(IMP).getJSONObject(0);
// remove the list of imp
jsonRequest.remove(IMP);
// add the new one into the json reqeust
jsonRequest.put(IMP, impObject);
}
// update the new
return jsonRequest.toString();
}
}
@Component
public class Cross implements Filter {
private final static Logger log = LoggerFactory.getLogger(Cross.class);
//********************************
// Filter Config //
//********************************
public enum Request { Success, Fail, Other }
@Autowired
ValidatorService validatorService;
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
// response
log.info("=================Request-Start=====================");
long before=System.currentTimeMillis();
ReadHttpServletRequest requestWrapper = new ReadHttpServletRequest((HttpServletRequest) servletRequest);
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "X-requested-with, Content-Type");
String body = requestWrapper.getBody();
String header = requestWrapper.getHeader();
if (body != null && header != null) {
/*
* Note:- This below line's first update the request and conver the imp's to imp by getting
* the single first index object. if the object is not there this will not process ad send the simple same
* json string for validation if the string valid then dofilter map the json into @Pojo.
* */
log.info("New:- "+"header " + header + ", body " + body);
if (validatorService.isResponseValidator(body, header.equals(InputType.REQUEST) ? InputType.REQUEST : InputType.REQUEST)) {
filterChain.doFilter(requestWrapper, servletResponse);
saveResponseTime(before, Request.Success);
} else {
/*
* Note:- IF Validator fail this will show error
* if imp's size 0 then here
* if imp's object not their then here
* */
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print("<html><head><title>Oops an error happened!</title></head>");
response.getWriter().print("<body>Something bad happened uh-oh!</body>");
response.getWriter().print(validatorService.showValidationResult());
response.getWriter().println("</html>");
saveResponseTime(before, Request.Fail);
}
// handle the other request
}else {
/*
* Note:- used for get-count
* No-Header, No-Body -> mean Get
* */
filterChain.doFilter(requestWrapper, servletResponse);
saveResponseTime(before, Request.Other);
}
}catch (NullPointerException e) {
log.error("--Error--> "+ e.getMessage());
}
}
@Override
public void destroy() { }
private void saveResponseTime(long before, Request request) {
long after = System.currentTimeMillis();
long result = after - before;
PushIdController.setCount(result, request);
log.info("Total response time -> (" + (result) + ") miliseconds");
log.info("=================Request-End=====================");
}
}
{
"userId": "1d3ejjj",
"userName": "Nabeel.amd93@gmail.com",
"phone": "03153817177",
"role": {
"roleId": "d423",
"name": "nabeel ahmed"
},
"video": {
"videoId": "123456d",
"h": "25",
"w": "25"
},
"imp": [{
"id": "padsfs1",
"name": "dsfsfs",
"xyz": "dfsfsdfads"
},{
"id": "padsfs2",
"name": "dsfsfs",
"xyz": "dfsfsdfads"
},
{
"id": "padsfs3",
"name": "dsfsfs",
"xyz": "dfsfsdfads"
}]
}
{
"userId": "1d3ejjj",
"userName": "Nabeel.amd93@gmail.com",
"phone": "03153817177",
"role": {
"roleId": "d423",
"name": "nabeel ahmed"
},
"video": {
"videoId": "123456d",
"h": "25",
"w": "25"
},
"imp": {
"id": "padsfs1",
"name": "dsfsfs",
"xyz": "dfsfsdfads"
}
}
在我的过滤器调用之后的所有过程。在请求中,旧流不会更新与新json流相关的内容。
filterChain.doFilter(requestWrapper, servletResponse);
答案 0 :(得分:0)
对于这个问题,我在这个问题中回答
https://stackoverflow.com/questions/50460834/parse-json-array-init-single-object-in-spring-boot-rest-controller