Subversion Repositories SmartDukaan

Rev

Rev 22432 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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