Search in sources :

Example 1 with ProviderCommunicationException

use of uk.nhs.digital.intranet.model.exception.ProviderCommunicationException in project hippo by NHS-digital-website.

the class SearchPageComponent method searchPeople.

private void searchPeople(HstRequest request, List<SearchResultTab> searchResultTabs) {
    final String accessToken = (String) RequestContextProvider.get().getAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME);
    final SearchArea area = getAreaOption(request);
    final String searchQuery = getSearchQuery(request, false);
    final int peopleLimit = getComponentInfo(request).getPeopleLimit();
    int numberOfResults;
    if (!StringUtils.hasText(accessToken)) {
        numberOfResults = 0;
        request.setAttribute(REQUEST_ATTR_ACCESS_TOKEN_REQUIRED, true);
    } else if (!StringUtils.hasText(searchQuery)) {
        numberOfResults = 0;
        request.setAttribute(REQUEST_ATTR_MISSING_TERM_ERROR_MESSAGE, true);
    } else {
        try {
            List<Person> people = graphProvider.getPeople(searchQuery);
            int peopleCount = people.size();
            if (SearchArea.ALL.equals(area) && peopleCount > peopleLimit) {
                people = people.subList(0, peopleLimit);
                request.setAttribute(REQUEST_ATTR_MORE_PEOPLE, true);
            }
            numberOfResults = peopleCount;
            request.setAttribute(REQUEST_ATTR_PEOPLE_RESULTS, people);
        } catch (ProviderCommunicationException e) {
            numberOfResults = 0;
            request.setAttribute(REQUEST_ATTR_API_ERROR_MESSAGE, true);
        }
    }
    searchResultTabs.add(new SearchResultTab(SearchArea.PEOPLE, numberOfResults));
}
Also used : ProviderCommunicationException(uk.nhs.digital.intranet.model.exception.ProviderCommunicationException) SearchArea(uk.nhs.digital.intranet.enums.SearchArea) SearchResultTab(uk.nhs.digital.intranet.model.SearchResultTab)

Example 2 with ProviderCommunicationException

use of uk.nhs.digital.intranet.model.exception.ProviderCommunicationException in project hippo by NHS-digital-website.

the class GraphProviderImpl method getPeople.

@Override
public List<Person> getPeople(final String searchTerm) throws ProviderCommunicationException {
    final HttpEntity<String> httpRequest = getHttpRequest(MediaType.APPLICATION_JSON);
    final String cleanSearchTerm = searchTerm.trim();
    final String filter = String.format("startsWith(displayName, '%s')", cleanSearchTerm) + " or " + String.format("startsWith(givenName, '%s')", cleanSearchTerm) + " or " + String.format("startsWith(surname, '%s')", cleanSearchTerm) + " or " + String.format("startsWith(mail, '%s')", cleanSearchTerm);
    final URI uri = UriComponentsBuilder.fromUriString(BASE_URL_V1).pathSegment("users").queryParam("$filter", filter).queryParam("$select", SELECTED_FIELDS_SHORT).build().toUri();
    try {
        final ResponseEntity<UserResponse> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, httpRequest, UserResponse.class);
        Assert.notNull(responseEntity.getBody(), "Received null response from Microsoft Graph API.");
        return personFactory.createPersons(responseEntity.getBody().getValue());
    } catch (final HttpStatusCodeException e) {
        LOGGER.error("Received exception with status {} from Microsoft Graph API.", e.getStatusCode(), e);
        throw new ProviderCommunicationException();
    }
}
Also used : UserResponse(uk.nhs.digital.intranet.json.UserResponse) ProviderCommunicationException(uk.nhs.digital.intranet.model.exception.ProviderCommunicationException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) URI(java.net.URI)

Example 3 with ProviderCommunicationException

use of uk.nhs.digital.intranet.model.exception.ProviderCommunicationException in project hippo by NHS-digital-website.

the class GraphProviderImpl method getUser.

private User getUser(final String id) throws ProviderCommunicationException {
    final HttpEntity<String> httpRequest = getHttpRequest(MediaType.APPLICATION_JSON);
    final URI uri = UriComponentsBuilder.fromUriString(BASE_URL_V1).pathSegment("users").pathSegment(id).queryParam("$select", SELECTED_FIELDS_LONG).build().toUri();
    try {
        final ResponseEntity<User> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, httpRequest, User.class);
        Assert.notNull(responseEntity.getBody(), "Received null response from Microsoft Graph API.");
        return responseEntity.getBody();
    } catch (final HttpStatusCodeException e) {
        LOGGER.error("Received exception with status {} from Microsoft Graph API.", e.getStatusCode(), e);
        throw new ProviderCommunicationException();
    }
}
Also used : ProviderCommunicationException(uk.nhs.digital.intranet.model.exception.ProviderCommunicationException) User(uk.nhs.digital.intranet.json.User) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) URI(java.net.URI)

Example 4 with ProviderCommunicationException

use of uk.nhs.digital.intranet.model.exception.ProviderCommunicationException in project hippo by NHS-digital-website.

the class PersonComponent method doBeforeRender.

@Override
public void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException {
    super.doBeforeRender(request, response);
    final String accessToken = (String) RequestContextProvider.get().getAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME);
    if (!StringUtils.hasText(accessToken)) {
        request.setAttribute("accessTokenRequired", true);
        return;
    }
    final String id = getComponentParameter("id");
    try {
        final Person person = graphProvider.getPerson(id);
        request.setAttribute("person", person);
    } catch (final ProviderCommunicationException e) {
        request.setAttribute("errorMessage", PROVIDER_COMMUNICATION_ERROR_MESSAGE);
    }
}
Also used : ProviderCommunicationException(uk.nhs.digital.intranet.model.exception.ProviderCommunicationException) Person(uk.nhs.digital.intranet.model.Person)

Aggregations

ProviderCommunicationException (uk.nhs.digital.intranet.model.exception.ProviderCommunicationException)4 URI (java.net.URI)2 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)2 SearchArea (uk.nhs.digital.intranet.enums.SearchArea)1 User (uk.nhs.digital.intranet.json.User)1 UserResponse (uk.nhs.digital.intranet.json.UserResponse)1 Person (uk.nhs.digital.intranet.model.Person)1 SearchResultTab (uk.nhs.digital.intranet.model.SearchResultTab)1