Subversion Repositories SmartDukaan

Rev

Rev 22816 | Rev 22861 | 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
 
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");
22445 amit.gupta 61
		response.getWriter().println(data);
21739 ashik.ali 62
		response.getWriter().flush();
63
	}
64
 
22813 amit.gupta 65
	public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{
66
		response.setStatus(HttpStatus.OK.value());
67
		response.setContentType(contentType);
22816 amit.gupta 68
		response.getOutputStream().write(Files.readAllBytes(file.toPath()));
69
		response.getOutputStream().flush();
22813 amit.gupta 70
	}
71
 
21739 ashik.ali 72
	public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
73
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);
74
	}
75
 
76
	public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
77
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);
78
	}
79
 
80
	public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
81
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
82
	}
83
	private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{
84
		response.setStatus(httpStatus.value());
85
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
86
		response.setCharacterEncoding("UTF-8");
87
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
88
		final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
89
		final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
90
		response.getWriter().write(jsonString.toString());
91
		response.getWriter().flush();
92
	}
93
 
94
}