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