Rev 22813 | Rev 22858 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.common.web.util;import java.io.File;import java.nio.file.Files;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;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;@Componentpublic class ResponseSender<T> {private final ObjectMapper objectMapper = new ObjectMapper();public ResponseEntity<?> ok(Object response){final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, response);return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);}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("", "", "errorCode", exception.getMessage());return this.generic(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.generic(response, status);}private ResponseEntity<?> generic(Response response, HttpStatus status){final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(status.toString(), status, ResponseStatus.FAILURE, response);return new ResponseEntity<>(profitMandiResponse, status);}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 writeGenericRequest(HttpServletRequest request, HttpServletResponse response, Response failedResponse, HttpStatus httpStatus) throws Exception{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();}}