use of org.eclipse.kapua.service.account.AccountService in project kapua by eclipse.
the class AccountServiceTest method testCreate.
@Test
public void testCreate() throws Exception {
// KapuaPeid accountPeid = KapuaEidGenerator.generate();//
KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
KapuaLocator locator = KapuaLocator.getInstance();
long accountSerial = (new Date()).getTime();
AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.create(accountCreator);
//
// Account asserts
assertNotNull(account);
assertNotNull(account.getId());
assertNotNull(account.getId().getId());
assertTrue(account.getOptlock() >= 0);
assertTrue(account.getScopeId().equals(scopeId));
assertTrue(account.getName().equals(accountCreator.getOrganizationName()));
assertNotNull(account.getOrganization());
assertTrue(account.getOrganization().getEmail().equals(accountCreator.getOrganizationEmail()));
assertNotNull(account.getCreatedOn());
assertNotNull(account.getCreatedBy());
assertNotNull(account.getModifiedOn());
assertNotNull(account.getModifiedBy());
}
use of org.eclipse.kapua.service.account.AccountService in project kapua by eclipse.
the class AccountServiceTest method testUpdate.
@Test
public void testUpdate() throws Exception {
// KapuaPeid accountPeid = KapuaEidGenerator.generate();//
KapuaId scopeId = new KapuaEid(BigInteger.valueOf(1));
KapuaLocator locator = KapuaLocator.getInstance();
long accountSerial = (new Date()).getTime();
AccountCreator accountCreator = this.getTestAccountCreator(scopeId, accountSerial);
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.create(accountCreator);
account = accountService.find(account.getScopeId(), account.getId());
Organization org = account.getOrganization();
org.setAddressLine1("5th evenue, NY");
account.setOrganization(org);
Account accountUpd = accountService.update(account);
//
// Account asserts
assertNotNull(accountUpd);
assertNotNull(accountUpd.getId());
assertNotNull(accountUpd.getId().getId());
assertTrue(accountUpd.getOptlock() >= 0);
assertTrue(accountUpd.getId().equals(account.getId()));
assertTrue(accountUpd.getScopeId().equals(account.getScopeId()));
assertTrue(accountUpd.getName().equals(account.getName()));
assertNotNull(accountUpd.getOrganization());
assertTrue(accountUpd.getOrganization().getAddressLine1().equals(org.getAddressLine1()));
assertNotNull(accountUpd.getCreatedOn().equals(account.getCreatedOn()));
assertNotNull(accountUpd.getCreatedBy().equals(account.getCreatedBy()));
assertNotNull(account.getModifiedOn());
assertNotNull(account.getModifiedBy());
}
use of org.eclipse.kapua.service.account.AccountService in project kapua by eclipse.
the class TranslatorAppCommandKapuaKura method translate.
@Override
public KuraRequestMessage translate(CommandRequestMessage kapuaMessage) throws KapuaException {
//
// Kura channel
KapuaLocator locator = KapuaLocator.getInstance();
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.find(kapuaMessage.getScopeId());
DeviceRegistryService deviceService = locator.getService(DeviceRegistryService.class);
Device device = deviceService.find(kapuaMessage.getScopeId(), kapuaMessage.getDeviceId());
KuraRequestChannel kuraRequestChannel = translate(kapuaMessage.getChannel());
kuraRequestChannel.setScope(account.getName());
kuraRequestChannel.setClientId(device.getClientId());
//
// Kura payload
KuraRequestPayload kuraPayload = translate(kapuaMessage.getPayload());
// return Kura Message
return new KuraRequestMessage(kuraRequestChannel, kapuaMessage.getReceivedOn(), kuraPayload);
}
use of org.eclipse.kapua.service.account.AccountService in project kapua by eclipse.
the class TranslatorAppBundleKuraKapua method translate.
@Override
public BundleResponseMessage translate(KuraResponseMessage kuraMessage) throws KapuaException {
//
// Kura channel
KapuaLocator locator = KapuaLocator.getInstance();
AccountService accountService = locator.getService(AccountService.class);
Account account = accountService.findByName(kuraMessage.getChannel().getScope());
BundleResponseChannel bundleResponseChannel = translate(kuraMessage.getChannel());
//
// Kura payload
BundleResponsePayload responsePayload = translate(kuraMessage.getPayload());
//
// Kura Message
BundleResponseMessage kapuaMessage = new BundleResponseMessage();
kapuaMessage.setScopeId(account.getId());
kapuaMessage.setChannel(bundleResponseChannel);
kapuaMessage.setPayload(responsePayload);
kapuaMessage.setCapturedOn(kuraMessage.getPayload().getTimestamp());
kapuaMessage.setSentOn(kuraMessage.getPayload().getTimestamp());
kapuaMessage.setReceivedOn(kuraMessage.getTimestamp());
kapuaMessage.setResponseCode(TranslatorKuraKapuaUtils.translate((Integer) kuraMessage.getPayload().getMetrics().get(ResponseMetrics.RESP_METRIC_EXIT_CODE.getValue())));
// Return Kapua Message
return kapuaMessage;
}
use of org.eclipse.kapua.service.account.AccountService in project kapua by eclipse.
the class KapuaAuthenticatingRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//
// Extract credentials
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String tokenUsername = token.getUsername();
// char[] tokenPassword = token.getPassword();
//
// Get Services
KapuaLocator locator;
UserService userService;
AccountService accountService;
CredentialService credentialService;
try {
locator = KapuaLocator.getInstance();
userService = locator.getService(UserService.class);
accountService = locator.getService(AccountService.class);
credentialService = locator.getService(CredentialService.class);
} catch (KapuaRuntimeException kre) {
throw new ShiroException("Error while getting services!", kre);
}
//
// Get the associated user by name
final User user;
try {
user = KapuaSecurityUtils.doPriviledge(new Callable<User>() {
@Override
public User call() throws Exception {
return userService.findByName(tokenUsername);
}
});
} catch (Exception e) {
// to preserve the original exception message (if possible)
if (e instanceof AuthenticationException) {
throw (AuthenticationException) e;
} else {
throw new ShiroException("Error while find user!", e);
}
}
// Check existence
if (user == null) {
throw new UnknownAccountException();
}
// Check disabled
if (UserStatus.DISABLED.equals(user.getStatus())) {
throw new DisabledAccountException();
}
//
// Find account
final Account account;
try {
account = KapuaSecurityUtils.doPriviledge(new Callable<Account>() {
@Override
public Account call() throws Exception {
return accountService.find(user.getScopeId());
}
});
} catch (Exception e) {
// to preserve the original exception message (if possible)
if (e instanceof AuthenticationException) {
throw (AuthenticationException) e;
} else {
throw new ShiroException("Error while find account!", e);
}
}
// Check existence
if (account == null) {
throw new UnknownAccountException();
}
//
// Find credentials
// FIXME: manage multiple credentials and multiple credentials type
Credential credential = null;
try {
credential = KapuaSecurityUtils.doPriviledge(new Callable<Credential>() {
@Override
public Credential call() throws Exception {
CredentialListResult credentialList = credentialService.findByUserId(user.getScopeId(), user.getId());
// TODO may be better to filter by credential type?
if (credentialList != null && !credentialList.isEmpty()) {
return credentialList.getItem(0);
} else {
throw new UnknownAccountException();
}
}
});
} catch (Exception e) {
if (e instanceof AuthenticationException) {
throw (AuthenticationException) e;
} else {
throw new ShiroException("Error while find credentials!", e);
}
}
//
// BuildAuthenticationInfo8
KapuaSimpleAuthenticationInfo info = new KapuaSimpleAuthenticationInfo(user, credential, account, getName());
return info;
}
Aggregations