PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/com/atlassian/connect/play/java/controllers/AcAdmin.java

https://bitbucket.org/awei/ac-play-java
Java | 86 lines | 80 code | 6 blank | 0 comment | 2 complexity | 7d8824e5320f94dec5cf2748f93d7f94 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.connect.play.java.controllers;
  2. import com.atlassian.connect.play.java.AC;
  3. import com.atlassian.connect.play.java.AcHost;
  4. import com.google.common.base.Function;
  5. import com.google.common.base.Supplier;
  6. import com.atlassian.connect.play.java.model.AcHostModel;
  7. import org.codehaus.jackson.JsonNode;
  8. import org.codehaus.jackson.node.ObjectNode;
  9. import play.db.jpa.Transactional;
  10. import play.libs.F;
  11. import play.libs.Json;
  12. import play.libs.WS;
  13. import play.mvc.Http;
  14. import play.mvc.Result;
  15. import play.mvc.With;
  16. import static java.lang.String.format;
  17. import static play.mvc.Results.async;
  18. import static play.mvc.Results.badRequest;
  19. import static play.mvc.Results.ok;
  20. @With(IsDevAction.class)
  21. public class AcAdmin
  22. {
  23. @Transactional(readOnly = true)
  24. public static Result index()
  25. {
  26. return ok(views.html.ac.internal.admin.index.render(AcHostModel.all()));
  27. }
  28. @Transactional(readOnly = true)
  29. public static Result clearMacroCache(final String key)
  30. {
  31. return async(AC.getAcHost(key).fold(
  32. noAcHost(),
  33. new Function<AcHost, F.Promise<Result>>()
  34. {
  35. @Override
  36. public F.Promise<Result> apply(final AcHost host)
  37. {
  38. return AC.url("/rest/atlassian-connect/1/macro/app/" + AC.PLUGIN_KEY, host).delete().map(new F.Function<WS.Response, Result>()
  39. {
  40. @Override
  41. public Result apply(WS.Response response) throws Throwable
  42. {
  43. if (response.getStatus() == Http.Status.NO_CONTENT)
  44. {
  45. return ok(message("Cache cleared",
  46. format("The macro cache for host at '%s' was cleared.", host.getBaseUrl())));
  47. }
  48. else
  49. {
  50. return badRequest(message("Unknown error",
  51. format("An unknown error happened clearing the cache for host at '%s'. Http status is %s (%s).",
  52. host.getBaseUrl(), response.getStatus(), response.getStatusText())));
  53. }
  54. }
  55. });
  56. }
  57. }));
  58. }
  59. private static Supplier<F.Promise<Result>> noAcHost()
  60. {
  61. return new Supplier<F.Promise<Result>>()
  62. {
  63. @Override
  64. public F.Promise<Result> get()
  65. {
  66. return F.Promise.<Result>pure(badRequest(message(
  67. "No AC host",
  68. "Couldn't find AC host to send request to."
  69. )));
  70. }
  71. };
  72. }
  73. public static JsonNode message(String title, String message)
  74. {
  75. final ObjectNode result = Json.newObject();
  76. result.put("title", title);
  77. result.put("message", message);
  78. return result;
  79. }
  80. }