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

com.sap.cloud.sdk.s4hana.connectivity.exception.AccessDeniedException Maven / Gradle / Ivy

/*
 * Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved.
 */

package com.sap.cloud.sdk.s4hana.connectivity.exception;

import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.collect.Sets;
import com.sap.cloud.sdk.cloudplatform.security.Authorization;
import com.sap.cloud.sdk.cloudplatform.security.user.User;
import com.sap.cloud.sdk.cloudplatform.servlet.response.AccessDeniedResponse;
import com.sap.cloud.sdk.cloudplatform.servlet.response.ResponseWithErrorCode;

import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * Thrown when a certain service denies access to the requested resources.
 */
@NoArgsConstructor
public class AccessDeniedException extends QueryExecutionException
{
    private static final long serialVersionUID = 8256471661877663966L;

    public static AccessDeniedException raiseMissingAuthorizations(
        @Nullable final User user,
        @Nullable final Iterable missingAuthorizations )
    {
        return new AccessDeniedException(
            user,
            missingAuthorizations != null ? Sets.newHashSet(missingAuthorizations) : null);
    }

    @Getter
    @Nullable
    protected transient User user = null;

    @Getter
    @Nullable
    protected transient Set missingAuthorizations = null;

    public AccessDeniedException( @Nullable final String message )
    {
        super(message);
    }

    public AccessDeniedException( @Nullable final Throwable cause )
    {
        super(cause);
    }

    public AccessDeniedException( @Nullable final String message, @Nullable final Throwable cause )
    {
        super(message, cause);
    }

    public AccessDeniedException( @Nullable final User user, @Nullable final Set missingAuthorizations )
    {
        super(buildErrorMessage(user, missingAuthorizations));

        this.user = user;
        this.missingAuthorizations = missingAuthorizations;
    }

    private static String buildErrorMessage(
        @Nullable final User user,
        @Nullable final Iterable missingAuthorizations )
    {
        if( user != null ) {
            return "User "
                + user
                + " does not have the required authorizations"
                + (missingAuthorizations != null ? ": " + missingAuthorizations : "")
                + ".";
        } else {
            return "Missing authorizations" + (missingAuthorizations != null ? ": " + missingAuthorizations : "") + ".";
        }
    }

    /**
     * {@inheritDoc}
     */
    @Nonnull
    @Override
    public ResponseWithErrorCode getErrorResponse()
    {
        return new AccessDeniedResponse(null, null, "Access denied.");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy