| 21739 |
ashik.ali |
1 |
package com.spice.profitmandi.common.web.util;
|
|
|
2 |
|
| 22861 |
ashik.ali |
3 |
import java.io.File;
|
|
|
4 |
import java.nio.file.Files;
|
|
|
5 |
|
| 21739 |
ashik.ali |
6 |
import javax.servlet.http.HttpServletRequest;
|
|
|
7 |
import javax.servlet.http.HttpServletResponse;
|
|
|
8 |
|
| 23950 |
amit.gupta |
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21739 |
ashik.ali |
10 |
import org.springframework.http.HttpStatus;
|
|
|
11 |
import org.springframework.http.MediaType;
|
|
|
12 |
import org.springframework.http.ResponseEntity;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
17 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
|
|
18 |
import com.spice.profitmandi.common.model.Response;
|
|
|
19 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
|
|
20 |
|
|
|
21 |
@Component
|
|
|
22 |
public class ResponseSender<T> {
|
|
|
23 |
|
|
|
24 |
|
| 23950 |
amit.gupta |
25 |
@Autowired
|
|
|
26 |
ObjectMapper objectMapper;
|
|
|
27 |
|
| 21739 |
ashik.ali |
28 |
public ResponseEntity<?> ok(Object response){
|
| 22987 |
ashik.ali |
29 |
return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
|
| 21739 |
ashik.ali |
30 |
}
|
| 22924 |
ashik.ali |
31 |
public ResponseEntity<?> badRequest(Object response){
|
| 22987 |
ashik.ali |
32 |
return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
|
| 22924 |
ashik.ali |
33 |
}
|
|
|
34 |
public ResponseEntity<?> unauthorized(Object response){
|
| 22987 |
ashik.ali |
35 |
return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
|
| 22924 |
ashik.ali |
36 |
}
|
|
|
37 |
public ResponseEntity<?> forbidden(Object response){
|
| 22987 |
ashik.ali |
38 |
return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);
|
| 22924 |
ashik.ali |
39 |
}
|
|
|
40 |
public ResponseEntity<?> notFound(Object response){
|
| 22987 |
ashik.ali |
41 |
return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);
|
| 22924 |
ashik.ali |
42 |
}
|
| 23017 |
ashik.ali |
43 |
public ResponseEntity<?> internalServerError(Object response){
|
|
|
44 |
return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE);
|
|
|
45 |
}
|
| 21739 |
ashik.ali |
46 |
public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
47 |
return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
|
|
|
48 |
}
|
|
|
49 |
public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
50 |
return this.genericError(profitMandiBusinessException, HttpStatus.UNAUTHORIZED);
|
|
|
51 |
}
|
|
|
52 |
public ResponseEntity<?> forbidden(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
53 |
return this.genericError(profitMandiBusinessException, HttpStatus.FORBIDDEN);
|
|
|
54 |
}
|
|
|
55 |
public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
56 |
return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
|
|
|
57 |
}
|
|
|
58 |
public ResponseEntity<?> internalServerError(Throwable exception){
|
| 22924 |
ashik.ali |
59 |
final Response response=new Response("", "", "GE_1007", exception.getMessage());
|
| 22987 |
ashik.ali |
60 |
return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
| 21739 |
ashik.ali |
61 |
}
|
|
|
62 |
|
|
|
63 |
private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
|
|
|
64 |
final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
|
| 22987 |
ashik.ali |
65 |
return this.genericError(response, status);
|
| 21739 |
ashik.ali |
66 |
}
|
|
|
67 |
|
| 22987 |
ashik.ali |
68 |
private ResponseEntity<?> genericError(Response response, HttpStatus status){
|
| 21739 |
ashik.ali |
69 |
final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
|
|
|
70 |
return new ResponseEntity<>(profitMandiResponse, status);
|
|
|
71 |
}
|
|
|
72 |
|
| 22987 |
ashik.ali |
73 |
private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
|
|
|
74 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
|
|
|
75 |
return new ResponseEntity<>(profitMandiResponse, status);
|
|
|
76 |
}
|
|
|
77 |
|
| 21739 |
ashik.ali |
78 |
public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
|
|
79 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{
|
|
|
83 |
response.setStatus(HttpStatus.OK.value());
|
|
|
84 |
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
85 |
response.setCharacterEncoding("UTF-8");
|
| 22445 |
amit.gupta |
86 |
response.getWriter().println(data);
|
| 21739 |
ashik.ali |
87 |
response.getWriter().flush();
|
|
|
88 |
}
|
|
|
89 |
|
| 22813 |
amit.gupta |
90 |
public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{
|
|
|
91 |
response.setStatus(HttpStatus.OK.value());
|
|
|
92 |
response.setContentType(contentType);
|
| 22816 |
amit.gupta |
93 |
response.getOutputStream().write(Files.readAllBytes(file.toPath()));
|
|
|
94 |
response.getOutputStream().flush();
|
| 22813 |
amit.gupta |
95 |
}
|
|
|
96 |
|
| 21739 |
ashik.ali |
97 |
public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
|
|
98 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
|
|
102 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
|
|
106 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
|
|
|
107 |
}
|
|
|
108 |
private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{
|
|
|
109 |
response.setStatus(httpStatus.value());
|
|
|
110 |
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
111 |
response.setCharacterEncoding("UTF-8");
|
|
|
112 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
|
|
|
113 |
final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
|
|
|
114 |
final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
|
|
115 |
response.getWriter().write(jsonString.toString());
|
|
|
116 |
response.getWriter().flush();
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
}
|