/** * Construct a RobotName from an address. The address must be well-formed as * described. @see RobotName * * @param address the address to parse. * @return robot name instance, or {@code null} if the address is not a robot * address. */ public static RobotName fromAddress(String address) { if (!isWellFormedAddress(address)) { return null; } int index = address.indexOf('@'); String id = address.substring(0, index); String domain = address.substring(index + 1); index = id.indexOf('#'); String version = ""; if (index >= 0) { version = id.substring(index + 1); id = address.substring(0, index); } index = id.indexOf('+'); String proxyFor = ""; if (index >= 0) { proxyFor = id.substring(index + 1); id = id.substring(0, index); } return new RobotName(id, domain, proxyFor, version); }
public void testWellFormedAddress() { assertFalse(RobotName.isWellFormedAddress("foo")); assertFalse(RobotName.isWellFormedAddress("bar.com")); assertFalse(RobotName.isWellFormedAddress("@bar.com")); assertFalse(RobotName.isWellFormedAddress("foo@")); assertTrue(RobotName.isWellFormedAddress("foo@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo#1@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo+wave#1@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo+wave+foo#1@bar.com")); }
public void testWellFormedAddress() { assertFalse(RobotName.isWellFormedAddress("foo")); assertFalse(RobotName.isWellFormedAddress("bar.com")); assertFalse(RobotName.isWellFormedAddress("@bar.com")); assertFalse(RobotName.isWellFormedAddress("foo@")); assertTrue(RobotName.isWellFormedAddress("foo@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo#1@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo+wave#1@bar.com")); assertTrue(RobotName.isWellFormedAddress("foo+wave+foo#1@bar.com")); }
/** * Appends proxyFor to the participant address. * * @param proxyFor the proxyFor. * @param participant the participant to apply the proxyFor. * @return new participant instance in the format * somebody+proxyFor@example.com. If proxyFor is null then just * returns unmodified participant. * @throws InvalidParticipantAddress if participant address and/or proxy are * invalid. */ public static ParticipantId toProxyParticipant(ParticipantId participant, String proxyFor) throws InvalidParticipantAddress { if (!Strings.isNullOrEmpty(proxyFor)) { RobotName robotName = RobotName.fromAddress(participant.getAddress()); robotName.setProxyFor(proxyFor); String robotAddress = robotName.toParticipantAddress(); if (!RobotName.isWellFormedAddress(robotAddress)) { throw new InvalidParticipantAddress(robotAddress, "is not a valid robot name, the proxy is likely to be wrong"); } return ParticipantId.of(robotName.toParticipantAddress()); } else { return participant; } }