PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/com/atlassian/connect/play/java/remoteapps/ConnectClient.java

https://bitbucket.org/awei/ac-play-java
Java | 62 lines | 53 code | 8 blank | 1 comment | 2 complexity | c709884df6e792a6706d9c5942ed4d87 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.connect.play.java.remoteapps;
  2. import com.atlassian.connect.play.java.util.Environment;
  3. import com.ning.http.client.Realm;
  4. import play.libs.F;
  5. import play.libs.WS;
  6. import static com.atlassian.connect.play.java.util.Utils.LOGGER;
  7. import static com.google.common.base.Preconditions.checkNotNull;
  8. import static java.lang.String.format;
  9. public final class ConnectClient
  10. {
  11. private final String baseUrl;
  12. public ConnectClient(String baseUrl)
  13. {
  14. this.baseUrl = checkNotNull(baseUrl);
  15. }
  16. public F.Promise<Boolean> install(final String uri)
  17. {
  18. // You can specify the username and password of the system administrator in conf/env.properties
  19. final String userName = Environment.getOptionalEnv("adminuser", "admin");
  20. final String password = Environment.getOptionalEnv("adminpass", "admin");
  21. final String postUrl = baseUrl + "/rest/atlassian-connect/latest/installer";
  22. final String parameters = "url=" + uri;
  23. LOGGER.debug(format("Posting to URL '%s', with parameters '%s' using credentials of '%s' user", postUrl, parameters, userName));
  24. return WS.url(postUrl)
  25. .setAuth(userName, password, Realm.AuthScheme.BASIC)
  26. .post(parameters)
  27. .map(new F.Function<WS.Response, Boolean>()
  28. {
  29. public Boolean apply(WS.Response response) throws Throwable
  30. {
  31. int status = response.getStatus();
  32. if (200 <= status && status < 300)
  33. {
  34. LOGGER.info(format("Plugin successfully installed on %s (using the Atlassian Connect REST end point).", baseUrl));
  35. return true;
  36. }
  37. else
  38. {
  39. LOGGER.error(format("Failed to install plugin into '%s': %d - %s", baseUrl, status, response.getStatusText()));
  40. return false;
  41. }
  42. }
  43. })
  44. .recover(new F.Function<Throwable, Boolean>()
  45. {
  46. @Override
  47. public Boolean apply(Throwable throwable) throws Throwable
  48. {
  49. LOGGER.error(format("Unable to install plugin into '%s'", baseUrl), throwable);
  50. return false;
  51. }
  52. });
  53. }
  54. }