PageRenderTime 79ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/app/com/atlassian/connect/play/java/play/CacheControlAction.java

https://bitbucket.org/awei/ac-play-java
Java | 56 lines | 47 code | 9 blank | 0 comment | 4 complexity | ac10ac06f9d51ee95a7ac82f489d881c MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.connect.play.java.play;
  2. import com.atlassian.connect.play.java.AC;
  3. import com.google.common.base.Function;
  4. import play.Play;
  5. import play.mvc.Action;
  6. import play.mvc.Http;
  7. import play.mvc.Result;
  8. import javax.annotation.Nullable;
  9. import static com.atlassian.fugue.Option.option;
  10. import static play.mvc.Http.HeaderNames.CACHE_CONTROL;
  11. public final class CacheControlAction extends Action<WithCacheControl>
  12. {
  13. private static final String DEFAULT_CACHE_CONTROL = Play.application().configuration().getString("ac.cache-control", AC.isDev() ? "no-cache" : null);
  14. public Result call(Http.Context ctx) throws Throwable
  15. {
  16. if (!ctx.args.containsKey(CACHE_CONTROL))
  17. {
  18. ctx.args.put(CACHE_CONTROL, getCacheControl());
  19. }
  20. final Result result = delegate.call(ctx);
  21. final String cacheControl = (String) ctx.args.get(CACHE_CONTROL);
  22. final Http.Response response = ctx.response();
  23. if (cacheControl != null && !responseCacheControlIsSet(response))
  24. {
  25. response.setHeader(CACHE_CONTROL, cacheControl);
  26. }
  27. return result;
  28. }
  29. public String getCacheControl()
  30. {
  31. return option(configuration).map(
  32. new Function<WithCacheControl, String>()
  33. {
  34. @Nullable
  35. @Override
  36. public String apply(WithCacheControl annotation)
  37. {
  38. return annotation.value();
  39. }
  40. })
  41. .getOrElse(DEFAULT_CACHE_CONTROL);
  42. }
  43. private static boolean responseCacheControlIsSet(Http.Response response)
  44. {
  45. return response.getHeaders().containsKey(CACHE_CONTROL);
  46. }
  47. }