Skip to content

Commit

Permalink
Handle NPE during get remote hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhan1 committed Sep 4, 2024
1 parent 1b02583 commit 3130f85
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.commonjava.indy.service.repository.controller;

import org.apache.commons.lang3.StringUtils;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import org.commonjava.indy.service.repository.data.ArtifactStoreQuery;
import org.commonjava.indy.service.repository.data.StoreDataManager;
import org.commonjava.indy.service.repository.exception.IndyDataException;
Expand All @@ -34,6 +34,7 @@

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -71,7 +72,7 @@ public List<ArtifactStore> getAllArtifactStores( final String packageType, final
throws IndyWorkflowException
{
List<StoreType> typesLs = new ArrayList<>();
if ( StringUtils.isNotBlank( types ) )
if ( isNotBlank( types ) )
{
typesLs = Arrays.stream( types.split( "," ) )
.map( String::trim )
Expand Down Expand Up @@ -211,15 +212,25 @@ public String getAllRemoteRepositoryHosts()
throws IndyDataException
{
final Set<String> ret = new HashSet<>();
storeManager.query().getAllRemoteRepositories(PKG_TYPE_MAVEN).forEach( r -> ret.add( r.getHost() ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_NPM).forEach( r -> ret.add( r.getHost() ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_GENERIC_HTTP).forEach( r -> ret.add( r.getHost() ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_MAVEN).forEach( r -> safelyAddHost( ret, r ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_NPM).forEach( r -> safelyAddHost( ret, r ) );
storeManager.query().getAllRemoteRepositories(PKG_TYPE_GENERIC_HTTP).forEach( r -> safelyAddHost( ret, r ) );

final StringBuilder sb = new StringBuilder();
ret.stream().sorted().forEach( s -> sb.append(s).append(","));
return sb.toString();
}

private void safelyAddHost( Set<String> hosts, RemoteRepository r )
{
// getHost returns null if MalformedURLException occurs during parsing
String host = r.getHost();
if ( isNotBlank( host ) )
{
hosts.add( host );
}
}

public List<HostedRepository> getAllHostedRepositories( final String packageType, final String enabled )
throws IndyWorkflowException
{
Expand Down Expand Up @@ -280,7 +291,7 @@ public EndpointViewListing getEndpointsListing( final String pkgType, final Stri
try
{
stores = new ArrayList<>( storeManager.getAllArtifactStores() );
if ( StringUtils.isNotBlank( pkgType ) && !"all".equals( pkgType ) && isValidPackageType( pkgType ) )
if ( isNotBlank( pkgType ) && !"all".equals( pkgType ) && isValidPackageType( pkgType ) )
{
stores = stores.stream()
.filter( s -> pkgType.equals( s.getPackageType() ) )
Expand Down Expand Up @@ -321,7 +332,7 @@ public Map<String, List<String>> getStoreKeysByPackageType( final String pkgType
final Map<String, List<String>> result = new HashMap<>();
stores = new ArrayList<>( storeManager.getAllArtifactStores() );
List<String> items;
if ( StringUtils.isNotBlank( pkgType ) && !"all".equals( pkgType ) && isValidPackageType( pkgType ) )
if ( isNotBlank( pkgType ) && !"all".equals( pkgType ) && isValidPackageType( pkgType ) )
{
items = stores.stream()
.filter( s -> pkgType.equals( s.getPackageType() ) )
Expand Down

0 comments on commit 3130f85

Please sign in to comment.