OrgController.java

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

  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  4. import io.swagger.v3.oas.annotations.tags.Tag;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import no.nav.data.common.security.SecurityUtils;
  8. import no.nav.data.common.security.dto.UserInfo;
  9. import no.nav.data.team.resource.NomGraphClient;
  10. import no.nav.nom.graphql.model.OrgEnhetDto;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.security.core.context.SecurityContextHolder;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.PathVariable;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RestController;

  17. @Slf4j
  18. @RestController
  19. @RequestMapping("/org")
  20. @Tag(name = "Org")
  21. @RequiredArgsConstructor
  22. public class OrgController {

  23.     private final NomGraphClient nomGraphClient;

  24.     @Operation(summary = "Get Org")
  25.     @ApiResponse(description = "ok")
  26.     @GetMapping("/{id}")
  27.     public ResponseEntity<OrgEnhetDto> getUnitsById(@PathVariable String id) {
  28.         temporaryLogConsumer();

  29.         log.info("Org get id={}", id);
  30.         var org = nomGraphClient.getOrgEnhet(id);
  31.         return org.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
  32.     }

  33.     private void temporaryLogConsumer() {
  34.         var auth = SecurityContextHolder.getContext().getAuthentication();
  35.         var c = auth.getPrincipal().getClass();
  36.         var x = SecurityUtils.getCurrentUser().map(UserInfo::getAppName);
  37.         var y = SecurityUtils.getCurrentUser().map(UserInfo::getAppId);
  38.         log.info("/org called by: name = " + x.orElse("<>") + " , id = " + y.orElse("<>") + " . Principal class = " + c.getName() + " , Authentication class = " + auth.getClass().getName());
  39.     }

  40. }