use of inet.ipaddr.IPAddressString in project jcasbin by casbin.
the class BuiltInFunctions method ipMatch.
/**
* ipMatch determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be
* an IP address or a CIDR pattern. For example, "192.168.2.123" matches "192.168.2.0/24"
*
* @param ip1 the first argument.
* @param ip2 the second argument.
* @return whether ip1 matches ip2.
*/
public static boolean ipMatch(String ip1, String ip2) {
IPAddressString ipas1 = new IPAddressString(ip1);
try {
ipas1.validateIPv4();
} catch (AddressStringException e) {
e.printStackTrace();
throw new IllegalArgumentException("invalid argument: ip1 in IPMatch() function is not an IP address.");
}
IPAddressString ipas2 = new IPAddressString(ip2);
try {
ipas2.validate();
} catch (AddressStringException e) {
e.printStackTrace();
throw new IllegalArgumentException("invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR.");
}
if (ipas1.equals(ipas2)) {
return true;
}
IPAddress ipa1;
IPAddress ipa2;
try {
ipa1 = ipas1.toAddress();
ipa2 = ipas2.toAddress();
} catch (AddressStringException e) {
e.printStackTrace();
throw new IllegalArgumentException("invalid argument: ip1 or ip2 in IPMatch() function is not an IP address.");
}
Integer prefix = ipa2.getNetworkPrefixLength();
IPAddress mask = ipa2.getNetwork().getNetworkMask(prefix, false);
return ipa1.mask(mask).equals(ipa2.mask(mask));
}
use of inet.ipaddr.IPAddressString in project ArTEMiS by ls1intum.
the class HttpRequestUtils method getIpAddressFromRequest.
/**
* Extract Client IP Address from Http Request as IPAddress Object
*
* @param request Http Request
* @return IPAddress Object
*/
public static Optional<IPAddress> getIpAddressFromRequest(@NotNull HttpServletRequest request) {
final String ipString = getIpStringFromRequest(request);
final IPAddress ipAddress = new IPAddressString(ipString).getAddress();
return Optional.ofNullable(ipAddress);
}
use of inet.ipaddr.IPAddressString in project ArTEMiS by ls1intum.
the class ExamSessionIntegrationTest method storeIPv6OnStartExamSession_asStudent.
@Test
@WithMockUser(username = "student1", roles = "USER")
public void storeIPv6OnStartExamSession_asStudent() {
final var ipAddress = new IPAddressString("2001:db8:0:0:0:8a2e:370:7334").getAddress();
final Long id = examSessionService.startExamSession(studentExam1, null, null, null, ipAddress).getId();
final var examSessionById = examSessionRepository.findById(id);
assertThat(examSessionById).isPresent();
assertThat(examSessionById.get().getIpAddress().toCanonicalString()).isEqualTo("2001:db8::8a2e:370:7334");
}
use of inet.ipaddr.IPAddressString in project ArTEMiS by ls1intum.
the class ExamSessionIntegrationTest method storeIPv4OnStartExamSession_asStudent.
@Test
@WithMockUser(username = "student1", roles = "USER")
public void storeIPv4OnStartExamSession_asStudent() {
final var ipAddress = new IPAddressString("192.0.2.235").getAddress();
final Long id = examSessionService.startExamSession(studentExam1, null, null, null, ipAddress).getId();
final var examSessionById = examSessionRepository.findById(id);
assertThat(examSessionById).isPresent();
assertThat(examSessionById.get().getIpAddress().toCanonicalString()).isEqualTo("192.0.2.235");
}
use of inet.ipaddr.IPAddressString in project apiman by apiman.
the class JavaSysPropsProxySettings method determineQuote.
// Start or end star is already removed by this point.
private String determineQuote(String candidate) {
if (isIpAddress(candidate)) {
/*
* If the IP address has a mask (for example 1.2.3/8), then it seems the convention is to just
* to chop off the /slash part and replace it with .*
*/
IPAddressString ipString = new IPAddressString(candidate);
if (ipString.isPrefixed()) {
// Snip off the prefix segment replace it with .*
String withoutPrefix = StringUtils.removeEnd(candidate, "/" + ipString.getNetworkPrefixLength());
LOGGER.debug("Found prefixed IP address {0} -> {1}", ipString, withoutPrefix + ".*");
return Pattern.quote(withoutPrefix) + ".*";
}
}
return Pattern.quote(candidate);
}
Aggregations