Skip to content

Commit

Permalink
Replace Guava by JDK (Partly) (wiremock#2380)
Browse files Browse the repository at this point in the history
* Replace Guava by JDK (Partly)

* Replace Guava by JDK (Partly)

---------

Co-authored-by: Kirill Peshin <[email protected]>
  • Loading branch information
pks-1981 and Kirill Peshin authored Sep 18, 2023
1 parent 81dd277 commit 851588b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package com.github.tomakehurst.wiremock.common;

import com.google.common.net.InetAddresses;
import java.math.BigInteger;
import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.ipToLong;
import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.isValidInet4Address;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Objects;
Expand Down Expand Up @@ -84,25 +85,25 @@ public String toString() {

private static class IpRange extends NetworkAddressRange {

private final BigInteger start;
private final BigInteger end;
private final Long start;
private final Long end;
private final String asString;

private IpRange(String ipRange) {
String[] parts = ipRange.split("-");
if (parts.length != 2) {
throw new InvalidInputException(Errors.single(18, ipRange + " is not a valid IP range"));
}
this.start = InetAddresses.toBigInteger(parseIpAddress(parts[0]));
this.end = InetAddresses.toBigInteger(parseIpAddress(parts[1]));
this.start = ipToLong(parseIpAddress(parts[0]));
this.end = ipToLong(parseIpAddress(parts[1]));
this.asString = ipRange;
}

@Override
public boolean isIncluded(String testValue) {
InetAddress testValueAddress = lookup(testValue);
BigInteger intVal = InetAddresses.toBigInteger(testValueAddress);
return intVal.compareTo(start) >= 0 && intVal.compareTo(end) <= 0;
long longValue = ipToLong(testValueAddress);
return (longValue >= start && longValue <= end);
}

@Override
Expand Down Expand Up @@ -166,7 +167,7 @@ public boolean isIncluded(String testValue) {
}

private static InetAddress parseIpAddress(String ipAddress) {
if (!InetAddresses.isInetAddress(ipAddress)) {
if (!isValidInet4Address(ipAddress)) {
throw new InvalidInputException(Errors.single(16, ipAddress + " is not a valid IP address"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.github.tomakehurst.wiremock.common;

import static com.github.tomakehurst.wiremock.common.NetworkAddressRange.ALL;
import static com.github.tomakehurst.wiremock.common.NetworkAddressUtils.isValidInet4Address;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toSet;

import com.google.common.collect.ImmutableSet;
import com.google.common.net.InetAddresses;
import java.util.HashSet;
import java.util.Set;

public class NetworkAddressRules {
Expand All @@ -34,7 +34,8 @@ public static Builder builder() {
private final Set<NetworkAddressRange> denied;
private final Set<NetworkAddressRange> deniedHostPatterns;

public static NetworkAddressRules ALLOW_ALL = new NetworkAddressRules(Set.of(ALL), emptySet());
public static final NetworkAddressRules ALLOW_ALL =
new NetworkAddressRules(Set.of(ALL), emptySet());

public NetworkAddressRules(Set<NetworkAddressRange> allowed, Set<NetworkAddressRange> denied) {
this.allowed =
Expand Down Expand Up @@ -78,7 +79,7 @@ private static <T> Set<T> defaultIfEmpty(Set<T> original, Set<T> ifEmpty) {
}

public boolean isAllowed(String testValue) {
if (InetAddresses.isInetAddress(testValue)) {
if (isValidInet4Address(testValue)) {
return allowed.stream().anyMatch(rule -> rule.isIncluded(testValue))
&& denied.stream().noneMatch(rule -> rule.isIncluded(testValue));
} else {
Expand All @@ -88,8 +89,8 @@ public boolean isAllowed(String testValue) {
}

public static class Builder {
private final ImmutableSet.Builder<NetworkAddressRange> allowed = ImmutableSet.builder();
private final ImmutableSet.Builder<NetworkAddressRange> denied = ImmutableSet.builder();
private final Set<NetworkAddressRange> allowed = new HashSet<>();
private final Set<NetworkAddressRange> denied = new HashSet<>();

public Builder allow(String expression) {
allowed.add(NetworkAddressRange.of(expression));
Expand All @@ -102,11 +103,11 @@ public Builder deny(String expression) {
}

public NetworkAddressRules build() {
Set<NetworkAddressRange> allowedRanges = allowed.build();
Set<NetworkAddressRange> allowedRanges = allowed;
if (allowedRanges.isEmpty()) {
allowedRanges = Set.of(ALL);
}
return new NetworkAddressRules(allowedRanges, denied.build());
return new NetworkAddressRules(Set.copyOf(allowedRanges), Set.copyOf(denied));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.common;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class NetworkAddressUtils {

private NetworkAddressUtils() {}

public static boolean isValidInet4Address(String ip) {
try {
return InetAddress.getByName(ip).getHostAddress().equals(ip);
} catch (UnknownHostException ex) {
return false;
}
}

public static long ipToLong(InetAddress ipAddress) {
long resultIP = 0;
byte[] ipAddressOctets = ipAddress.getAddress();

for (byte octet : ipAddressOctets) {
resultIP <<= 8;
resultIP |= octet & 0xFF;
}
return resultIP;
}
}

0 comments on commit 851588b

Please sign in to comment.