PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/legacy-stub/src/main/java/com/atlassian/webhooks/api/register/listener/PersistentWebHookListener.java

https://bitbucket.org/atlassian/atlassian-webhooks-plugin
Java | 199 lines | 156 code | 34 blank | 9 comment | 3 complexity | 3d7b3c508fc34be1818df99bbb9cf5b8 MD5 | raw file
  1. package com.atlassian.webhooks.api.register.listener;
  2. import com.atlassian.webhooks.api.util.Filter;
  3. import com.atlassian.webhooks.api.util.SectionKey;
  4. import com.google.common.base.MoreObjects;
  5. import com.google.common.collect.ImmutableMap;
  6. import com.google.common.collect.ImmutableSet;
  7. import com.google.common.collect.Maps;
  8. import com.google.common.collect.Sets;
  9. import io.atlassian.fugue.Option;
  10. import javax.annotation.Nullable;
  11. import javax.annotation.concurrent.Immutable;
  12. import java.io.Serializable;
  13. import java.util.Collection;
  14. import java.util.Date;
  15. import java.util.Map;
  16. import java.util.Set;
  17. import static com.google.common.base.Strings.nullToEmpty;
  18. /**
  19. * Webhook listener that can be saved in a persistent store.
  20. */
  21. @Deprecated
  22. @Immutable
  23. public class PersistentWebHookListener implements Serializable {
  24. private final Option<Integer> listenerId;
  25. private final String listenerName;
  26. private final String description;
  27. private final ImmutableSet<String> webHookIds;
  28. private final String url;
  29. private final boolean enabled;
  30. private final boolean excludeBody;
  31. private final ImmutableMap<SectionKey, Filter> filters;
  32. private final Date lastUpdated;
  33. private final String lastUpdatedByUser;
  34. private PersistentWebHookListener(Option<Integer> listenerId, String listenerName, String description,
  35. Set<String> webHookIds, String url, boolean enabled, boolean excludeBody,
  36. Map<SectionKey, Filter> filters, Date lastUpdated, String lastUpdatedByUser) {
  37. this.listenerId = listenerId;
  38. this.listenerName = nullToEmpty(listenerName);
  39. this.description = description;
  40. this.webHookIds = ImmutableSet.copyOf(webHookIds);
  41. this.url = nullToEmpty(url);
  42. this.enabled = enabled;
  43. this.excludeBody = excludeBody;
  44. this.filters = ImmutableMap.copyOf(filters);
  45. this.lastUpdated = MoreObjects.firstNonNull(lastUpdated, new Date());
  46. this.lastUpdatedByUser = nullToEmpty(lastUpdatedByUser);
  47. }
  48. public static Builder newlyCreated() {
  49. return new Builder(null);
  50. }
  51. public static Builder existing(Integer id) {
  52. return new Builder(id);
  53. }
  54. public Option<Integer> getId() {
  55. return listenerId;
  56. }
  57. public String getName() {
  58. return listenerName;
  59. }
  60. public String getDescription() {
  61. return description;
  62. }
  63. public Set<String> getEvents() {
  64. return webHookIds;
  65. }
  66. public String getUrl() {
  67. return url;
  68. }
  69. public boolean isEnabled() {
  70. return enabled;
  71. }
  72. public boolean isExcludeBody() {
  73. return excludeBody;
  74. }
  75. public Map<SectionKey, Filter> getFilters() {
  76. return filters;
  77. }
  78. public Date getLastUpdated() {
  79. return new Date(lastUpdated.getTime()); // defensive copy
  80. }
  81. public String getLastUpdatedByUser() {
  82. return lastUpdatedByUser;
  83. }
  84. /**
  85. * Returns a filter for the specified section, or empty string if the filter is not defined.
  86. *
  87. * @param sectionKey section for which we want the filter
  88. * @return filter or empty string if not found or if the section key is null
  89. */
  90. public String getFilterFor(@Nullable SectionKey sectionKey) {
  91. if (sectionKey == null) {
  92. return "";
  93. }
  94. return MoreObjects.firstNonNull(filters.get(sectionKey), new Filter("")).getValue();
  95. }
  96. public static class Builder {
  97. private final Integer listenerId;
  98. private final Set<String> webHookIds = Sets.newHashSet();
  99. private final Map<SectionKey, Filter> filters = Maps.newHashMap();
  100. private String listenerName;
  101. private String description;
  102. private String url;
  103. private boolean enabled;
  104. private boolean excludeBody;
  105. private Date lastUpdated = new Date();
  106. private String lastUpdatedByUser;
  107. private Builder(Integer listenerId) {
  108. this.listenerId = listenerId;
  109. }
  110. public Builder setListenerName(String listenerName) {
  111. this.listenerName = listenerName;
  112. return this;
  113. }
  114. public Builder setDescription(String description) {
  115. this.description = description;
  116. return this;
  117. }
  118. public Builder addWebHookId(String webhookId) {
  119. this.webHookIds.add(webhookId);
  120. return this;
  121. }
  122. public Builder addWebHookIds(Collection<String> webHookIds) {
  123. this.webHookIds.addAll(webHookIds);
  124. return this;
  125. }
  126. public Builder setUrl(String url) {
  127. this.url = url;
  128. return this;
  129. }
  130. public Builder setEnabled(boolean enabled) {
  131. this.enabled = enabled;
  132. return this;
  133. }
  134. public Builder setExcludeBody(boolean excludeBody) {
  135. this.excludeBody = excludeBody;
  136. return this;
  137. }
  138. public Builder addFilter(String section, String filter) {
  139. filters.put(new SectionKey(section), new Filter(filter));
  140. return this;
  141. }
  142. public Builder addFilters(Map<String, String> filters) {
  143. for (Map.Entry<String, String> entry : filters.entrySet()) {
  144. this.filters.put(new SectionKey(entry.getKey()), new Filter(entry.getValue()));
  145. }
  146. return this;
  147. }
  148. public Builder addFiltersTypeRich(Map<SectionKey, Filter> filters) {
  149. this.filters.putAll(filters);
  150. return this;
  151. }
  152. public Builder setLastUpdated(Date lastUpdated) {
  153. this.lastUpdated = lastUpdated;
  154. return this;
  155. }
  156. public Builder setLastUpdatedByUser(String lastUpdatedByUser) {
  157. this.lastUpdatedByUser = lastUpdatedByUser;
  158. return this;
  159. }
  160. public PersistentWebHookListener build() {
  161. return new PersistentWebHookListener(Option.option(listenerId), listenerName, description, webHookIds, url,
  162. enabled, excludeBody, filters, lastUpdated, lastUpdatedByUser);
  163. }
  164. }
  165. }