Search in sources :

Example 36 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method execute.

private String execute(final HttpRequestBase request) throws IOException {
    CisClientException last = null;
    long remainingAttempts = numberOfReauthTries;
    do {
        try {
            return client.execute(request);
        } catch (CisClientException e) {
            if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED || e.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
                last = e;
                remainingAttempts--;
                LOGGER.warn("Unauthorized response from CIS, retrying request ({} attempt(s) left).", remainingAttempts);
                login();
                // Update the corresponding query parameter in the request, then retry
                try {
                    URI requestUri = request.getURI();
                    URI updatedUri = new URIBuilder(requestUri).setParameter("token", getToken()).build();
                    request.setURI(updatedUri);
                    request.reset();
                } catch (URISyntaxException se) {
                    throw new IOException("Couldn't update authentication token.", se);
                }
            } else {
                throw new BadRequestException(e.getReasonPhrase(), e);
            }
        }
    } while (remainingAttempts > 0);
    // Re-throw the last captured exception otherwise
    throw new BadRequestException(last.getReasonPhrase());
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 37 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class CodeSystemUpgradeRequest method execute.

@Override
public String execute(RepositoryContext context) {
    // get available upgrades
    final CodeSystem currentCodeSystem = CodeSystemRequests.prepareGetCodeSystem(resource.getResourceId()).setExpand(CodeSystem.Expand.AVAILABLE_UPGRADES + "()").build().execute(context);
    if (currentCodeSystem.getUpgradeOf() != null) {
        throw new BadRequestException("Upgrade can not be started on an existing upgrade resource");
    }
    final List<ResourceURI> availableUpgrades = currentCodeSystem.getAvailableUpgrades();
    // report bad request if there are no upgrades available
    if (availableUpgrades.isEmpty()) {
        throw new BadRequestException("There are no upgrades available for resource '%s'.", resource.getResourceId());
    }
    // or the selected extensionOf version is not present as a valid available upgrade
    if (!availableUpgrades.contains(extensionOf)) {
        throw new BadRequestException("Upgrades can only be performed to the next available version dependency.").withDeveloperMessage("Use '%s/<VERSION_ID>', where <VERSION_ID> is one of: '%s'", extensionOf.getResourceId(), availableUpgrades);
    }
    // auto-generate the resourceId if not provided
    // auto-generated upgrade IDs consist of the original Resource's ID and the new extensionOf dependency's path, which is version at this point
    final String upgradeResourceId;
    if (resourceId == null) {
        upgradeResourceId = String.format("%s-%s-UPGRADE", resource.getResourceId(), extensionOf.getPath());
    } else if (resourceId.isBlank()) {
        throw new BadRequestException("'resourceId' property should not be empty, if provided");
    } else {
        BranchNameValidator.DEFAULT.checkName(resourceId);
        upgradeResourceId = resourceId;
    }
    String mergeContentFromBranchPath = currentCodeSystem.getBranchPath();
    // only allow HEAD or valid code system versions
    if (!resource.isHead()) {
        mergeContentFromBranchPath = new DefaultResourceURIPathResolver(false).resolve(context, List.of(resource)).stream().findFirst().get();
    }
    // create the same branch name under the new extensionOf path
    String parentBranch = context.service(ResourceURIPathResolver.class).resolve(context, List.of(extensionOf)).stream().findFirst().get();
    // merge content in the tooling repository from the current resource's to the upgrade resource's branch
    final String upgradeBranch = RepositoryRequests.branching().prepareCreate().setParent(parentBranch).setName(resource.getResourceId()).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
    try {
        // merge branch content from the current code system to the new upgradeBranch
        Merge merge = RepositoryRequests.merging().prepareCreate().setSource(mergeContentFromBranchPath).setTarget(upgradeBranch).setSquash(false).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
        if (merge.getStatus() != Merge.Status.COMPLETED) {
            // report conflicts
            ApiError apiError = merge.getApiError();
            Collection<MergeConflict> conflicts = merge.getConflicts();
            context.log().error("Failed to sync source CodeSystem content to upgrade CodeSystem. Error: {}. Conflicts: {}", apiError.getMessage(), conflicts);
            throw new ConflictException("Upgrade can not be performed due to content synchronization errors.").withAdditionalInfo(Map.of("conflicts", conflicts, "mergeError", apiError.getMessage()));
        }
        // and lastly create the actual CodeSystem so users will be able to browse, access and complete the upgrade
        return CodeSystemRequests.prepareNewCodeSystem().setId(upgradeResourceId).setBranchPath(upgradeBranch).setTitle(String.format("Upgrade of '%s' to '%s'", currentCodeSystem.getTitle(), extensionOf)).setUrl(currentCodeSystem.getUrl() + "?upgrade=" + upgradeResourceId).setLanguage(currentCodeSystem.getLanguage()).setDescription(currentCodeSystem.getDescription()).setStatus("draft").setCopyright(currentCodeSystem.getCopyright()).setOwner(currentCodeSystem.getOwner()).setContact(currentCodeSystem.getContact()).setUsage(currentCodeSystem.getUsage()).setPurpose(currentCodeSystem.getPurpose()).setToolingId(currentCodeSystem.getToolingId()).setExtensionOf(extensionOf).setUpgradeOf(resource).setSettings(currentCodeSystem.getSettings()).commit().setCommitComment(String.format("Start upgrade of '%s' to '%s'", resource, extensionOf)).build().execute(context.openBranch(context, Branch.MAIN_PATH)).getResultAs(String.class);
    } catch (Throwable e) {
        // delete upgrade branch if any exception have been thrown during the upgrade
        RepositoryRequests.branching().prepareDelete(upgradeBranch).build(currentCodeSystem.getToolingId()).getRequest().execute(context);
        throw e;
    }
}
Also used : MergeConflict(com.b2international.snowowl.core.merge.MergeConflict) ResourceURI(com.b2international.snowowl.core.ResourceURI) DefaultResourceURIPathResolver(com.b2international.snowowl.core.uri.DefaultResourceURIPathResolver) Merge(com.b2international.snowowl.core.merge.Merge) ConflictException(com.b2international.commons.exceptions.ConflictException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ApiError(com.b2international.commons.exceptions.ApiError)

Example 38 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class VersionCreateRequest method execute.

@Override
public Boolean execute(RepositoryContext context) {
    final String user = context.service(User.class).getUsername();
    if (!resource.isHead()) {
        throw new BadRequestException("Version '%s' cannot be created on unassigned branch '%s'.", version, resource).withDeveloperMessage("Did you mean to version '%s'?", resource.withoutPath());
    }
    if (resourcesById == null) {
        resourcesById = fetchResources(context);
    }
    if (!resourcesById.containsKey(resource)) {
        context.log().warn("Resource cannot be found during versioning: " + resourcesById + ", uri: " + resource);
        throw new NotFoundException("Resource", resource.getResourceId());
    }
    // validate new path
    RevisionBranch.BranchNameValidator.DEFAULT.checkName(version);
    TerminologyResource resourceToVersion = resourcesById.get(resource);
    // TODO resurrect or eliminate tooling dependencies
    final List<TerminologyResource> resourcesToVersion = List.of(resourcesById.get(resource));
    // final List<CodeSystem> resourcesToVersion = codeSystem.getDependenciesAndSelf()
    // .stream()
    // .map(resourcesById::get)
    // .collect(Collectors.toList());
    resourcesToVersion.stream().filter(cs -> cs.getUpgradeOf() != null).findAny().ifPresent(cs -> {
        throw new BadRequestException("Upgrade resource '%s' can not be versioned.", cs.getResourceURI());
    });
    for (TerminologyResource terminologyResource : resourcesToVersion) {
        // check that the new versionId does not conflict with any other currently available branch
        final String newVersionPath = String.join(Branch.SEPARATOR, terminologyResource.getBranchPath(), version);
        final String repositoryId = terminologyResource.getToolingId();
        if (!force) {
            // branch needs checking in the non-force cases only
            try {
                Branch branch = RepositoryRequests.branching().prepareGet(newVersionPath).build(repositoryId).execute(context);
                if (!branch.isDeleted()) {
                    throw new ConflictException("An existing version or branch with path '%s' conflicts with the specified version identifier.", newVersionPath);
                }
            } catch (NotFoundException e) {
            // branch does not exist, ignore
            }
        } else {
            // if there is no conflict, delete the branch (the request also ignores non-existent branches)
            deleteBranch(context, newVersionPath, repositoryId);
        }
    }
    acquireLocks(context, user, resourcesToVersion);
    final IProgressMonitor monitor = SubMonitor.convert(context.service(IProgressMonitor.class), TASK_WORK_STEP);
    try {
        // resourcesToVersion.forEach(resourceToVersion -> {
        // check that the specified effective time is valid in this code system
        validateVersion(context, resourceToVersion);
        // version components in the given repository
        new RepositoryRequest<>(resourceToVersion.getToolingId(), new BranchRequest<>(resourceToVersion.getBranchPath(), new RevisionIndexReadRequest<CommitResult>(context.service(RepositoryManager.class).get(resourceToVersion.getToolingId()).service(VersioningRequestBuilder.class).build(new VersioningConfiguration(user, resourceToVersion.getResourceURI(), version, description, effectiveTime, force))))).execute(context);
        // tag the repository
        doTag(context, resourceToVersion, monitor);
        // create a version for the resource
        return new BranchRequest<>(Branch.MAIN_PATH, new ResourceRepositoryCommitRequestBuilder().setBody(tx -> {
            tx.add(VersionDocument.builder().id(resource.withPath(version).withoutResourceType()).version(version).description(description).effectiveTime(EffectiveTimes.getEffectiveTime(effectiveTime)).resource(resource).branchPath(resourceToVersion.getRelativeBranchPath(version)).author(user).createdAt(Instant.now().toEpochMilli()).updatedAt(Instant.now().toEpochMilli()).toolingId(resourceToVersion.getToolingId()).url(buildVersionUrl(context, resourceToVersion)).build());
            return Boolean.TRUE;
        }).setCommitComment(CompareUtils.isEmpty(commitComment) ? String.format("Version '%s' as of '%s'", resource, version) : commitComment).build()).execute(context).getResultAs(Boolean.class);
    } finally {
        releaseLocks(context);
        if (null != monitor) {
            monitor.done();
        }
    }
}
Also used : EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) SubMonitor(org.eclipse.core.runtime.SubMonitor) RevisionBranch(com.b2international.index.revision.RevisionBranch) Multimap(com.google.common.collect.Multimap) Branch(com.b2international.snowowl.core.branch.Branch) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) CREATE_VERSION(com.b2international.snowowl.core.internal.locks.DatastoreLockContextDescriptions.CREATE_VERSION) HashMultimap(com.google.common.collect.HashMultimap) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) Permission(com.b2international.snowowl.core.identity.Permission) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) CompareUtils(com.b2international.commons.CompareUtils) com.b2international.snowowl.core(com.b2international.snowowl.core) NotFoundException(com.b2international.commons.exceptions.NotFoundException) com.b2international.snowowl.core.request(com.b2international.snowowl.core.request) Version(com.b2international.snowowl.core.version.Version) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ConflictException(com.b2international.commons.exceptions.ConflictException) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Request(com.b2international.snowowl.core.events.Request) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) Instant(java.time.Instant) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) NotEmpty(org.hibernate.validator.constraints.NotEmpty) LocalDate(java.time.LocalDate) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) Entry(java.util.Map.Entry) IOperationLockManager(com.b2international.snowowl.core.locks.IOperationLockManager) ResourceURLSchemaSupport(com.b2international.snowowl.core.uri.ResourceURLSchemaSupport) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder) User(com.b2international.snowowl.core.identity.User) User(com.b2international.snowowl.core.identity.User) ConflictException(com.b2international.commons.exceptions.ConflictException) NotFoundException(com.b2international.commons.exceptions.NotFoundException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RevisionBranch(com.b2international.index.revision.RevisionBranch) Branch(com.b2international.snowowl.core.branch.Branch) BadRequestException(com.b2international.commons.exceptions.BadRequestException) ResourceRepositoryCommitRequestBuilder(com.b2international.snowowl.core.context.ResourceRepositoryCommitRequestBuilder)

Example 39 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class VersionCreateRequest method validateVersion.

private void validateVersion(RepositoryContext context, TerminologyResource codeSystem) {
    if (!context.service(TerminologyRegistry.class).getTerminology(codeSystem.getToolingId()).isEffectiveTimeSupported()) {
        return;
    }
    Optional<Version> mostRecentVersion = getMostRecentVersion(context, codeSystem);
    mostRecentVersion.ifPresent(mrv -> {
        LocalDate mostRecentVersionEffectiveTime = mostRecentVersion.map(Version::getEffectiveTime).orElse(LocalDate.EPOCH);
        if (force) {
            if (!Objects.equals(version, mrv.getVersion())) {
                throw new BadRequestException("Force creating version requires the same versionId ('%s') to be used", version);
            }
            // force recreating an existing version should use the same or later effective date value, allow same here
            if (effectiveTime.equals(mostRecentVersionEffectiveTime)) {
                return;
            }
        }
        if (!effectiveTime.isAfter(mostRecentVersionEffectiveTime)) {
            throw new BadRequestException("The specified '%s' effective time is invalid. Date should be after '%s'.", effectiveTime, mostRecentVersionEffectiveTime);
        }
    });
}
Also used : Version(com.b2international.snowowl.core.version.Version) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) BadRequestException(com.b2international.commons.exceptions.BadRequestException) LocalDate(java.time.LocalDate)

