| 21277 |
ashik.ali |
1 |
package com.spice.profitmandi.web.processor;
|
| 21165 |
ashik.ali |
2 |
|
|
|
3 |
import java.io.BufferedReader;
|
|
|
4 |
import java.io.InputStream;
|
|
|
5 |
import java.io.InputStreamReader;
|
|
|
6 |
import java.time.LocalDateTime;
|
|
|
7 |
|
|
|
8 |
import javax.servlet.http.HttpServletRequest;
|
|
|
9 |
import javax.servlet.http.HttpServletResponse;
|
|
|
10 |
|
| 21220 |
ashik.ali |
11 |
import org.springframework.http.HttpStatus;
|
|
|
12 |
|
| 21165 |
ashik.ali |
13 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 21740 |
ashik.ali |
14 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
|
|
15 |
import com.spice.profitmandi.common.model.Response;
|
|
|
16 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
| 21165 |
ashik.ali |
17 |
|
|
|
18 |
public class JsonProcessor {
|
| 21220 |
ashik.ali |
19 |
|
| 21390 |
ashik.ali |
20 |
public static void writeJsonValidationFailedResponse(HttpServletRequest request, HttpServletResponse response, Response jsonValidationResponse)throws Exception{
|
| 21165 |
ashik.ali |
21 |
|
|
|
22 |
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
23 |
response.setContentType("application/json");
|
|
|
24 |
response.setCharacterEncoding("UTF-8");
|
| 21220 |
ashik.ali |
25 |
final ProfitMandiResponse<?> chatOnResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, jsonValidationResponse);
|
| 21165 |
ashik.ali |
26 |
final ObjectMapper objectMapper=new ObjectMapper();
|
|
|
27 |
final Object obj=objectMapper.readValue(objectMapper.writeValueAsString(chatOnResponse),Object.class);
|
|
|
28 |
final String jsonString=objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
|
|
29 |
response.getWriter().write(jsonString.toString());
|
|
|
30 |
response.getWriter().flush();
|
|
|
31 |
}
|
|
|
32 |
public static String getBody(HttpServletRequest request)throws Exception{
|
|
|
33 |
final StringBuilder stringBuilder=new StringBuilder();
|
|
|
34 |
final InputStream inputStream=request.getInputStream();
|
|
|
35 |
if (inputStream != null) {
|
|
|
36 |
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
37 |
final char[] charBuffer = new char[128];
|
|
|
38 |
int bytesRead = -1;
|
|
|
39 |
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
|
|
40 |
stringBuilder.append(charBuffer, 0, bytesRead);
|
|
|
41 |
}
|
|
|
42 |
} else {
|
|
|
43 |
stringBuilder.append("");
|
|
|
44 |
}
|
|
|
45 |
return stringBuilder.toString();
|
|
|
46 |
}
|
|
|
47 |
}
|