Skip to content

Commit

Permalink
feat: extend the api with a application name endpoint (#966)
Browse files Browse the repository at this point in the history
* feat: extend the api with a application name endpoint

* fix: adds openapi docs
  • Loading branch information
FritzHoing authored Jan 16, 2025
1 parent b8c921d commit de669ef
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@

import de.terrestris.shogun.lib.model.Application;
import de.terrestris.shogun.lib.service.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.Optional;

@RestController
@RequestMapping("/applications")
Expand All @@ -32,4 +42,82 @@
description = "The endpoints to manage applications"
)
@SecurityRequirement(name = "bearer-key")
public class ApplicationController extends BaseController<ApplicationService, Application> { }
@Log4j2
public class ApplicationController extends BaseController<ApplicationService, Application> {
@GetMapping("/findByName/{name}")
@ResponseStatus(HttpStatus.OK)
@Operation(security = { @SecurityRequirement(name = "bearer-key") })
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "Ok: Successfully returned the application with the given name"
),
@ApiResponse(
responseCode = "401",
description = "Unauthorized: You need to provide a bearer token",
content = @Content
),
@ApiResponse(
responseCode = "404",
description = "Not found: An application with the provided name does not exist (or you don't have the permission to open it)"
),
@ApiResponse(
responseCode = "500",
description = "Internal Server Error: Something internal went wrong while returning the entity"
)
})
public Application findOne(@PathVariable("name") String applicationName) {
log.trace("Requested to return application with name {}", applicationName);

try {
Optional<Application> entity = service.findOne(applicationName);

if (entity.isPresent()) {
Application persistedEntity = entity.get();

log.trace("Successfully got application with name {}", applicationName);

return persistedEntity;
} else {
log.error("Could not find application with name {}", applicationName);

throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
messageSource.getMessage(
"BaseController.NOT_FOUND",
null,
LocaleContextHolder.getLocale()
)
);
}
} catch (AccessDeniedException ade) {
log.warn("Access to application with name {} is denied", applicationName);

throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
messageSource.getMessage(
"BaseController.NOT_FOUND",
null,
LocaleContextHolder.getLocale()
),
ade
);
} catch (ResponseStatusException rse) {
throw rse;
} catch (Exception e) {
log.error("Error while requesting application with name {}: \n {}",
applicationName, e.getMessage());
log.trace("Full stack trace: ", e);

throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR,
messageSource.getMessage(
"BaseController.INTERNAL_SERVER_ERROR",
null,
LocaleContextHolder.getLocale()
),
e
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@

import de.terrestris.shogun.lib.model.Application;
import de.terrestris.shogun.lib.repository.ApplicationRepository;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class ApplicationService extends BaseService<ApplicationRepository, Application> { }
public class ApplicationService extends BaseService<ApplicationRepository, Application> {
@PostAuthorize("hasRole('ROLE_ADMIN') or hasPermission(returnObject.orElse(null), 'READ')")
@Transactional(readOnly = true)
public Optional<Application> findOne(String name) {
return repository.findByName(name);
}
}

0 comments on commit de669ef

Please sign in to comment.