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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations