Rev 34405 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.common.web.util;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiResponse;import com.spice.profitmandi.common.model.Response;import com.spice.profitmandi.common.model.ResponseStatus;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.nio.file.Files;import java.util.Map;@Componentpublic class ResponseSender<T> {@AutowiredObjectMapper objectMapper;public ResponseEntity<?> ok(Object response){return this.genericError(response, HttpStatus.OK, ResponseStatus.SUCCESS);}public ResponseEntity<?> okJson(Object response) throws JsonProcessingException {String jsonString = objectMapper.writeValueAsString(response);Map<String, Object> actualJson = objectMapper.readValue(jsonString, Map.class);return this.genericSuccess(actualJson);}public ResponseEntity<?> badRequest(Object response){return this.genericError(response, HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE);}public ResponseEntity<?> unauthorized(Object response){return this.genericError(response, HttpStatus.UNAUTHORIZED, ResponseStatus.FAILURE);}public ResponseEntity<?> forbidden(Object response){return this.genericError(response, HttpStatus.FORBIDDEN, ResponseStatus.FAILURE);}public ResponseEntity<?> notFound(Object response){return this.genericError(response, HttpStatus.NOT_FOUND, ResponseStatus.FAILURE);}public ResponseEntity<?> internalServerError(Object response){return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE);}public ResponseEntity<?> badRequest(ProfitMandiBusinessException profitMandiBusinessException){return this.genericError(profitMandiBusinessException, HttpStatus.BAD_REQUEST);}public ResponseEntity<?> unauthorized(ProfitMandiBusinessException profitMandiBusinessException){return this.genericError(profitMandiBusinessException, HttpStatus.UNAUTHORIZED);}public ResponseEntity<?> forbidden(ProfitMandiBusinessException profitMandiBusinessException){return this.genericError(profitMandiBusinessException, HttpStatus.FORBIDDEN);}public ResponseEntity<?> notFound(ProfitMandiBusinessException profitMandiBusinessException){return this.genericError(profitMandiBusinessException, HttpStatus.NOT_FOUND);}public ResponseEntity<?> internalServerError(Throwable exception){final Response response=new Response("", "", "GE_1007", exception.getMessage());return this.genericError(response, HttpStatus.INTERNAL_SERVER_ERROR);}private ResponseEntity<?> genericError(ProfitMandiBusinessException profitMandiBusinessException, HttpStatus status){final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());return this.genericError(response, status);}private ResponseEntity<?> genericError(Response response, HttpStatus status){final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);return new ResponseEntity<>(profitMandiResponse, status);}private ResponseEntity<?> genericError(Object response, HttpStatus status, ResponseStatus responseStatus){final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(status.toString(), status, responseStatus, response);return new ResponseEntity<>(profitMandiResponse, status);}private ResponseEntity<?> genericSuccess(Object response) {final ProfitMandiResponse<?> profitMandiResponse =new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(profitMandiResponse);}public void writeBadRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{this.writeGenericRequest(request, response, failedResponse, HttpStatus.BAD_REQUEST);}public void writeOk(HttpServletRequest request, HttpServletResponse response, Object data) throws Exception{response.setStatus(HttpStatus.OK.value());response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("UTF-8");response.getWriter().println(data);response.getWriter().flush();}public void writeOkFileContentType(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws Exception{response.setStatus(HttpStatus.OK.value());response.setContentType(contentType);response.getOutputStream().write(Files.readAllBytes(file.toPath()));response.getOutputStream().flush();}public void writeUnauthorized(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{this.writeGenericRequest(request, response, failedResponse, HttpStatus.UNAUTHORIZED);}public void writeForbidden(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{this.writeGenericRequest(request, response, failedResponse, HttpStatus.FORBIDDEN);}public void writeNotFound(HttpServletRequest request, HttpServletResponse response, Response failedResponse) throws Exception{this.writeGenericRequest(request, response, failedResponse, HttpStatus.NOT_FOUND);}private void addCorsHeaders(HttpServletRequest request, HttpServletResponse response) {String origin = request.getHeader("Origin");if (origin != null && !origin.isEmpty()) {response.setHeader("Access-Control-Allow-Origin", origin);response.setHeader("Access-Control-Allow-Credentials", "true");response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, PATCH");response.setHeader("Access-Control-Allow-Headers","Content-Type, Auth-Token, Authorization, X-Requested-With, Idempotencykey");response.setHeader("Access-Control-Expose-Headers", "Auth-Token, Content-Type, Authorization");response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");response.setHeader("Pragma", "no-cache");response.setHeader("Expires", "0");}}private void writeGenericRequest(HttpServletRequest request, HttpServletResponse response,Response failedResponse, HttpStatus httpStatus) throws Exception {this.addCorsHeaders(request, response);response.setStatus(httpStatus.value());response.setContentType(MediaType.APPLICATION_JSON_VALUE);response.setCharacterEncoding("UTF-8");final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(httpStatus.toString(), httpStatus, ResponseStatus.FAILURE, failedResponse);final Object obj = objectMapper.readValue(objectMapper.writeValueAsString(profitMandiResponse), Object.class);final String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);response.getWriter().write(jsonString.toString());response.getWriter().flush();}}