Subversion Repositories SmartDukaan

Rev

Rev 22924 | Rev 23017 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22924 Rev 22987
Line 21... Line 21...
21
public class ResponseSender<T> {
21
public class ResponseSender<T> {
22
	
22
	
23
	private final ObjectMapper objectMapper = new ObjectMapper();
23
	private final ObjectMapper objectMapper = new ObjectMapper();
24
	
24
	
25
	public ResponseEntity<?> ok(Object response){
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);
26
		return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
28
	}
27
	}
29
	public ResponseEntity<?> badRequest(Object response){
28
	public ResponseEntity<?> badRequest(Object response){
30
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
-
 
31
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.BAD_REQUEST);
29
		return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
32
	}
30
	}
33
	public ResponseEntity<?> unauthorized(Object response){
31
	public ResponseEntity<?> unauthorized(Object response){
34
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
-
 
35
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.UNAUTHORIZED);
32
		return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
36
	}
33
	}
37
	public ResponseEntity<?> forbidden(Object response){
34
	public ResponseEntity<?> forbidden(Object response){
38
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
-
 
39
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
35
		return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);
40
	}
36
	}
41
	public ResponseEntity<?> notFound(Object response){
37
	public ResponseEntity<?> notFound(Object response){
42
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
-
 
43
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.NOT_FOUND);
38
		return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);
44
	}
39
	}
45
	public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
40
	public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
46
		return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
41
		return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
47
	}
42
	}
48
	public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
43
	public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
Line 54... Line 49...
54
	public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
49
	public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
55
		return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
50
		return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
56
	}
51
	}
57
	public ResponseEntity<?> internalServerError(Throwable exception){
52
	public ResponseEntity<?> internalServerError(Throwable exception){
58
		final Response response=new Response("", "", "GE_1007", exception.getMessage());
53
		final Response response=new Response("", "", "GE_1007", exception.getMessage());
59
		return this.generic(response, HttpStatus.INTERNAL_SERVER_ERROR);
54
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);
60
	}
55
	}
61
	
56
	
62
	private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
57
	private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
63
		final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
58
		final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
64
		return this.generic(response, status);
59
		return this.genericError(response, status);
65
	}
60
	}
66
	
61
	
67
	private ResponseEntity<?> generic(Response response, HttpStatus status){
62
	private ResponseEntity<?> genericError(Response response, HttpStatus status){
68
		final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
63
		final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
69
		return new ResponseEntity<>(profitMandiResponse, status);
64
		return new ResponseEntity<>(profitMandiResponse, status);
70
	}
65
	}
71
	
66
	
-
 
67
	private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
-
 
68
		final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
-
 
69
		return new ResponseEntity<>(profitMandiResponse, status);
-
 
70
	}
-
 
71
	
72
	public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
72
	public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
73
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
73
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
74
	}
74
	}
75
	
75
	
76
	public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{
76
	public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{