All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mosip.pmp.partner.exception.PartnerControllerAdvice Maven / Gradle / Ivy

There is a newer version: 1.1.4
Show newest version
package io.mosip.pmp.partner.exception;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.util.ContentCachingRequestWrapper;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.mosip.pmp.partner.constant.PartnerInputExceptionConstant;
import io.mosip.pmp.partner.core.ResponseWrapper;

/**
 * @author sanjeev.shrivastava
 *
 */

@RestControllerAdvice
public class PartnerControllerAdvice extends ResponseEntityExceptionHandler {

	@Autowired
	private ObjectMapper objectMapper;

	String msg = "mosip.partnermanagement";
	String version = "1.0";

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Partner already registered exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(PartnerAlreadyRegisteredException.class)
	public ResponseEntity> getExcepionMassage(
			final HttpServletRequest httpServletRequest, final PartnerAlreadyRegisteredException exception)
			throws IOException {
		ResponseWrapper responseError = setErrors(httpServletRequest);
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	@ExceptionHandler(AuthenticationFailedException.class)
	public ResponseEntity> getAuthenticationFailedExceptionMassage(
			final HttpServletRequest httpServletRequest, final AuthenticationFailedException exception)
			throws IOException {
		ResponseWrapper responseError = setErrors(httpServletRequest);
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	@ExceptionHandler(PartnerAlreadyRegisteredWithSamePolicyGroupException.class)
	public ResponseEntity> getPartnerAlreadyRegisteredWithSamePolicyGroupExceptionMassage(
			final HttpServletRequest httpServletRequest,
			final PartnerAlreadyRegisteredWithSamePolicyGroupException exception) throws IOException {
		ResponseWrapper responseError = setErrors(httpServletRequest);
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	@Override
	protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
			HttpHeaders headers, HttpStatus status, WebRequest request) {
		Map body = new LinkedHashMap<>();
		body.put("timestamp", new Date());
		body.put("status", status.value());
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(PartnerInputExceptionConstant.MISSING_PARTNER_INPUT_PARAMETER.getErrorCode());
		errorResponse.setMessage(PartnerInputExceptionConstant.MISSING_PARTNER_INPUT_PARAMETER.getErrorMessage());
		List errors = new ArrayList<>();
		errors.add(errorResponse);

		body.put("errors", errors);
		return new ResponseEntity<>(body, headers, status);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Partner Does Not Exist Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */

	@ExceptionHandler(PartnerDoesNotExistException.class)
	public ResponseEntity> getExcepionMassages(
			final HttpServletRequest httpServletRequest, final PartnerDoesNotExistException exception)
			throws IOException {
		ResponseWrapper responseError = setErrors(httpServletRequest);
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Policy Group Does Not Exist Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(PolicyGroupDoesNotExistException.class)
	public ResponseEntity> getExcepionMassages(
			final HttpServletRequest httpServletRequest, final PolicyGroupDoesNotExistException exception)
			throws IOException {
		ResponseWrapper responseError = setErrors(httpServletRequest);
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Partner API Key Is Not Created Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(PartnerAPIKeyIsNotCreatedException.class)
	public ResponseEntity> getExcepionMassages(
			final HttpServletRequest httpServletRequest, final PartnerAPIKeyIsNotCreatedException exception) {
		ResponseWrapper responseError = new ResponseWrapper<>();
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		responseError.setId(msg);
		responseError.setVersion(version);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains APIKeyReqId Status In Progress Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(APIKeyReqIdStatusInProgressException.class)
	public ResponseEntity> getExcepionMassages(
			final HttpServletRequest httpServletRequest, final APIKeyReqIdStatusInProgressException exception) {
		ResponseWrapper responseError = new ResponseWrapper<>();
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		responseError.setId(msg);
		responseError.setVersion(version);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Partner APIKeyReqID Does Not Exist Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(PartnerAPIKeyReqIDDoesNotExistException.class)
	public ResponseEntity> getPartnerAPIKEYExcepionMassages(
			final HttpServletRequest httpServletRequest, final PartnerAPIKeyReqIDDoesNotExistException exception) {
		ResponseWrapper responseError = new ResponseWrapper<>();
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Partner Does Not Exists Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */
	@ExceptionHandler(PartnerDoesNotExistsException.class)
	public ResponseEntity> getExcepionsMassages(
			final HttpServletRequest httpServletRequest, final PartnerDoesNotExistsException exception) {
		ResponseWrapper responseError = new ResponseWrapper<>();
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setErrorCode(exception.getErrorCode());
		errorResponse.setMessage(exception.getErrorText());
		responseError.setId(msg);
		responseError.setVersion(version);
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @param exception
	 *            this class contains Checked Exception
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */

	@ExceptionHandler(Exception.class)
	public ResponseEntity> getAllExcepionMassage(
			final HttpServletRequest httpServletRequest, final Exception exception) {
		ResponseWrapper responseError = new ResponseWrapper<>();
		ErrorResponse errorResponse = new ErrorResponse();
		errorResponse.setMessage(exception.getMessage());
		responseError.setErrors(errorResponse);
		return new ResponseEntity<>(responseError, HttpStatus.OK);
	}

	/**
	 * @param httpServletRequest
	 *            this class contains servlet request
	 * @return this class contains errorCode and message
	 * @throws IOException
	 *             this class contains Checked Exception
	 */

	private ResponseWrapper setErrors(HttpServletRequest httpServletRequest) throws IOException {
		ResponseWrapper responseWrapper = new ResponseWrapper<>();
		String requestBody = null;
		if (httpServletRequest instanceof ContentCachingRequestWrapper) {
			requestBody = new String(((ContentCachingRequestWrapper) httpServletRequest).getContentAsByteArray());
		}

		JsonNode reqNode = objectMapper.readTree(requestBody);
		responseWrapper.setId(reqNode.path("id").asText());
		responseWrapper.setVersion(reqNode.path("version").asText());
		return responseWrapper;
	}
}