You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OAuthRestClient.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth;
  21. import com.github.scribejava.core.model.OAuth2AccessToken;
  22. import com.github.scribejava.core.model.OAuthRequest;
  23. import com.github.scribejava.core.model.Response;
  24. import com.github.scribejava.core.model.Verb;
  25. import com.github.scribejava.core.oauth.OAuth20Service;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.concurrent.ExecutionException;
  31. import java.util.function.Function;
  32. import java.util.regex.Matcher;
  33. import java.util.regex.Pattern;
  34. import static java.lang.String.format;
  35. public class OAuthRestClient {
  36. private static final int DEFAULT_PAGE_SIZE = 100;
  37. private static final Pattern NEXT_LINK_PATTERN = Pattern.compile(".*<(.*)>; rel=\"next\"");
  38. private OAuthRestClient() {
  39. // Only static method
  40. }
  41. public static Response executeRequest(String requestUrl, OAuth20Service scribe, OAuth2AccessToken accessToken) throws IOException {
  42. OAuthRequest request = new OAuthRequest(Verb.GET, requestUrl);
  43. scribe.signRequest(accessToken, request);
  44. try {
  45. Response response = scribe.execute(request);
  46. if (!response.isSuccessful()) {
  47. throw unexpectedResponseCode(requestUrl, response);
  48. }
  49. return response;
  50. } catch (InterruptedException e) {
  51. Thread.currentThread().interrupt();
  52. throw new IllegalStateException(e);
  53. } catch (ExecutionException e) {
  54. throw new IllegalStateException(e);
  55. }
  56. }
  57. public static <E> List<E> executePaginatedRequest(String request, OAuth20Service scribe, OAuth2AccessToken accessToken, Function<String, List<E>> function) {
  58. List<E> result = new ArrayList<>();
  59. readPage(result, scribe, accessToken, request + "?per_page=" + DEFAULT_PAGE_SIZE, function);
  60. return result;
  61. }
  62. private static <E> void readPage(List<E> result, OAuth20Service scribe, OAuth2AccessToken accessToken, String endPoint, Function<String, List<E>> function) {
  63. try (Response nextResponse = executeRequest(endPoint, scribe, accessToken)) {
  64. String content = nextResponse.getBody();
  65. if (content == null) {
  66. return;
  67. }
  68. result.addAll(function.apply(content));
  69. readNextEndPoint(nextResponse).ifPresent(newNextEndPoint -> readPage(result, scribe, accessToken, newNextEndPoint, function));
  70. } catch (IOException e) {
  71. throw new IllegalStateException(format("Failed to get %s", endPoint), e);
  72. }
  73. }
  74. private static Optional<String> readNextEndPoint(Response response) {
  75. String link = response.getHeader("Link");
  76. if (link == null || link.isEmpty() || !link.contains("rel=\"next\"")) {
  77. return Optional.empty();
  78. }
  79. Matcher nextLinkMatcher = NEXT_LINK_PATTERN.matcher(link);
  80. if (!nextLinkMatcher.find()) {
  81. return Optional.empty();
  82. }
  83. return Optional.of(nextLinkMatcher.group(1));
  84. }
  85. private static IllegalStateException unexpectedResponseCode(String requestUrl, Response response) throws IOException {
  86. return new IllegalStateException(format("Fail to execute request '%s'. HTTP code: %s, response: %s", requestUrl, response.getCode(), response.getBody()));
  87. }
  88. }