Search in sources :

Example 31 with SnowowlRuntimeException

use of com.b2international.snowowl.core.api.SnowowlRuntimeException in project snow-owl by b2ihealthcare.

the class CisClient method execute.

public String execute(final HttpRequestBase request) {
    try {
        final HttpResponse response = client.execute(request);
        checkResponseStatus(response);
        final HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    } catch (IOException e) {
        LOGGER.error("Exception while executing HTTP request.", e);
        throw new SnowowlRuntimeException("Exception while executing HTTP request.", e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 32 with SnowowlRuntimeException

use of com.b2international.snowowl.core.api.SnowowlRuntimeException in project snow-owl by b2ihealthcare.

the class CisClient method login.

public void login() {
    final String tokenBeforeLogin = getToken();
    LogUtils.logUserAccess(LOGGER, username, "Logging in to Component Identifier service.");
    HttpPost request = null;
    try {
        final Credentials credentials = new Credentials(username, password);
        request = httpPost("login", credentials);
        final String response = execute(request);
        final JsonNode node = mapper.readValue(response, JsonNode.class);
        String tokenAfterLogin = node.get("token").asText();
        // If this replacement fails, someone changed the token by the time we got here; let them have their ways.
        if (token.compareAndSet(tokenBeforeLogin, tokenAfterLogin)) {
            LOGGER.info("Received token from CIS: {}", tokenAfterLogin);
        }
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Exception while logging in.", e);
    } finally {
        if (null != request)
            release(request);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Credentials(com.b2international.snowowl.core.identity.Credentials) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 33 with SnowowlRuntimeException

use of com.b2international.snowowl.core.api.SnowowlRuntimeException in project snow-owl by b2ihealthcare.

the class CisClient method logout.

public void logout() {
    final String tokenBeforeLogout = token.getAndSet(BAD_TOKEN);
    // If this is already set to BAD_TOKEN, we are no longer logged in
    if (BAD_TOKEN.equals(tokenBeforeLogout)) {
        return;
    }
    LogUtils.logUserAccess(LOGGER, username, "Logging out from Component Identifier service.");
    HttpPost request = null;
    try {
        final JsonNodeFactory factory = JsonNodeFactory.instance;
        final JsonNode node = factory.objectNode().set("token", factory.textNode(tokenBeforeLogout));
        request = httpPost("logout", node);
        client.execute(request);
    } catch (IOException e) {
        throw new SnowowlRuntimeException("Exception while logging out.", e);
    } finally {
        if (null != request)
            release(request);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Example 34 with SnowowlRuntimeException

use of com.b2international.snowowl.core.api.SnowowlRuntimeException in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method release.

@Override
public Map<String, SctId> release(final Set<String> componentIds) {
    LOGGER.debug("Releasing {} component IDs.", componentIds.size());
    final Map<String, SctId> sctIds = getSctIds(componentIds);
    final Map<String, SctId> problemSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.<SctId>not(Predicates.or(SctId::isAssigned, SctId::isReserved, SctId::isAvailable))));
    if (!problemSctIds.isEmpty()) {
        throw new SctIdStatusException("Cannot release %s component IDs because they are not assigned, reserved, or already available.", problemSctIds);
    }
    final Map<String, SctId> assignedOrReservedSctIds = ImmutableMap.copyOf(Maps.filterValues(sctIds, Predicates.or(SctId::isAssigned, SctId::isReserved)));
    // if there is no IDs to release, then just return the current sctIds set as a response
    if (assignedOrReservedSctIds.isEmpty()) {
        return sctIds;
    }
    HttpPut releaseRequest = null;
    String currentNamespace = null;
    try {
        if (assignedOrReservedSctIds.size() > 1) {
            final Multimap<String, String> componentIdsByNamespace = toNamespaceMultimap(assignedOrReservedSctIds.keySet());
            for (final Entry<String, Collection<String>> entry : componentIdsByNamespace.asMap().entrySet()) {
                currentNamespace = entry.getKey();
                for (final Collection<String> bulkIds : Iterables.partition(entry.getValue(), requestBulkLimit)) {
                    LOGGER.debug("Sending bulk release request for namespace {} with size {}.", currentNamespace, bulkIds.size());
                    releaseRequest = httpPut(String.format("sct/bulk/release?token=%s", getToken()), createBulkReleaseData(currentNamespace, bulkIds));
                    execute(releaseRequest);
                }
            }
        } else {
            final String componentId = Iterables.getOnlyElement(assignedOrReservedSctIds.keySet());
            currentNamespace = SnomedIdentifiers.getNamespace(componentId);
            releaseRequest = httpPut(String.format("sct/release?token=%s", getToken()), createReleaseData(componentId));
            execute(releaseRequest);
        }
        return ImmutableMap.copyOf(assignedOrReservedSctIds);
    } catch (IOException e) {
        throw new SnowowlRuntimeException(String.format("Exception while releasing IDs for namespace %s.", currentNamespace), e);
    } finally {
        release(releaseRequest);
    }
}
Also used : Collection(java.util.Collection) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) SctId(com.b2international.snowowl.snomed.cis.domain.SctId)

Example 35 with SnowowlRuntimeException

use of com.b2international.snowowl.core.api.SnowowlRuntimeException in project snow-owl by b2ihealthcare.

the class CisSnomedIdentifierService method joinBulkJobPolling.

private void joinBulkJobPolling(final String jobId, final int quantity, final String token) {
    HttpGet request = null;
    JobStatus status = JobStatus.PENDING;
    try {
        LOGGER.debug("Polling job status with ID {}.", jobId);
        request = httpGet(String.format("bulk/jobs/%s?token=%s", jobId, token));
        for (long pollTry = numberOfPollTries; pollTry > 0; pollTry--) {
            final String response = execute(request);
            final JsonNode node = mapper.readValue(response, JsonNode.class);
            status = JobStatus.get(node.get("status").asInt());
            if (JobStatus.FINISHED == status) {
                break;
            } else if (JobStatus.ERROR == status) {
                throw new SnowowlRuntimeException("Bulk request has ended in error.");
            } else {
                Thread.sleep(timeBetweenPollTries);
            }
        }
    } catch (Exception e) {
        throw new SnowowlRuntimeException("Exception while polling job status.", e);
    } finally {
        release(request);
    }
    if (JobStatus.FINISHED != status) {
        throw new SnowowlRuntimeException("Job didn't finish with expected status: " + status);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) JsonNode(com.fasterxml.jackson.databind.JsonNode) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) URISyntaxException(java.net.URISyntaxException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) IOException(java.io.IOException)

Aggregations

SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)39 IOException (java.io.IOException)27 SctId (com.b2international.snowowl.snomed.cis.domain.SctId)7 Collection (java.util.Collection)7 HttpPost (org.apache.http.client.methods.HttpPost)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)4 Promise (com.b2international.snowowl.core.events.util.Promise)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 File (java.io.File)3 Path (java.nio.file.Path)3 NamingException (javax.naming.NamingException)3 InitialLdapContext (javax.naming.ldap.InitialLdapContext)3 HttpGet (org.apache.http.client.methods.HttpGet)3 Logger (org.slf4j.Logger)3 JwkException (com.auth0.jwk.JwkException)2 JwkProvider (com.auth0.jwk.JwkProvider)2 JwkProviderBuilder (com.auth0.jwk.JwkProviderBuilder)2