|
1 | 1 | package uk.gov.hmcts.ccd.data.user;
|
2 | 2 |
|
| 3 | +import java.net.URI; |
| 4 | +import java.net.URISyntaxException; |
3 | 5 | import java.util.HashMap;
|
4 | 6 | import java.util.List;
|
5 | 7 | import java.util.Map;
|
6 | 8 | import java.util.Objects;
|
| 9 | +import java.util.Optional; |
7 | 10 | import java.util.Set;
|
8 | 11 | import java.util.stream.Collectors;
|
9 | 12 |
|
|
21 | 24 | import org.springframework.security.core.GrantedAuthority;
|
22 | 25 | import org.springframework.security.core.context.SecurityContextHolder;
|
23 | 26 | import org.springframework.stereotype.Repository;
|
24 |
| -import org.springframework.web.client.HttpStatusCodeException; |
25 | 27 | import org.springframework.web.client.RestClientException;
|
| 28 | +import org.springframework.web.client.RestClientResponseException; |
26 | 29 | import org.springframework.web.client.RestTemplate;
|
| 30 | +import org.springframework.web.util.UriComponentsBuilder; |
27 | 31 | import uk.gov.hmcts.ccd.ApplicationParams;
|
28 | 32 | import uk.gov.hmcts.ccd.AuthCheckerConfiguration;
|
29 | 33 | import uk.gov.hmcts.ccd.data.SecurityUtils;
|
@@ -111,17 +115,22 @@ public UserDefault getUserDefaultSettings(final String userId) {
|
111 | 115 | LOG.debug("retrieving default user settings for user {}", userId);
|
112 | 116 | final HttpEntity requestEntity = new HttpEntity(securityUtils.authorizationHeaders());
|
113 | 117 | final Map<String, String> queryParams = new HashMap<>();
|
114 |
| - queryParams.put("uid", userId); |
115 |
| - return restTemplate.exchange(applicationParams.userDefaultSettingsURL(), |
116 |
| - HttpMethod.GET, requestEntity, UserDefault.class, queryParams).getBody(); |
117 |
| - } catch (HttpStatusCodeException e) { |
| 118 | + queryParams.put("uid", ApplicationParams.encode(userId.toLowerCase())); |
| 119 | + final String encodedUrl = UriComponentsBuilder.fromHttpUrl(applicationParams.userDefaultSettingsURL()) |
| 120 | + .buildAndExpand(queryParams).toUriString(); |
| 121 | + return restTemplate.exchange(new URI(encodedUrl), HttpMethod.GET, requestEntity, UserDefault.class) |
| 122 | + .getBody(); |
| 123 | + } catch (RestClientResponseException e) { |
118 | 124 | LOG.error("Failed to retrieve user profile", e);
|
119 |
| - final List<String> headerMessages = e.getResponseHeaders().get("Message"); |
| 125 | + final List<String> headerMessages = Optional.ofNullable(e.getResponseHeaders()) |
| 126 | + .map(headers -> headers.get("Message")).orElse(null); |
120 | 127 | final String message = headerMessages != null ? headerMessages.get(0) : e.getMessage();
|
121 | 128 | if (message != null) {
|
122 | 129 | throw new BadRequestException(message);
|
123 | 130 | }
|
124 | 131 | throw new ServiceException("Problem getting user default settings for " + userId);
|
| 132 | + } catch (URISyntaxException e) { |
| 133 | + throw new BadRequestException(e.getMessage()); |
125 | 134 | }
|
126 | 135 | }
|
127 | 136 |
|
|
0 commit comments