Skip to content

Commit

Permalink
Null checks in ContainerState.getMappedPort
Browse files Browse the repository at this point in the history
Guard against `getNetworkSettings()` returning null
  • Loading branch information
candrews committed Mar 26, 2021
1 parent a0e3e47 commit 18bd092
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Objects;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -142,9 +143,11 @@ default Integer getMappedPort(int originalPort) {

Ports.Binding[] binding = new Ports.Binding[0];
final InspectContainerResponse containerInfo = this.getContainerInfo();
if (containerInfo != null) {
binding = containerInfo.getNetworkSettings().getPorts().getBindings().get(new ExposedPort(originalPort));
}
binding = Optional.ofNullable(containerInfo)
.flatMap(it -> Optional.ofNullable(it.getNetworkSettings()))
.flatMap(it -> Optional.ofNullable(it.getPorts()))
.flatMap(it -> Optional.ofNullable(it.getBindings()))
.flatMap(it -> Optional.ofNullable(it.get(new ExposedPort(originalPort)))).orElse(null);

if (binding != null && binding.length > 0 && binding[0] != null) {
return Integer.valueOf(binding[0].getHostPortSpec());
Expand Down

0 comments on commit 18bd092

Please sign in to comment.