Example 40 with BadRequestException

use of com.b2international.commons.exceptions.BadRequestException in project snow-owl by b2ihealthcare.

the class BaseTerminologyResourceUpdateRequest method updateBranchPath.

private boolean updateBranchPath(final TransactionContext context, final ResourceDocument.Builder resource, final String currentBranchPath, final String toolingId) {
    // if extensionOf is set, branch path changes are already handled in updateExtensionOf
    if (extensionOf == null && branchPath != null && !currentBranchPath.equals(branchPath)) {
        try {
            final Branch branch = RepositoryRequests.branching().prepareGet(branchPath).build(toolingId).getRequest().execute(context);
            if (branch.isDeleted()) {
                throw new BadRequestException("Branch with identifier '%s' is deleted.", branchPath);
            }
        } catch (NotFoundException e) {
            throw e.toBadRequestException();
        }
        // TODO: check if update branch path coincides with a version working path
        // and update extensionOf accordingly?
        resource.extensionOf(null);
        resource.branchPath(branchPath);
        return true;
    }
    return false;
}
Also used : Branch(com.b2international.snowowl.core.branch.Branch) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NotFoundException(com.b2international.commons.exceptions.NotFoundException)

Aggregations

BadRequestException (com.b2international.commons.exceptions.BadRequestException)41 IOException (java.io.IOException)8 Collectors (java.util.stream.Collectors)7 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 BranchContext (com.b2international.snowowl.core.domain.BranchContext)6 Request (com.b2international.snowowl.core.events.Request)6 List (java.util.List)6 Map (java.util.Map)6 Options (com.b2international.commons.options.Options)5 Branch (com.b2international.snowowl.core.branch.Branch)5 Set (java.util.Set)5 CompareUtils (com.b2international.commons.CompareUtils)4 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)4 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)4 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Collection (java.util.Collection)4 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)3 ConflictException (com.b2international.commons.exceptions.ConflictException)3 NotFoundException (com.b2international.commons.exceptions.NotFoundException)3 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)3