Skip to content

Commit

Permalink
Get a service based on only MRN
Browse files Browse the repository at this point in the history
This will return either a direct match or the newest service that has the given mrn as a prefix
  • Loading branch information
oliverhaagh committed Jan 19, 2024
1 parent bfa3867 commit 1ef75b5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import net.maritimeconnectivity.identityregistry.exception.DuplicatedKeycloakEntry;
import net.maritimeconnectivity.identityregistry.exception.McpBasicRestException;
Expand Down Expand Up @@ -54,6 +52,8 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
Expand Down Expand Up @@ -179,6 +179,49 @@ public Page<Service> getServices(HttpServletRequest request, @PathVariable Strin
}
}

/**
* @param request the HTTP request
* @param orgMrn the org MRN
* @param serviceMrn the service MRN
* @return a response containing a service
* @throws McpBasicRestException if something goes wrong
*/
@GetMapping(
value = "/api/org/{orgMrn}/service/{serviceMrn}",
produces = MediaType.APPLICATION_JSON_VALUE
)
@Operation(
description = "Get the service identity with the given MRN. If a direct match cannot be found, the service " +
"that was last created and has the given MRN as a prefix of its MRN followed by an additional namespace will be returned."
)
public ResponseEntity<Service> getService(HttpServletRequest request, @PathVariable String orgMrn, @PathVariable String serviceMrn) throws McpBasicRestException {
Organization org = this.organizationService.getOrganizationByMrn(orgMrn);
if (org != null) {
// Check that the entity being queried belongs to the organization
if (!mrnUtil.getOrgShortNameFromOrgMrn(orgMrn).equalsIgnoreCase(mrnUtil.getOrgShortNameFromEntityMrn(serviceMrn))) {
throw new McpBasicRestException(HttpStatus.BAD_REQUEST, MCPIdRegConstants.MISSING_RIGHTS, request.getServletPath());
}

// Start by checking whether we have a direct match on the MRN
Service service = entityService.getByMrn(serviceMrn);
if (service != null) {
return ResponseEntity.ok(service);
}

// Else, check if there are any services that have the MRN as prefix of their MRN and return the latest that was created
service = ((ServiceService) entityService).getNewestServiceByMrn(serviceMrn);
if (service == null) {
throw new McpBasicRestException(HttpStatus.NOT_FOUND, MCPIdRegConstants.ENTITY_NOT_FOUND, request.getServletPath());
}
if (service.getIdOrganization().equals(org.getId())) {
return new ResponseEntity<>(service, HttpStatus.OK);
}
throw new McpBasicRestException(HttpStatus.FORBIDDEN, MCPIdRegConstants.MISSING_RIGHTS, request.getServletPath());
} else {
throw new McpBasicRestException(HttpStatus.NOT_FOUND, MCPIdRegConstants.ORG_NOT_FOUND, request.getServletPath());
}
}

/**
* Returns specific version of the service instance identified by the given ID
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public interface ServiceRepository extends PagingAndSortingRepository<Service, L
Service findByMrnIgnoreCase(String mrn);

Page<Service> findByMrnStartingWithIgnoreCase(String mrn, Pageable pageable);

List<Service> findByMrnStartingWithIgnoreCase(String mrn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public interface ServiceService extends EntityService<Service> {
Service getServiceByMrnAndVersion(String mrn, String version);

Page<Service> getServicesByMrn(String mrn, Pageable pageable);

List<Service> getServicesByMrn(String mrn);
Service getNewestServiceByMrn(String mrn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -63,6 +64,23 @@ public Page<Service> getServicesByMrn(String mrn, Pageable pageable) {
return this.filterResult(ret);
}

@Override
public List<Service> getServicesByMrn(String mrn) {
List<Service> services = serviceRepository.findByMrnStartingWithIgnoreCase(mrn);
return this.filterResult(services);
}

@Override
public Service getNewestServiceByMrn(String mrn) {
List<Service> services = getServicesByMrn(mrn);
int mrnSplitLength = mrn.split(":").length;

services = services.stream()
.filter(s -> s.getMrn().split(":").length == mrnSplitLength + 1)
.sorted(Comparator.comparing(Service::getCreatedAt)).toList();
return services.isEmpty() ? null : services.getFirst();
}

@Transactional
@Override
public void deleteByOrg(Long id) {
Expand Down

0 comments on commit 1ef75b5

Please sign in to comment.