Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
strehle committed Nov 21, 2024
1 parent 92dfa15 commit aafcd7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,22 @@ public List<IdentityProvider> retrieveActive(String zoneId) {

@Override
public List<IdentityProvider> retrieveActiveByTypes(final String zoneId, final String... types) {
if (types == null || types.length == 0) {
if (ObjectUtils.isNotEmpty(types)) {
// eliminate duplicates
final Set<String> typesAsSet = new HashSet<>(Arrays.asList(types));

// adjust the number of SQL parameters in the prepared statement
final String sqlPlaceholdersForTypes = typesAsSet.stream().map(type -> "?").collect(joining(","));
final String sql = IDENTITY_ACTIVE_PROVIDERS_OF_TYPE_QUERY_TEMPLATE.formatted(sqlPlaceholdersForTypes);

final ArrayList<Object> arrayList = new ArrayList<>(typesAsSet.size() + 2);
arrayList.add(zoneId);
arrayList.add(true);
arrayList.addAll(typesAsSet);
return jdbcTemplate.query(sql, mapper, arrayList.toArray());
} else {
return emptyList();
}

// eliminate duplicates
final Set<String> typesAsSet = new HashSet<>(Arrays.asList(types));

// adjust the number of SQL parameters in the prepared statement
final String sqlPlaceholdersForTypes = typesAsSet.stream()
.map(type -> "?")
.collect(joining(","));
final String sql = IDENTITY_ACTIVE_PROVIDERS_OF_TYPE_QUERY_TEMPLATE.formatted(sqlPlaceholdersForTypes);

final Object[] args = new Object[2 + typesAsSet.size()];
args[0] = zoneId;
args[1] = true; // active
final List<String> typesAsList = new ArrayList<>(typesAsSet);
for (int i = 0; i < typesAsList.size(); i++) {
args[i + 2] = typesAsList.get(i);
}
return jdbcTemplate.query(sql, mapper, args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.http.client.utils.URIBuilder;
import org.cloudfoundry.identity.uaa.constants.OriginKeys;
import org.cloudfoundry.identity.uaa.provider.IdentityProvider;
import org.cloudfoundry.identity.uaa.provider.IdentityProviderProvisioning;
import org.cloudfoundry.identity.uaa.provider.SamlIdentityProviderDefinition;
import org.cloudfoundry.identity.uaa.zone.IdentityZone;
Expand All @@ -19,7 +18,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

import static org.springframework.util.StringUtils.hasText;
Expand All @@ -44,25 +42,14 @@ public List<SamlIdentityProviderDefinition> getIdentityProviderDefinitions() {
}

public List<SamlIdentityProviderDefinition> getIdentityProviderDefinitionsForZone(IdentityZone zone) {
final List<IdentityProvider> samlIdpsInZone = providerProvisioning.retrieveActiveByTypes(zone.getId(),
OriginKeys.SAML);
return samlIdpsInZone.stream()
return providerProvisioning.retrieveActiveByTypes(zone.getId(), OriginKeys.SAML).stream()
.map(samlIdp -> (SamlIdentityProviderDefinition) samlIdp.getConfig())
.toList();
}

public List<SamlIdentityProviderDefinition> getIdentityProviderDefinitions(List<String> allowedIdps, IdentityZone zone) {
List<SamlIdentityProviderDefinition> idpsInTheZone = getIdentityProviderDefinitionsForZone(zone);
if (allowedIdps != null) {
List<SamlIdentityProviderDefinition> result = new LinkedList<>();
for (SamlIdentityProviderDefinition def : idpsInTheZone) {
if (allowedIdps.contains(def.getIdpEntityAlias())) {
result.add(def);
}
}
return result;
}
return idpsInTheZone;
return getIdentityProviderDefinitionsForZone(zone).stream()
.filter(def -> allowedIdps == null || allowedIdps.contains(def.getIdpEntityAlias())).toList();
}

/**
Expand Down

0 comments on commit aafcd7a

Please sign in to comment.