@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(Amazon.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
/** * Get user name from Amazon, with the token provided. * @param token Amazon access token * @return The user found in Amazon * @throws java.io.IOException If fails */ private Identity fetch(final String token) throws IOException { final URI uri = UriBuilder .fromUri("https://api.amazon.com/user/profile") .queryParam("access_token", "{token}") .build(token); return this.parse( new JdkRequest(uri) .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json().readObject() ); }