use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.
the class Groups method putIpGroups.
/**
* Creates or updates the {@link IpGroup} named {@code groupName}.
* @param groupName Name of the group.
* @param dto data of the group.
* @return Response describing the created group or the error that prevented
* that group from being created.
*/
@PUT
@Path("ip/{groupName}")
public Response putIpGroups(@PathParam("groupName") String groupName, JsonObject dto) {
try {
if (groupName == null || groupName.trim().isEmpty()) {
return badRequest("Group name cannot be empty");
}
if (!legalGroupName.matcher(groupName).matches()) {
return badRequest("Group name can contain only letters, digits, and the chars '-' and '_'");
}
IpGroup grp = new JsonParser().parseIpGroup(dto);
grp.setGroupProvider(ipGroupPrv);
grp.setPersistedGroupAlias(groupName);
grp = ipGroupPrv.store(grp);
return created("/groups/ip/" + grp.getPersistedGroupAlias(), json(grp));
} catch (Exception e) {
logger.log(Level.WARNING, "Error while storing a new IP group: " + e.getMessage(), e);
return error(Response.Status.INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
}
}
use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.
the class JsonParserTest method testIpGroupRoundTrip.
@Test
public void testIpGroupRoundTrip() {
IpGroup original = new IpGroup();
original.setDescription("Ip group description");
original.setDisplayName("Test-ip-group");
original.setId(42l);
original.setPersistedGroupAlias("test-ip-group");
original.setGroupProvider(new IpGroupProvider(null));
original.add(IpAddressRange.make(IpAddress.valueOf("1.2.1.1"), IpAddress.valueOf("1.2.1.10")));
original.add(IpAddressRange.make(IpAddress.valueOf("1.1.1.1"), IpAddress.valueOf("1.1.1.1")));
original.add(IpAddressRange.make(IpAddress.valueOf("1:2:3::4:5"), IpAddress.valueOf("1:2:3::4:5")));
original.add(IpAddressRange.make(IpAddress.valueOf("1:2:3::3:ff"), IpAddress.valueOf("1:2:3::3:5")));
JsonObject serialized = JsonPrinter.json(original).build();
System.out.println(serialized.toString());
IpGroup parsed = new JsonParser().parseIpGroup(serialized);
assertEquals(original, parsed);
}
use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.
the class JsonParserTest method testIpGroupRoundTrip_singleIpv6Address.
@Test
public void testIpGroupRoundTrip_singleIpv6Address() {
IpGroup original = new IpGroup();
original.setDescription("Ip group description");
original.setDisplayName("Test-ip-group");
original.setId(42l);
original.setPersistedGroupAlias("test-ip-group");
original.setGroupProvider(new IpGroupProvider(null));
original.add(IpAddressRange.make(IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61")));
JsonObject serialized = JsonPrinter.json(original).build();
System.out.println(serialized.toString());
IpGroup parsed = new JsonParser().parseIpGroup(serialized);
assertEquals(original, parsed);
assertTrue(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce60"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce62"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe47:ce61"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0af:fe48:ce61"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe79::22c9:d0ff:fe48:ce61"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("2.1.1.1"))));
}
use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.
the class JsonParserTest method testIpGroupRoundTrip_singleIpv4Address.
@Test
public void testIpGroupRoundTrip_singleIpv4Address() {
IpGroup original = new IpGroup();
original.setDescription("Ip group description");
original.setDisplayName("Test-ip-group");
original.setId(42l);
original.setPersistedGroupAlias("test-ip-group");
original.setGroupProvider(new IpGroupProvider(null));
original.add(IpAddressRange.make(IpAddress.valueOf("1.1.1.1"), IpAddress.valueOf("1.1.1.1")));
JsonObject serialized = JsonPrinter.json(original).build();
System.out.println(serialized.toString());
IpGroup parsed = new JsonParser().parseIpGroup(serialized);
assertEquals(original, parsed);
assertTrue(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.1"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.2"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.2.1"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.0"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.250"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.2.1.1"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("2.1.1.1"))));
assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"))));
}
use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.
the class Groups method postIpGroup.
/**
* Creates a new {@link IpGroup}. The name of the group is based on the
* {@code alias:} field, but might be changed to ensure uniqueness.
* @param dto
* @return Response describing the created group or the error that prevented
* that group from being created.
*/
@POST
@Path("ip")
public Response postIpGroup(JsonObject dto) {
try {
IpGroup grp = new JsonParser().parseIpGroup(dto);
grp.setGroupProvider(ipGroupPrv);
grp.setPersistedGroupAlias(ipGroupPrv.findAvailableName(grp.getPersistedGroupAlias() == null ? "ipGroup" : grp.getPersistedGroupAlias()));
grp = ipGroupPrv.store(grp);
return created("/groups/ip/" + grp.getPersistedGroupAlias(), json(grp));
} catch (Exception e) {
logger.log(Level.WARNING, "Error while storing a new IP group: " + e.getMessage(), e);
return error(Response.Status.INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
}
}
Aggregations