[2.7.x] NEXUS-6249: Fix and UT

Backported to Nexus 2.7.x, original PR
https://github.com/sonatype/nexus-oss/pull/341

Issue
https://issues.sonatype.org/browse/NEXUS-6249

CI
https://bamboo.zion.sonatype.com/browse/NX-OSSF17