Subversion Repositories SmartDukaan

Rev

Rev 34405 | 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
 
34405 vikas.jang 3
import com.fasterxml.jackson.core.JsonProcessingException;
35418 aman 4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.model.ProfitMandiResponse;
7
import com.spice.profitmandi.common.model.Response;
8
import com.spice.profitmandi.common.model.ResponseStatus;
23950 amit.gupta 9
import org.springframework.beans.factory.annotation.Autowired;
21739 ashik.ali 10
import org.springframework.http.HttpStatus;
11
import org.springframework.http.MediaType;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Component;
14
 
35418 aman 15
import javax.servlet.http.HttpServletRequest;
16
import javax.servlet.http.HttpServletResponse;
17
import java.io.File;
18
import java.nio.file.Files;
19
import java.util.Map;
21739 ashik.ali 20
 
21
@Component
22
public class ResponseSender<T> {
23
 
24
 
23950 amit.gupta 25
	@Autowired
26
	ObjectMapper objectMapper;
27
 
21739 ashik.ali 28
	public ResponseEntity<?> ok(Object response){
22987 ashik.ali 29
		return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);
21739 ashik.ali 30
	}
34405 vikas.jang 31
 
32
	public ResponseEntity<?> okJson(Object response) throws JsonProcessingException {
33
		String jsonString = objectMapper.writeValueAsString(response);
34
		Map<String, Object> actualJson = objectMapper.readValue(jsonString, Map.class);
35
		return this.genericSuccess(actualJson);
36
	}
37
 
22924 ashik.ali 38
	public ResponseEntity<?> badRequest(Object response){
22987 ashik.ali 39
		return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);
22924 ashik.ali 40
	}
41
	public ResponseEntity<?> unauthorized(Object response){
22987 ashik.ali 42
		return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);
22924 ashik.ali 43
	}
44
	public ResponseEntity<?> forbidden(Object response){
22987 ashik.ali 45
		return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);
22924 ashik.ali 46
	}
47
	public ResponseEntity<?> notFound(Object response){
22987 ashik.ali 48
		return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);
22924 ashik.ali 49
	}
23017 ashik.ali 50
	public ResponseEntity<?> internalServerError(Object response){
51
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE);
52
	}
21739 ashik.ali 53
	public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){
54
		return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);
55
	}
56
	public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){
57
		return this.genericError(profitMandiBusinessException, HttpStatus.UNAUTHORIZED);
58
	}
59
	public ResponseEntity<?> forbidden(ProfitMandiBusinessException profitMandiBusinessException){
60
		return this.genericError(profitMandiBusinessException, HttpStatus.FORBIDDEN);
61
	}
62
	public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){
63
		return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);
64
	}
65
	public ResponseEntity<?> internalServerError(Throwable exception){
22924 ashik.ali 66
		final Response response=new Response("", "", "GE_1007", exception.getMessage());
22987 ashik.ali 67
		return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);
21739 ashik.ali 68
	}
69
 
70
	private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){
71
		final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
22987 ashik.ali 72
		return this.genericError(response, status);
21739 ashik.ali 73
	}
74
 
22987 ashik.ali 75
	private ResponseEntity<?> genericError(Response response, HttpStatus status){
21739 ashik.ali 76
		final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);
77
		return new ResponseEntity<>(profitMandiResponse, status);
78
	}
79
 
22987 ashik.ali 80
	private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){
81
		final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);
82
		return new ResponseEntity<>(profitMandiResponse, status);
83
	}
34405 vikas.jang 84
 
85
	private ResponseEntity<?> genericSuccess(Object response) {
86
		final ProfitMandiResponse<?> profitMandiResponse =
87
				new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);
88
 
89
		return ResponseEntity.ok()
90
				.contentType(MediaType.APPLICATION_JSON)
91
				.body(profitMandiResponse);
92
	}
22987 ashik.ali 93
 
21739 ashik.ali 94
	public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
95
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);
96
	}
97
 
98
	public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{
99
		response.setStatus(HttpStatus.OK.value());
100
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
101
		response.setCharacterEncoding("UTF-8");
22445 amit.gupta 102
		response.getWriter().println(data);
21739 ashik.ali 103
		response.getWriter().flush();
104
	}
105
 
22813 amit.gupta 106
	public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{
107
		response.setStatus(HttpStatus.OK.value());
108
		response.setContentType(contentType);
22816 amit.gupta 109
		response.getOutputStream().write(Files.readAllBytes(file.toPath()));
110
		response.getOutputStream().flush();
22813 amit.gupta 111
	}
112
 
21739 ashik.ali 113
	public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
114
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);
115
	}
116
 
117
	public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
118
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);
119
	}
120
 
121
	public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{
122
		this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);
123
	}
35418 aman 124
 
125
    private void addCorsHeaders(HttpServletRequest request, HttpServletResponse response) {
126
        String origin = request.getHeader("Origin");
127
        if (origin != null && !origin.isEmpty()) {
128
            response.setHeader("Access-Control-Allow-Origin", origin);
129
            response.setHeader("Access-Control-Allow-Credentials", "true");
130
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, PATCH");
131
            response.setHeader("Access-Control-Allow-Headers",
132
                    "Content-Type, Auth-Token, Authorization, X-Requested-With, Idempotencykey");
133
            response.setHeader("Access-Control-Expose-Headers", "Auth-Token, Content-Type, Authorization");
134
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
135
            response.setHeader("Pragma", "no-cache");
136
            response.setHeader("Expires", "0");
137
        }
138
    }
139
 
140
    private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response,
141
                                     Response failedResponse, HttpStatus httpStatus) throws Exception {
142
        this.addCorsHeaders(request, response);
143
 
144
        response.setStatus(httpStatus.value());
145
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
146
        response.setCharacterEncoding("UTF-8");
147
        final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(
148
                httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);
149
        final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);
150
        final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
151
        response.getWriter().write(jsonString.toString());
152
        response.getWriter().flush();
153
    }
21739 ashik.ali 154
 
155
}