Search in sources :

Example 1 with AuthClientException

use of org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskService method readAll.

public List<TaskDto> readAll() throws AuthClientException {
    List<Server> serverList = serverRepository.findAll();
    List<TaskDto> taskDtoList = new ArrayList<>();
    for (Server server : serverList) {
        IGenericClient fhirClient = fhirContext.newRestfulGenericClient(server.getFhirServerUrl());
        // Doesn't support now
        // fhirClient.registerInterceptor(new BearerTokenAuthInterceptor(
        // authorizationClient.getTokenResponse(URI.create(server.getAuthServerUrl()), server.getClientId(),
        // server.getClientSecret(), SCOPE)
        // .getAccessToken()));
        TaskRepository taskRepository = new TaskRepository(fhirClient, applicationUrl);
        taskDtoList.addAll(new TaskBundleToDtoConverter(server.getId()).convert(taskRepository.findAllTasks()));
    }
    return taskDtoList;
}
Also used : Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) TaskRepository(org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository) TaskBundleToDtoConverter(org.hl7.gravity.refimpl.sdohexchange.dto.converter.TaskBundleToDtoConverter) ArrayList(java.util.ArrayList) TaskDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskDto)

Example 2 with AuthClientException

use of org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class AuthorizationClient method getTokenResponse.

/**
 * Extract token endpoint url from the Authorization Server metadata endpoint and return token response after
 * successful authentication.
 *
 * @param authServerUrl Authorization Server Base URL
 * @param clientId      OAuth2 Client ID
 * @param secret        OAuth2 Client Secret
 * @param scope         OAuth2 Client Scope
 * @return {@link TokenResponse} entity
 * @throws AuthClientException will be thrown if token retrieval fails
 */
public TokenResponse getTokenResponse(URI authServerUrl, String clientId, String secret, String scope) throws AuthClientException {
    HttpEntity<MultiValueMap<String, String>> entity = createRequestEntity(clientId, secret, scope);
    String tokenEndpoint = getTokenEndpoint(authServerUrl);
    try {
        return restTemplate.exchange(tokenEndpoint, HttpMethod.POST, entity, TokenResponse.class).getBody();
    } catch (RestClientException e) {
        throw new AuthClientException(e.getMessage(), e);
    }
}
Also used : AuthClientException(org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException) RestClientException(org.springframework.web.client.RestClientException) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 3 with AuthClientException

use of org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class ServerService method createServer.

