public ReqT intercept()

in custos-services/custos-integration-services/tenant-management-service/src/main/java/org/apache/custos/tenant/management/interceptors/TenantManagementSuperTenantRestrictedOperationsInterceptorImpl.java [56:113]


    public <ReqT> ReqT intercept(String method, Metadata headers, ReqT msg) {

        if (method.equals("updateTenantStatus")) {
            if (((UpdateStatusRequest) msg).getSuperTenant()) {
                GetTenantsRequest allTenants = GetTenantsRequest.newBuilder().setOffset(0).setLimit(2).build();
                GetAllTenantsResponse tenantsResponse = tenantProfileClient.getAllTenants(allTenants);
                if (tenantsResponse.getTenantList().size() > 1) {
                    throw new UnAuthorizedException("Request is not authorized, You have more than one tenant registered with the system  " +
                            " and super tenant should be the first tenant, please clean up the databases", null);
                }
            } else {
                Optional<AuthClaim> claim = null;
                String token = getToken(headers);
                try {
                    claim = authorizeUsingUserToken(headers);
                } catch (Exception ex) {
                    LOGGER.error(" Authorizing error " + ex.getMessage());
                    throw new UnAuthorizedException("Request is not authorized", ex);
                }
                if (claim == null || claim.isEmpty() || !claim.get().isSuperTenant() || !claim.get().isAdmin()) {
                    throw new UnAuthorizedException("Request is not authorized", null);
                }
                return (ReqT) ((UpdateStatusRequest) msg).toBuilder().setUpdatedBy(claim.get().getPerformedBy())
                        .setAccessToken(token).build();
            }
            return msg;

        } else if (method.equals("getAllTenants")) {
            Optional<AuthClaim> claim = null;
            try {
                claim = authorizeUsingUserToken(headers);
            } catch (Exception ex) {
                throw new UnAuthorizedException("Request is not authorized", ex);
            }
            if (claim == null || claim.isEmpty() || !claim.get().isSuperTenant()) {
                throw new UnAuthorizedException("Request is not authorized", null);
            }

            return msg;

        } else if (method.equals("validateTenant")) {
            Optional<AuthClaim> claim = null;
            try {
                claim = authorizeUsingUserToken(headers);
            } catch (Exception ex) {
                LOGGER.error(" Authorizing error " + ex.getMessage());
                throw new UnAuthorizedException("Request is not authorized", ex);
            }
            if (claim == null || claim.isEmpty() || !claim.get().isSuperTenant()) {
                throw new UnAuthorizedException("Request is not authorized", null);
            }

            return msg;

        }

        return msg;
    }