@Override public void putAccount(AccountData account) { DBObject object = getDBObjectForParticipant(account.getId()); if (account.isHuman()) { object.put(ACCOUNT_HUMAN_DATA_FIELD, humanToObject(account.asHuman())); } else if (account.isRobot()) { object.put(ACCOUNT_ROBOT_DATA_FIELD, robotToObject(account.asRobot())); } else { throw new IllegalStateException("Account is neither a human nor a robot"); } getAccountCollection().save(object); }
/** * Ensures that the account belongs to a robot. * * @param existingAccount the account to check. * @throws RobotRegistrationException if the account is not robot. */ private void throwExceptionIfNotRobot(AccountData existingAccount) throws RobotRegistrationException { if (!existingAccount.isRobot()) { throw new RobotRegistrationException(existingAccount.getId().getAddress() + " is not a robot account!"); } }
public void testReRegisterSucceedsOnExistingRobotAccount() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.registerOrUpdate(ROBOT_ID, OTHER_LOCATION); verify(accountStore).removeAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); assertTrue(unregisteredAccountData.isRobot()); RobotAccountData robotAccountData = unregisteredAccountData.asRobot(); // Remove the last '/'. assertEquals(OTHER_LOCATION.substring(0, OTHER_LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }
if (account == null || !account.isRobot()) { throw new InvalidRequestException("Can't exectute robot.notify for unknown robot " + robotAccountId); RobotAccountData robotAccountData = account.asRobot(); RobotCapabilities capabilities = robotAccountData.getCapabilities(); if (capabilities != null && capabilitiesHash.equals(capabilities.getCapabilitiesHash())) { robotAccountData = connector.fetchCapabilities(robotAccountData, ""); } catch (CapabilityFetchException e) { LOG.fine("Unable to retrieve capabilities for " + account.getId(), e); context.constructErrorResponse(operation, "Unable to retrieve new capabilities"); return;
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { ParticipantId participant = sessionManager.getLoggedInUser(req.getSession(false)); if (participant == null) { resp.setStatus(HttpServletResponse.SC_FORBIDDEN); return; } String locale = URLDecoder.decode(req.getParameter("locale"), "UTF-8"); AccountData account = accountStore.getAccount(participant); HumanAccountData humanAccount; if (account != null) { humanAccount = account.asHuman(); } else { humanAccount = new HumanAccountDataImpl(participant); } humanAccount.setLocale(locale); accountStore.putAccount(humanAccount); } catch (PersistenceException ex) { throw new IOException(ex); } } }
@Override public void putAccount(AccountData account) { accounts.put(account.getId(), account); }
@Override public RobotAccountData unregister(ParticipantId robotId) throws RobotRegistrationException, PersistenceException { Preconditions.checkNotNull(robotId); AccountData accountData = accountStore.getAccount(robotId); if (accountData == null) { return null; } throwExceptionIfNotRobot(accountData); RobotAccountData robotAccount = accountData.asRobot(); removeRobotAccount(robotAccount); return robotAccount; }
return; if (account == null || !account.isRobot()) { LOG.info("The account for robot named " + participant + " does not exist"); resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED); new OAuthConsumer(null, participant.getAddress(), account.asRobot().getConsumerSecret(), oauthServiceProvider); OAuthAccessor accessor = new OAuthAccessor(consumer);
String locale = account.asHuman().getLocale(); if (locale != null) { String requestLocale = UrlParameters.getParameters(request.getQueryString()).get("locale");
@Override public void putAccount(AccountData account) { accountStore.put(account.getId(), account); }
@Override public RobotAccountData registerOrUpdate(ParticipantId robotId, String location) throws RobotRegistrationException, PersistenceException { Preconditions.checkNotNull(robotId); Preconditions.checkNotNull(location); Preconditions.checkArgument(!location.isEmpty()); AccountData account = accountStore.getAccount(robotId); if (account != null) { throwExceptionIfNotRobot(account); RobotAccountData robotAccount = account.asRobot(); if (robotAccount.getUrl().equals(location)) { return robotAccount; } else { removeRobotAccount(robotAccount); } } return registerRobot(robotId, location); }
/** * Serialize {@link AccountData} into {@link ProtoAccountData}. */ public static ProtoAccountData serialize(AccountData account) { Preconditions.checkNotNull(account, "account is null"); Preconditions.checkArgument(account.isHuman() || account.isRobot(), "account is neither a human or robot account!"); ProtoAccountData.Builder builder = ProtoAccountData.newBuilder(); builder.setAccountId(account.getId().getAddress()); if (account.isHuman()) { builder.setAccountType(AccountDataType.HUMAN_ACCOUNT); builder.setHumanAccountData(serialize(account.asHuman())); } else if (account.isRobot()) { builder.setAccountType(AccountDataType.ROBOT_ACCOUNT); builder.setRobotAccountData(serialize(account.asRobot())); } return builder.build(); }
public void testReRegisterSucceedsOnExistingRobotAccount() throws PersistenceException, RobotRegistrationException { when(accountStore.getAccount(ROBOT_ID)).thenReturn(accountData); AccountData unregisteredAccountData = registrar.registerOrUpdate(ROBOT_ID, OTHER_LOCATION); verify(accountStore).removeAccount(ROBOT_ID); verify(accountStore).putAccount(any(RobotAccountData.class)); assertTrue(unregisteredAccountData.isRobot()); RobotAccountData robotAccountData = unregisteredAccountData.asRobot(); // Remove the last '/'. assertEquals(OTHER_LOCATION.substring(0, OTHER_LOCATION.length() - 1), robotAccountData.getUrl()); assertEquals(ROBOT_ID, robotAccountData.getId()); assertEquals(CONSUMER_TOKEN, robotAccountData.getConsumerSecret()); }
public void testRegisterNewUserEnabled() throws Exception { attemptToRegister(req, resp, "foo@example.com", "internet", false); verify(resp).setStatus(HttpServletResponse.SC_OK); ParticipantId participantId = ParticipantId.ofUnsafe("foo@example.com"); AccountData account = store.getAccount(participantId); assertNotNull(account); assertTrue(account.asHuman().getPasswordDigest().verify("internet".toCharArray())); verify(welcomeBot).greet(eq(participantId)); }
@Override public void putAccount(AccountData account) throws PersistenceException { synchronized (accounts) { Preconditions.checkNotNull(account); writeAccount(account); accounts.put(account.getId(), account); } }
account = getAccountStore() .getAccount(ParticipantId.ofUnsafe(getRobotId() + "@" + getWaveDomain())).asRobot(); } catch (PersistenceException e) { LOG.log(Level.WARNING, "Cannot fetch account data for robot id: " + getRobotId(), e);
public void testRegisterNewUserEnabled() throws Exception { attemptToRegister(req, resp, "foo@example.com", "internet", false); verify(resp).setStatus(HttpServletResponse.SC_OK); ParticipantId participantId = ParticipantId.ofUnsafe("foo@example.com"); AccountData account = store.getAccount(participantId); assertNotNull(account); assertTrue(account.asHuman().getPasswordDigest().verify("internet".toCharArray())); verify(welcomeBot).greet(eq(participantId)); }
private void writeAccount(AccountData account) throws PersistenceException { File accountFile = new File(participantIdToFileName(account.getId())); OutputStream file = null; try { file = new FileOutputStream(accountFile); ProtoAccountData data = ProtoAccountDataSerializer.serialize(account); file.write(data.toByteArray()); file.flush(); } catch (IOException e) { LOG.severe("Failed to write account data to file: " + accountFile.getAbsolutePath(), e); throw new PersistenceException(e); } finally { FileUtils.closeAndIgnoreException(file, accountFile, LOG); } } }