@Transactional
public ServerDto createServer(NewServerDto newServerDto) throws AuthClientException {
    if (serverRepository.findByServerName(newServerDto.getServerName()).isPresent()) {
        throw new DuplicateServerNameNotAllowedException(newServerDto.getServerName());
    }
    Server server = modelMapper.map(newServerDto, Server.class);
    // Just a validation of credentials.
    authorizationClient.getTokenResponse(URI.create(newServerDto.getAuthServerUrl()), newServerDto.getClientId(), newServerDto.getClientSecret(), SCOPE);
    serverRepository.save(server);
    return modelMapper.map(server, ServerDto.class);
}
Also used : Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) DuplicateServerNameNotAllowedException(org.hl7.gravity.refimpl.sdohexchange.exception.DuplicateServerNameNotAllowedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with AuthClientException

use of org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class ServerService method updateServer.

@Transactional
public ServerDto updateServer(Integer id, NewServerDto newServerDto) throws AuthClientException {
    Server server = serverRepository.findById(id).orElseThrow(() -> new ServerNotFoundException(String.format("No Server was found by id '%s'", id)));
    if (serverRepository.findByServerName(newServerDto.getServerName()).isPresent() && !server.getServerName().equals(newServerDto.getServerName())) {
        throw new DuplicateServerNameNotAllowedException(newServerDto.getServerName());
    }
    // Just a validation of credentials.
    authorizationClient.getTokenResponse(URI.create(newServerDto.getAuthServerUrl()), newServerDto.getClientId(), newServerDto.getClientSecret(), SCOPE);
    modelMapper.map(newServerDto, server);
    return modelMapper.map(server, ServerDto.class);
}
Also used : Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) DuplicateServerNameNotAllowedException(org.hl7.gravity.refimpl.sdohexchange.exception.DuplicateServerNameNotAllowedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with AuthClientException

use of org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException in project Gravity-SDOH-Exchange-RI by FHIR.

the class TaskService method update.

public void update(String id, UpdateTaskRequestDto update) throws AuthClientException {
    Server server = serverRepository.findById(update.getServerId()).orElseThrow(() -> new ServerNotFoundException(String.format("No server was found by id '%s'", update.getServerId())));
    IGenericClient fhirClient = fhirContext.newRestfulGenericClient(server.getFhirServerUrl());
    // Doesn't support now
    // fhirClient.registerInterceptor(new BearerTokenAuthInterceptor(
    // authorizationClient.getTokenResponse(URI.create(server.getAuthServerUrl()), server.getClientId(),
    // server.getClientSecret(), SCOPE)
    // .getAccessToken()));
    TaskRepository taskRepository = new TaskRepository(fhirClient, applicationUrl);
    // Validates and converts Procedure codes to Coding
    List<Coding> procedureCodes = Optional.ofNullable(update.getProcedureCodes()).orElse(Collections.emptyList()).stream().map(code -> sdohMappings.findResourceCoding(Procedure.class, code)).collect(Collectors.toList());
    Bundle taskBundle = taskRepository.find(id, Lists.newArrayList(Task.INCLUDE_FOCUS));
    TaskInfoBundleExtractor.TaskInfoHolder taskInfo = new TaskInfoBundleExtractor().extract(taskBundle).stream().findFirst().orElseThrow(() -> new ResourceNotFoundException(new IdType(Task.class.getSimpleName(), id)));
    TaskUpdateBundleFactory bundleFactory = new TaskUpdateBundleFactory();
    bundleFactory.setTask(taskInfo.getTask());
    bundleFactory.setStatus(update.getTaskStatus());
    bundleFactory.setServiceRequest(taskInfo.getServiceRequest());
    bundleFactory.setStatusReason(update.getStatusReason());
    bundleFactory.setComment(update.getComment());
    bundleFactory.setOutcome(update.getOutcome());
    bundleFactory.setProcedureCodes(procedureCodes);
    taskRepository.transaction(bundleFactory.createUpdateBundle());
}
Also used : TaskDto(org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskDto) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) AuthorizationClient(org.hl7.gravity.refimpl.sdohexchange.auth.AuthorizationClient) TaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskUpdateBundleFactory) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Procedure(org.hl7.fhir.r4.model.Procedure) Task(org.hl7.fhir.r4.model.Task) FhirContext(ca.uhn.fhir.context.FhirContext) UpdateTaskRequestDto(org.hl7.gravity.refimpl.sdohexchange.dto.request.UpdateTaskRequestDto) Lists(com.google.common.collect.Lists) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) Service(org.springframework.stereotype.Service) TaskRepository(org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository) TaskReadException(org.hl7.gravity.refimpl.sdohexchange.exception.TaskReadException) Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) RestTemplate(org.springframework.web.client.RestTemplate) AuthClientException(org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException) TaskBundleToDtoConverter(org.hl7.gravity.refimpl.sdohexchange.dto.converter.TaskBundleToDtoConverter) ServerRepository(org.hl7.gravity.refimpl.sdohexchange.dao.ServerRepository) Collectors(java.util.stream.Collectors) IdType(org.hl7.fhir.r4.model.IdType) Objects(java.util.Objects) List(java.util.List) Coding(org.hl7.fhir.r4.model.Coding) Optional(java.util.Optional) Bundle(org.hl7.fhir.r4.model.Bundle) Collections(java.util.Collections) SDOHMappings(org.hl7.gravity.refimpl.sdohexchange.sdohmappings.SDOHMappings) FhirUtil(org.hl7.gravity.refimpl.sdohexchange.util.FhirUtil) Task(org.hl7.fhir.r4.model.Task) Server(org.hl7.gravity.refimpl.sdohexchange.model.Server) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Bundle(org.hl7.fhir.r4.model.Bundle) TaskRepository(org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository) IdType(org.hl7.fhir.r4.model.IdType) Coding(org.hl7.fhir.r4.model.Coding) ServerNotFoundException(org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException) TaskInfoBundleExtractor(org.hl7.gravity.refimpl.sdohexchange.fhir.extract.TaskInfoBundleExtractor) ResourceNotFoundException(ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException) TaskUpdateBundleFactory(org.hl7.gravity.refimpl.sdohexchange.fhir.factory.TaskUpdateBundleFactory)

Aggregations

Server (org.hl7.gravity.refimpl.sdohexchange.model.Server)4 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)2 ArrayList (java.util.ArrayList)2 TaskRepository (org.hl7.gravity.refimpl.sdohexchange.dao.TaskRepository)2 TaskBundleToDtoConverter (org.hl7.gravity.refimpl.sdohexchange.dto.converter.TaskBundleToDtoConverter)2 TaskDto (org.hl7.gravity.refimpl.sdohexchange.dto.response.TaskDto)2 AuthClientException (org.hl7.gravity.refimpl.sdohexchange.exception.AuthClientException)2 DuplicateServerNameNotAllowedException (org.hl7.gravity.refimpl.sdohexchange.exception.DuplicateServerNameNotAllowedException)2 ServerNotFoundException (org.hl7.gravity.refimpl.sdohexchange.exception.ServerNotFoundException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 ResourceNotFoundException (ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException)1 Lists (com.google.common.collect.Lists)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Bundle (org.hl7.fhir.r4.model.Bundle)1 Coding (org.hl7.fhir.r4.model.Coding)1