PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/spi/DefaultRequestHelper.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 167 lines | 136 code | 25 blank | 6 comment | 17 complexity | 152eebf946e4349912ae315d77054885 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.spi;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import org.apache.commons.httpclient.HttpStatus;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import com.atlassian.jira.plugins.bitbucket.api.Authentication;
  8. import com.atlassian.jira.plugins.bitbucket.api.impl.GithubOAuthAuthentication;
  9. import com.atlassian.jira.plugins.bitbucket.spi.ExtendedResponseHandler.ExtendedResponse;
  10. import com.atlassian.jira.util.json.JSONException;
  11. import com.atlassian.jira.util.json.JSONObject;
  12. import com.atlassian.sal.api.net.Request;
  13. import com.atlassian.sal.api.net.RequestFactory;
  14. import com.atlassian.sal.api.net.ResponseException;
  15. import com.atlassian.sal.api.net.ResponseHandler;
  16. public class DefaultRequestHelper implements RequestHelper
  17. {
  18. private final Logger log = LoggerFactory.getLogger(DefaultRequestHelper.class);
  19. private final RequestFactory<?> requestFactory;
  20. private final ExtendedResponseHandlerFactory responseHandlerFactory;
  21. /**
  22. * For testing only
  23. */
  24. public DefaultRequestHelper(RequestFactory<?> requestFactory, ExtendedResponseHandlerFactory responseHandlerFactory)
  25. {
  26. this.requestFactory = requestFactory;
  27. this.responseHandlerFactory = responseHandlerFactory;
  28. }
  29. public DefaultRequestHelper(RequestFactory<?> requestFactory)
  30. {
  31. this.requestFactory = requestFactory;
  32. this.responseHandlerFactory = new DefaultExtendedResponseHandlerFactory();
  33. }
  34. @Override
  35. public String get(Authentication auth, String urlPath, Map<String, Object> params, String apiBaseUrl) throws ResponseException
  36. {
  37. return runRequest(Request.MethodType.GET, apiBaseUrl, urlPath, auth, params, null);
  38. }
  39. @Override
  40. public ExtendedResponse getExtendedResponse(Authentication auth, String urlPath, Map<String, Object> params, String apiBaseUrl) throws ResponseException
  41. {
  42. ExtendedResponseHandler responseHandler = responseHandlerFactory.create();
  43. runRequest(Request.MethodType.GET, apiBaseUrl, urlPath, auth, params, null, responseHandler);
  44. ExtendedResponse extendedResponse = responseHandler.getExtendedResponse();
  45. log.debug("returned: " + extendedResponse);
  46. return extendedResponse;
  47. }
  48. @Override
  49. public String post(Authentication auth, String urlPath, String postData, String apiBaseUrl) throws ResponseException
  50. {
  51. return runRequest(Request.MethodType.POST, apiBaseUrl, urlPath, auth, null, postData);
  52. }
  53. @Override
  54. public void delete(Authentication auth, String apiUrl, String urlPath) throws ResponseException
  55. {
  56. runRequest(Request.MethodType.DELETE, apiUrl, urlPath, auth, null, null);
  57. }
  58. private String runRequest(Request.MethodType methodType, String apiBaseUrl, String urlPath, Authentication auth,
  59. Map<String, Object> params, String postData) throws ResponseException
  60. {
  61. return runRequest(methodType, apiBaseUrl, urlPath, auth, params, postData, null);
  62. }
  63. @SuppressWarnings({ "unchecked", "rawtypes" })
  64. private String runRequest(Request.MethodType methodType, String apiBaseUrl, String urlPath, Authentication auth,
  65. Map<String, Object> params, String postData, ResponseHandler responseHandler) throws ResponseException
  66. {
  67. String url = apiBaseUrl + urlPath + buildQueryString(params);
  68. log.debug("get [ " + url + " ]");
  69. if (auth instanceof GithubOAuthAuthentication)
  70. {
  71. String separator = (params == null || params.isEmpty()) ? "?" : "&";
  72. url += separator + "access_token=" + ((GithubOAuthAuthentication) auth).getAccessToken();
  73. }
  74. Request<?, ?> request = requestFactory.createRequest(methodType, url);
  75. if (auth != null) auth.addAuthentication(request, url);
  76. if (postData != null) request.setRequestBody(postData);
  77. request.setSoTimeout(60000);
  78. if (responseHandler!=null)
  79. {
  80. request.execute(responseHandler);
  81. return null;
  82. } else
  83. {
  84. String response = request.execute();
  85. log.debug("returned: " + response);
  86. return response;
  87. }
  88. }
  89. private String buildQueryString(Map<String, Object> params)
  90. {
  91. StringBuilder queryStringBuilder = new StringBuilder();
  92. if (params != null && !params.isEmpty())
  93. {
  94. queryStringBuilder.append("?");
  95. for (Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator(); iterator.hasNext();)
  96. {
  97. Map.Entry<String, Object> entry = iterator.next();
  98. queryStringBuilder.append(CustomStringUtils.encode(entry.getKey()));
  99. queryStringBuilder.append("=");
  100. queryStringBuilder.append(CustomStringUtils.encode(String.valueOf(entry.getValue())));
  101. if (iterator.hasNext()) queryStringBuilder.append("&");
  102. }
  103. }
  104. return queryStringBuilder.toString();
  105. }
  106. @Override
  107. public Boolean isRepositoryPrivate1(final RepositoryUri repositoryUri)
  108. {
  109. ExtendedResponse extendedResponse = null;
  110. try
  111. {
  112. extendedResponse = getExtendedResponse(Authentication.ANONYMOUS, repositoryUri.getRepositoryInfoUrl(), null, repositoryUri.getApiUrl());
  113. } catch (ResponseException e)
  114. {
  115. log.warn(e.getMessage());
  116. }
  117. if (extendedResponse==null)
  118. {
  119. log.warn("Unable to retrieve repository info for: " +repositoryUri.getRepositoryUrl());
  120. return null;
  121. }
  122. if (extendedResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED)
  123. {
  124. // this looks like a private repository
  125. return true;
  126. }
  127. if (extendedResponse.isSuccessful())
  128. {
  129. try
  130. {
  131. // this looks like public repo. Lets check if response looks parseable
  132. new JSONObject(extendedResponse.getResponseString());
  133. // everything looks fine, this repository is not public
  134. return false;
  135. } catch (JSONException e)
  136. {
  137. log.debug(e.getMessage());
  138. }
  139. }
  140. return null;
  141. }
  142. }