private void validateResourceForAddition()

in server/src/main/java/com/epam/aidial/core/server/service/PublicationService.java [445:504]


    private void validateResourceForAddition(ProxyContext context, Publication.Resource resource, String targetFolder,
                                             String reviewBucket, Set<String> urls) {
        ResourceDescriptor source = ResourceDescriptorFactory.fromPrivateUrl(resource.getSourceUrl(), encryption);
        ResourceDescriptor target = ResourceDescriptorFactory.fromPublicUrl(resource.getTargetUrl());
        verifyResourceType(source);

        String sourceUrl = source.getUrl();
        String targetUrl = target.getUrl();

        if (!accessService.hasReadAccess(source, context)) {
            throw new PermissionDeniedException("You don't have permission to access resource " + sourceUrl);
        }

        if (source.isFolder()) {
            throw new IllegalArgumentException("Source resource is folder: " + sourceUrl);
        }

        if (target.isFolder()) {
            throw new IllegalArgumentException("Target resource is folder: " + targetUrl);
        }

        if (source.getType() != target.getType()) {
            throw new IllegalArgumentException("Source and target resource types do not match: " + targetUrl);
        }

        String targetSuffix = targetUrl.substring(source.getType().group().length() + 1);

        if (!targetSuffix.startsWith(targetFolder)) {
            throw new IllegalArgumentException("Target resource folder does not match with target folder: " + targetUrl);
        } else {
            targetSuffix = targetSuffix.substring(targetFolder.length());
        }

        if (!resourceService.hasResource(source)) {
            throw new IllegalArgumentException("Source resource does not exists: " + sourceUrl);
        }

        if (resource.getAction() == Publication.ResourceAction.ADD && resourceService.hasResource(target)) {
            throw new IllegalArgumentException("Target resource already exists: " + targetUrl);
        }

        String reviewUrl = source.getType().group() + ResourceDescriptor.PATH_SEPARATOR
                + reviewBucket + ResourceDescriptor.PATH_SEPARATOR + targetSuffix;

        if (!urls.add(sourceUrl)) {
            throw new IllegalArgumentException("Source resources have duplicate urls: " + sourceUrl);
        }

        if (!urls.add(targetUrl)) {
            throw new IllegalArgumentException("Target resources have duplicate urls: " + targetUrl);
        }

        if (!urls.add(reviewUrl)) {
            throw new IllegalArgumentException("Review resources have duplicate urls: " + reviewUrl);
        }

        resource.setSourceUrl(sourceUrl);
        resource.setTargetUrl(targetUrl);
        resource.setReviewUrl(reviewUrl);
    }