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

io.imunity.scim.exception.providers.EngineExceptionMapper Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package io.imunity.scim.exception.providers;

import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import org.apache.logging.log4j.Logger;

import pl.edu.icm.unity.base.utils.Log;
import pl.edu.icm.unity.engine.api.authn.AuthenticationException;
import pl.edu.icm.unity.engine.api.authn.AuthorizationException;

@Provider
class EngineExceptionMapper implements ExceptionMapper
{
	private static final Logger log = Log.getLogger(Log.U_SERVER_SCIM, EngineExceptionMapper.class);

	public Response toResponse(Exception ex)
	{
		if (ex instanceof AuthorizationException || ex instanceof AuthenticationException)
		{
			log.debug("Access denied for SCIM API client", ex);
			return Response.status(Status.FORBIDDEN).entity(ErrorResponse.builder()
					.withStatus(Status.FORBIDDEN.getStatusCode()).withDetail("Forbidden").build().toJsonString())
					.type(MediaType.APPLICATION_JSON).build();
		} else if (ex instanceof ClientErrorException)
		{
			return ((ClientErrorException) ex).getResponse();
		} else
		{
			log.debug("Engine exception during SCIM API invocation", ex);
			return Response.status(Status.BAD_REQUEST).entity(ErrorResponse.builder()
					.withStatus(Status.BAD_REQUEST.getStatusCode()).withDetail("Bad Request").build().toJsonString())
					.type(MediaType.APPLICATION_JSON).build();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy