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