Subversion Repositories SmartDukaan

Rev

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;
34405 vikas.jang 5
import java.util.Map;
22861 ashik.ali 6
 
21739 ashik.ali 7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
 
34405 vikas.jang 10
import com.fasterxml.jackson.core.JsonProcessingException;
23950 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
21739 ashik.ali 12
import org.springframework.http.HttpStatus;
13
import org.springframework.http.MediaType;
14
import org.springframework.http.ResponseEntity;
15
import org.springframework.stereotype.Component;
16
 
17
import com.fasterxml.jackson.databind.ObjectMapper;
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.model.ProfitMandiResponse;
20
import com.spice.profitmandi.common.model.Response;
21
import com.spice.profitmandi.common.model.ResponseStatus;
22
 
23
@Component
24
public class ResponseSender<T> {
25
 
26
 
23950 amit.gupta 27
	@Autowired
28
	ObjectMapper objectMapper;
29
 
21739 ashik.ali 30
	public ResponseEntity<?> ok(Object response){
22987 ashik.ali 31
		return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
21739 ashik.ali 32
	}
34405 vikas.jang 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
 
22924 ashik.ali 40
	public ResponseEntity<?> badRequest(Object response){
22987 ashik.ali 41
		return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
22924 ashik.ali 42
	}
43
	public ResponseEntity<?> unauthorized(Object response){
22987 ashik.ali 44
		return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
22924 ashik.ali 45
	}
46
	public ResponseEntity<?> forbidden(Object response){
22987 ashik.ali 47
		return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);
22924 ashik.ali 48
	}
49
	public ResponseEntity<?> notFound(Object response){
22987 ashik.ali 50
		return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);
22924 ashik.ali 51
	}
23017 ashik.ali 52
	public ResponseEntity<?> internalServerError(Object response){
53
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE);
54
	}
21739 ashik.ali 55
	public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
56
		return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
57
	}
58
	public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
59
		return this.genericError(profitMandiBusinessException, HttpStatus.UNAUTHORIZED);
60
	}
61
	public ResponseEntity<?> forbidden(ProfitMandiBusinessException profitMandiBusinessException){
62
		return this.genericError(profitMandiBusinessException, HttpStatus.FORBIDDEN);
63
	}
64
	public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
65
		return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
66
	}
67
	public ResponseEntity<?> internalServerError(Throwable exception){
22924 ashik.ali 68
		final Response response=new Response("", "", "GE_1007", exception.getMessage());
22987 ashik.ali 69
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);
21739 ashik.ali 70
	}
71
 
72
	private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
73
		final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
22987 ashik.ali 74
		return this.genericError(response, status);
21739 ashik.ali 75
	}
76
 
22987 ashik.ali 77
	private ResponseEntity<?> genericError(Response response, HttpStatus status){
21739 ashik.ali 78
		final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
79
		return new ResponseEntity<>(profitMandiResponse, status);
80
	}
81
 
22987 ashik.ali 82
	private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
83
		final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
84
		return new ResponseEntity<>(profitMandiResponse, status);
85
	}
34405 vikas.jang 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
	}
22987 ashik.ali 95
 
21739 ashik.ali 96
	public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
97
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
98
	}
99
 
100
	public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{
101
		response.setStatus(HttpStatus.OK.value());
102
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
103
		response.setCharacterEncoding("UTF-8");
22445 amit.gupta 104
		response.getWriter().println(data);
21739 ashik.ali 105
		response.getWriter().flush();
106
	}
107
 
22813 amit.gupta 108
	public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{
109
		response.setStatus(HttpStatus.OK.value());
110
		response.setContentType(contentType);
22816 amit.gupta 111
		response.getOutputStream().write(Files.readAllBytes(file.toPath()));
112
		response.getOutputStream().flush();
22813 amit.gupta 113
	}
114
 
21739 ashik.ali 115
	public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
116
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);
117
	}
118
 
119
	public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
120
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);
121
	}
122
 
123
	public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
124
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
125
	}
126
	private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{
127
		response.setStatus(httpStatus.value());
128
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
129
		response.setCharacterEncoding("UTF-8");
130
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
131
		final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
132
		final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
133
		response.getWriter().write(jsonString.toString());
134
		response.getWriter().flush();
135
	}
136
 
137
}