| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.common.web.util;
|
1 |
package com.spice.profitmandi.common.web.util;
|
| 2 |
|
2 |
|
| 3 |
import java.io.File;
|
3 |
import java.io.File;
|
| 4 |
import java.nio.file.Files;
|
4 |
import java.nio.file.Files;
|
| - |
|
5 |
import java.util.Map;
|
| 5 |
|
6 |
|
| 6 |
import javax.servlet.http.HttpServletRequest;
|
7 |
import javax.servlet.http.HttpServletRequest;
|
| 7 |
import javax.servlet.http.HttpServletResponse;
|
8 |
import javax.servlet.http.HttpServletResponse;
|
| 8 |
|
9 |
|
| - |
|
10 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
| 9 |
import org.springframework.beans.factory.annotation.Autowired;
|
11 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 10 |
import org.springframework.http.HttpStatus;
|
12 |
import org.springframework.http.HttpStatus;
|
| 11 |
import org.springframework.http.MediaType;
|
13 |
import org.springframework.http.MediaType;
|
| 12 |
import org.springframework.http.ResponseEntity;
|
14 |
import org.springframework.http.ResponseEntity;
|
| 13 |
import org.springframework.stereotype.Component;
|
15 |
import org.springframework.stereotype.Component;
|
| Line 26... |
Line 28... |
| 26 |
ObjectMapper objectMapper;
|
28 |
ObjectMapper objectMapper;
|
| 27 |
|
29 |
|
| 28 |
public ResponseEntity<?> ok(Object response){
|
30 |
public ResponseEntity<?> ok(Object response){
|
| 29 |
return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
|
31 |
return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
|
| 30 |
}
|
32 |
}
|
| - |
|
33 |
|
| - |
|
34 |
public ResponseEntity<?> okJson(Object response) throws JsonProcessingException {
|
| - |
|
35 |
String jsonString = objectMapper.writeValueAsString(response);
|
| - |
|
36 |
Map<String, Object> actualJson = objectMapper.readValue(jsonString, Map.class);
|
| - |
|
37 |
return this.genericSuccess(actualJson);
|
| - |
|
38 |
}
|
| - |
|
39 |
|
| 31 |
public ResponseEntity<?> badRequest(Object response){
|
40 |
public ResponseEntity<?> badRequest(Object response){
|
| 32 |
return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
|
41 |
return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
|
| 33 |
}
|
42 |
}
|
| 34 |
public ResponseEntity<?> unauthorized(Object response){
|
43 |
public ResponseEntity<?> unauthorized(Object response){
|
| 35 |
return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
|
44 |
return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
|
| Line 72... |
Line 81... |
| 72 |
|
81 |
|
| 73 |
private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
|
82 |
private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
|
| 74 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
|
83 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
|
| 75 |
return new ResponseEntity<>(profitMandiResponse, status);
|
84 |
return new ResponseEntity<>(profitMandiResponse, status);
|
| 76 |
}
|
85 |
}
|
| - |
|
86 |
|
| - |
|
87 |
private ResponseEntity<?> genericSuccess(Object response) {
|
| - |
|
88 |
final ProfitMandiResponse<?> profitMandiResponse =
|
| - |
|
89 |
new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
|
| - |
|
90 |
|
| - |
|
91 |
return ResponseEntity.ok()
|
| - |
|
92 |
.contentType(MediaType.APPLICATION_JSON)
|
| - |
|
93 |
.body(profitMandiResponse);
|
| - |
|
94 |
}
|
| 77 |
|
95 |
|
| 78 |
public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
96 |
public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
|
| 79 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
|
97 |
this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
|
| 80 |
}
|
98 |
}
|
| 81 |
|
99 |
|