UrlGenerator.java

  1. package no.nav.data.team.notify;

  2. import lombok.Getter;
  3. import no.nav.data.common.auditing.domain.AuditVersion;
  4. import no.nav.data.common.security.SecurityProperties;
  5. import no.nav.data.common.storage.domain.DomainObject;
  6. import no.nav.data.common.storage.domain.TypeRegistration;
  7. import no.nav.data.team.po.domain.ProductArea;
  8. import no.nav.data.team.shared.dto.Links.NamedLink;
  9. import org.springframework.stereotype.Component;

  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.UUID;
  13. import java.util.stream.Collectors;

  14. @Component
  15. public class UrlGenerator {

  16.     private static final String teamSlackId = "T5LNAMWNA";
  17.     private static final String slackUrl = "https://slack.com/app_redirect?team=%s&channel=%s";

  18.     @Getter
  19.     private final String baseUrl;
  20.     @Getter
  21.     private final boolean dev;

  22.     private static UrlGenerator INSTANCE;

  23.     public UrlGenerator(SecurityProperties securityProperties) {
  24.         baseUrl = securityProperties.findBaseUrl();
  25.         dev = securityProperties.isDev();
  26.         INSTANCE = this;
  27.     }

  28.     public String urlFor(Class<? extends DomainObject> type, UUID id) {
  29.         return baseUrl + "/" + urlPathForTable(TypeRegistration.typeOf(type)) + "/" + id;
  30.     }

  31.     public String urlFor(AuditVersion auditVersion) {
  32.         return baseUrl + "/" + urlPathForTable(auditVersion.getTable()) + "/" + auditVersion.getTableId();
  33.     }

  34.     public String resourceUrl(String ident) {
  35.         return baseUrl + "/resource/" + ident;
  36.     }

  37.     public List<NamedLink> slackUrls(String slack) {
  38.         return Arrays.stream(slack.replaceAll("[#,]", "")
  39.                 .split(" "))
  40.                 .map(String::trim)
  41.                 .filter(s -> !s.isEmpty())
  42.                 .map(s -> new NamedLink("#" + s, slackUrl.formatted(teamSlackId, s)))
  43.                 .collect(Collectors.toList());
  44.     }

  45.     private String urlPathForTable(String table) {
  46.         if (table.equals(TypeRegistration.typeOf(ProductArea.class))) {
  47.             return "area";
  48.         }
  49.         return table.toLowerCase();
  50.     }

  51.     public static UrlGenerator instance() {
  52.         return INSTANCE;
  53.     }
  54. }