Skip to content

Commit

Permalink
use Optional in claim metadata managers
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-ariyaratne committed Oct 22, 2024
1 parent 6f72249 commit 291bdd4
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.wso2.carbon.user.api.UserStoreException;

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

/**
* Database based claim metadata manager.
Expand All @@ -54,16 +55,15 @@ public List<ClaimDialect> getClaimDialects(int tenantId) throws ClaimMetadataExc
}

@Override
public ClaimDialect getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {
public Optional<ClaimDialect> getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(claimDialectURI)) {
return null;
throw new ClaimMetadataException("Invalid claim dialect URI: " + claimDialectURI);
}

return this.claimDialectDAO.getClaimDialects(tenantId).stream()
.filter(claimDialect -> claimDialectURI.equals(claimDialect.getClaimDialectURI()))
.findFirst()
.orElse(null);
.findFirst();
}

@Override
Expand All @@ -88,16 +88,16 @@ public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataExcepti
}

@Override
public LocalClaim getLocalClaim(String localClaimURI , int tenantId) throws ClaimMetadataException {
public Optional<LocalClaim> getLocalClaim(String localClaimURI , int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(localClaimURI)) {
return null;
throw new ClaimMetadataException("Invalid local claim URI: " + localClaimURI);
}

List<LocalClaim> localClaims = this.localClaimDAO.getLocalClaims(tenantId);
return localClaims.stream()
.filter(localClaim -> localClaimURI.equals(localClaim.getClaimURI()))
.findFirst().orElse(null);
.findFirst();
}

@Override
Expand All @@ -108,16 +108,16 @@ public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int
}

@Override
public ExternalClaim getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
public Optional<ExternalClaim> getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
throws ClaimMetadataException {

if (StringUtils.isBlank(claimURI)) {
return null;
if (StringUtils.isBlank(externalClaimDialectURI) || StringUtils.isBlank(claimURI)) {
throw new ClaimMetadataException("Invalid external claim dialect URI or claim URI");
}

return this.externalClaimDAO.getExternalClaims(externalClaimDialectURI, tenantId).stream()
.filter(externalClaim -> claimURI.equals(externalClaim.getClaimURI()))
.findFirst().orElse(null);
.findFirst();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.wso2.carbon.identity.claim.metadata.mgt.util.ClaimConstants.LOCAL_CLAIM_DIALECT_URI;
Expand Down Expand Up @@ -61,16 +62,15 @@ public List<ClaimDialect> getClaimDialects(int tenantId) throws ClaimMetadataExc
}

@Override
public ClaimDialect getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {
public Optional<ClaimDialect> getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(claimDialectURI)) {
return null;
throw new ClaimMetadataException("Invalid claim dialect URI: " + claimDialectURI);
}

return claimDialects.stream()
.filter(claimDialect -> claimDialectURI.equals(claimDialect.getClaimDialectURI()))
.findFirst()
.orElse(null);
.findFirst();
}

@Override
Expand All @@ -88,25 +88,24 @@ public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataExcepti
}

@Override
public LocalClaim getLocalClaim(String localClaimURI ,int tenantId) throws ClaimMetadataException {
public Optional<LocalClaim> getLocalClaim(String localClaimURI ,int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(localClaimURI)) {
return null;
throw new ClaimMetadataException("Invalid local claim URI: " + localClaimURI);
}

return claims.getOrDefault(LOCAL_CLAIM_DIALECT_URI, Collections.emptyList()).stream()
.filter(claim -> localClaimURI.equals(claim.getClaimURI()))
.map(LocalClaim.class::cast)
.findFirst()
.orElse(null);
.findFirst();
}

@Override
public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int tenantId)
throws ClaimMetadataException {

if (StringUtils.isBlank(externalClaimDialectURI)) {
return null;
throw new ClaimMetadataException("Invalid external claim dialect URI: " + externalClaimDialectURI);
}

if (externalClaimDialectURI.equals(LOCAL_CLAIM_DIALECT_URI)) {
Expand All @@ -119,10 +118,11 @@ public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int
}

@Override
public ExternalClaim getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId) throws ClaimMetadataException {
public Optional<ExternalClaim> getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
throws ClaimMetadataException {

if (StringUtils.isBlank(externalClaimDialectURI) || StringUtils.isBlank(claimURI)) {
return null;
throw new ClaimMetadataException("Invalid external claim dialect URI or claim URI");
}

if (externalClaimDialectURI.equals(LOCAL_CLAIM_DIALECT_URI)) {
Expand All @@ -132,15 +132,14 @@ public ExternalClaim getExternalClaim(String externalClaimDialectURI, String cla
return claims.getOrDefault(externalClaimDialectURI, Collections.emptyList()).stream()
.filter(claim -> claimURI.equals(claim.getClaimURI()))
.map(ExternalClaim.class::cast)
.findFirst()
.orElse(null);
.findFirst();
}

@Override
public List<Claim> getMappedExternalClaims(String localClaimURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(localClaimURI)) {
return null;
throw new ClaimMetadataException("Invalid local claim URI: " + localClaimURI);
}

List<Claim> mappedExternalClaims = new ArrayList<>();
Expand All @@ -162,7 +161,7 @@ public List<Claim> getMappedExternalClaims(String localClaimURI, int tenantId) t
public boolean isMappedLocalClaim(String localClaimURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(localClaimURI)) {
return false;
throw new ClaimMetadataException("Invalid local claim URI: " + localClaimURI);
}

for (Map.Entry<String, List<Claim>> entry : claims.entrySet()) {
Expand All @@ -184,10 +183,10 @@ public boolean isMappedLocalClaim(String localClaimURI, int tenantId) throws Cla
@Override
public boolean isLocalClaimMappedWithinDialect(String mappedLocalClaim, String externalClaimDialectURI, int tenantId) throws ClaimMetadataException {

if (StringUtils.isBlank(externalClaimDialectURI) || !claims.containsKey(externalClaimDialectURI)) {
return false;
if (StringUtils.isBlank(externalClaimDialectURI) || StringUtils.isBlank(mappedLocalClaim)) {
throw new ClaimMetadataException("Invalid external claim dialect URI or mapped local claim");
}
if (StringUtils.isBlank(mappedLocalClaim)) {
if (!claims.containsKey(externalClaimDialectURI)) {
return false;
}
return claims.get(externalClaimDialectURI).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -74,10 +75,10 @@ public List<ClaimDialect> getClaimDialects(int tenantId) throws ClaimMetadataExc
* @return Claim dialect.
* @throws ClaimMetadataException If an error occurs while retrieving claim dialect.
*/
public ClaimDialect getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {
public Optional<ClaimDialect> getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {

ClaimDialect claimDialectInDB = this.dbBasedClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId);
if (claimDialectInDB != null) {
Optional<ClaimDialect> claimDialectInDB = this.dbBasedClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId);
if (claimDialectInDB.isPresent()) {
return claimDialectInDB;
}
return this.systemDefaultClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId);
Expand Down Expand Up @@ -153,13 +154,13 @@ public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataExcepti
* @return Local claim.
* @throws ClaimMetadataException If an error occurs while retrieving local claim.
*/
public LocalClaim getLocalClaim(String localClaimURI, int tenantId) throws ClaimMetadataException {
public Optional<LocalClaim> getLocalClaim(String localClaimURI, int tenantId) throws ClaimMetadataException {

LocalClaim localClaim = this.dbBasedClaimMetadataManager.getLocalClaim(localClaimURI, tenantId);
if (localClaim == null) {
localClaim = this.systemDefaultClaimMetadataManager.getLocalClaim(localClaimURI, tenantId);
Optional<LocalClaim> localClaimInDB = this.dbBasedClaimMetadataManager.getLocalClaim(localClaimURI, tenantId);
if (localClaimInDB.isPresent()) {
return localClaimInDB;
}
return localClaim;
return this.systemDefaultClaimMetadataManager.getLocalClaim(localClaimURI, tenantId);
}

/**
Expand Down Expand Up @@ -282,16 +283,15 @@ public List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int
* @return External claim.
* @throws ClaimMetadataException If an error occurs while retrieving external claim.
*/
public ExternalClaim getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
public Optional<ExternalClaim> getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
throws ClaimMetadataException {

ExternalClaim externalClaim = this.dbBasedClaimMetadataManager.getExternalClaim(
Optional<ExternalClaim> externalClaim = this.dbBasedClaimMetadataManager.getExternalClaim(
externalClaimDialectURI, claimURI, tenantId);
if (externalClaim == null) {
externalClaim = this.systemDefaultClaimMetadataManager.getExternalClaim(
externalClaimDialectURI, claimURI, tenantId);
if (externalClaim.isPresent()) {
return externalClaim;
}
return externalClaim;
return this.systemDefaultClaimMetadataManager.getExternalClaim(externalClaimDialectURI, claimURI, tenantId);
}

/**
Expand Down Expand Up @@ -440,9 +440,7 @@ public boolean isLocalClaimMappedWithinDialect(String mappedLocalClaim, String e
*/
public boolean isSystemDefaultClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException {

ClaimDialect claimDialectInSystem = this.systemDefaultClaimMetadataManager.getClaimDialect(claimDialectURI,
tenantId);
return claimDialectInSystem != null;
return this.systemDefaultClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId).isPresent();
}

/**
Expand Down Expand Up @@ -475,25 +473,27 @@ public boolean isSystemDefaultExternalClaim(String externalClaimDialectURI, Stri

private boolean isClaimDialectInDB(String claimDialectURI, int tenantId) throws ClaimMetadataException {

return this.dbBasedClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId) != null;
return this.dbBasedClaimMetadataManager.getClaimDialect(claimDialectURI, tenantId).isPresent();
}

private boolean isLocalClaimInDB(String localClaimURI, int tenantId) throws ClaimMetadataException {

return this.dbBasedClaimMetadataManager.getLocalClaim(localClaimURI, tenantId) != null;
return this.dbBasedClaimMetadataManager.getLocalClaim(localClaimURI, tenantId).isPresent();
}

private boolean isExternalClaimInDB(String claimURI, String claimDialectURI, int tenantId)
throws ClaimMetadataException {

return this.dbBasedClaimMetadataManager.getExternalClaim(claimDialectURI, claimURI, tenantId) != null;
return this.dbBasedClaimMetadataManager.getExternalClaim(claimDialectURI, claimURI, tenantId).isPresent();
}

private void addSystemDefaultDialectToDB(String claimDialectURI, int tenantId) throws ClaimMetadataException {

ClaimDialect claimDialectInSystem = this.systemDefaultClaimMetadataManager.getClaimDialect(claimDialectURI,
tenantId);
this.dbBasedClaimMetadataManager.addClaimDialect(claimDialectInSystem, tenantId);
Optional<ClaimDialect> claimDialectInSystem = this.systemDefaultClaimMetadataManager
.getClaimDialect(claimDialectURI, tenantId);
if (claimDialectInSystem.isPresent()) {
this.dbBasedClaimMetadataManager.addClaimDialect(claimDialectInSystem.get(), tenantId);
}
}

private void addSystemDefaultLocalClaimToDB(String claimURI, int tenantId)
Expand All @@ -503,7 +503,9 @@ private void addSystemDefaultLocalClaimToDB(String claimURI, int tenantId)
if (!isClaimDialectInDB) {
addSystemDefaultDialectToDB(ClaimConstants.LOCAL_CLAIM_DIALECT_URI, tenantId);
}
LocalClaim claimInSystem = this.systemDefaultClaimMetadataManager.getLocalClaim(claimURI, tenantId);
this.dbBasedClaimMetadataManager.addLocalClaim(claimInSystem, tenantId);
Optional<LocalClaim> claimInSystem = this.systemDefaultClaimMetadataManager.getLocalClaim(claimURI, tenantId);
if (claimInSystem.isPresent()) {
this.dbBasedClaimMetadataManager.addLocalClaim(claimInSystem.get(), tenantId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.wso2.carbon.identity.claim.metadata.mgt.model.LocalClaim;

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

/**
* Claim metadata reader.
Expand All @@ -48,7 +49,7 @@ public interface ClaimMetadataReader {
* @return Claim dialect.
* @throws ClaimMetadataException if an error occurs during the operation.
*/
ClaimDialect getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException;
Optional<ClaimDialect> getClaimDialect(String claimDialectURI, int tenantId) throws ClaimMetadataException;

/**
* Get all local claims.
Expand All @@ -67,7 +68,7 @@ public interface ClaimMetadataReader {
* @return Local claim.
* @throws ClaimMetadataException if an error occurs during the operation.
*/
LocalClaim getLocalClaim(String localClaimURI ,int tenantId) throws ClaimMetadataException;
Optional<LocalClaim> getLocalClaim(String localClaimURI ,int tenantId) throws ClaimMetadataException;

/**
* Get all external claims.
Expand All @@ -88,7 +89,7 @@ List<ExternalClaim> getExternalClaims(String externalClaimDialectURI, int tenant
* @return External claim.
* @throws ClaimMetadataException if an error occurs during the operation.
*/
ExternalClaim getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
Optional<ExternalClaim> getExternalClaim(String externalClaimDialectURI, String claimURI, int tenantId)
throws ClaimMetadataException;

/**
Expand Down
Loading

0 comments on commit 291bdd4

Please sign in to comment.