Subversion Repositories SmartDukaan

Rev

Rev 22987 | Rev 23950 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
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){
22987 ashik.ali 26
		return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
21739 ashik.ali 27
	}
22924 ashik.ali 28
	public ResponseEntity<?> badRequest(Object response){
22987 ashik.ali 29
		return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
22924 ashik.ali 30
	}
31
	public ResponseEntity<?> unauthorized(Object response){
22987 ashik.ali 32
		return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
22924 ashik.ali 33
	}
34
	public ResponseEntity<?> forbidden(Object response){
22987 ashik.ali 35
		return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);
22924 ashik.ali 36
	}
37
	public ResponseEntity<?> notFound(Object response){
22987 ashik.ali 38
		return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);
22924 ashik.ali 39
	}
23017 ashik.ali 40
	public ResponseEntity<?> internalServerError(Object response){
41
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE);
42
	}
21739 ashik.ali 43
	public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
44
		return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
45
	}
46
	public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
47
		return this.genericError(profitMandiBusinessException, HttpStatus.UNAUTHORIZED);
48
	}
49
	public ResponseEntity<?> forbidden(ProfitMandiBusinessException profitMandiBusinessException){
50
		return this.genericError(profitMandiBusinessException, HttpStatus.FORBIDDEN);
51
	}
52
	public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
53
		return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
54
	}
55
	public ResponseEntity<?> internalServerError(Throwable exception){
22924 ashik.ali 56
		final Response response=new Response("", "", "GE_1007", exception.getMessage());
22987 ashik.ali 57
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);
21739 ashik.ali 58
	}
59
 
60
	private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
61
		final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
22987 ashik.ali 62
		return this.genericError(response, status);
21739 ashik.ali 63
	}
64
 
22987 ashik.ali 65
	private ResponseEntity<?> genericError(Response response, HttpStatus status){
21739 ashik.ali 66
		final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
67
		return new ResponseEntity<>(profitMandiResponse, status);
68
	}
69
 
22987 ashik.ali 70
	private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
71
		final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
72
		return new ResponseEntity<>(profitMandiResponse, status);
73
	}
74
 
21739 ashik.ali 75
	public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
76
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
77
	}
78
 
79
	public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{
80
		response.setStatus(HttpStatus.OK.value());
81
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
82
		response.setCharacterEncoding("UTF-8");
22445 amit.gupta 83
		response.getWriter().println(data);
21739 ashik.ali 84
		response.getWriter().flush();
85
	}
86
 
22813 amit.gupta 87
	public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{
88
		response.setStatus(HttpStatus.OK.value());
89
		response.setContentType(contentType);
22816 amit.gupta 90
		response.getOutputStream().write(Files.readAllBytes(file.toPath()));
91
		response.getOutputStream().flush();
22813 amit.gupta 92
	}
93
 
21739 ashik.ali 94
	public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
95
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);
96
	}
97
 
98
	public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
99
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);
100
	}
101
 
102
	public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
103
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
104
	}
105
	private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{
106
		response.setStatus(httpStatus.value());
107
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
108
		response.setCharacterEncoding("UTF-8");
109
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
110
		final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
111
		final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
112
		response.getWriter().write(jsonString.toString());
113
		response.getWriter().flush();
114
	}
115
 
116
}