use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method updateManagedItem.
@Override
@JdbcTransaction
public <T extends ItemBase> T updateManagedItem(ProjectId project, T item) throws RepositoryException {
Class<T> itemClass = (Class<T>) item.getClass();
DbHelper db = new DbHelper(itemClass, project);
try {
ManagedItemId itemId = new ManagedItemId(item.getId());
ModelClass<T> modelClass = serviceProviderDirectory.getModelClass(itemClass);
int projectId = db.mapToValue(project);
int modelId = db.mapToValue(modelClass.getItemType());
int serviceId = db.mapToValue(modelClass.getServiceType());
ItemEntity rs = db.queries.findItem(serviceId, modelId, projectId, itemId.getKey());
if (rs == null) {
throw new RepositoryException("Item not found");
}
byte[] secretData = rs.secret;
CryptoKey itemSecret;
if (secretData == null) {
itemSecret = FathomdbCrypto.generateKey();
secretData = itemSecrets.encodeItemSecret(itemSecret);
db.updateSecret(itemId, secretData);
} else {
itemSecret = item.secret.getSecret();
}
byte[] data = serialize(item, itemSecret);
db.updateItem(itemId, data, item.state);
// Note: we can't change tags here (that needs a separate call to updateTags)
SecretProvider secretProvider = SecretProvider.forKey(itemSecret);
boolean fetchTags = true;
return fetchItem(db, modelClass.getServiceType(), modelClass.getItemType(), project, itemId, itemClass, secretProvider, fetchTags);
} catch (SQLException e) {
throw new RepositoryException("Error running query", e);
} finally {
db.close();
}
}
use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.
the class JdbcManagedItemRepository method listAll.
@Override
@JdbcTransaction
public List<ItemBase> listAll(ProjectId project, Filter filter, SecretProvider secretProvider) throws RepositoryException {
DbHelper db = new DbHelper(project);
try {
log.debug("listAll with filter: {}", filter);
// TODO: Use this logic for item selection as well
List<Tag> requiredTags = filter.getRequiredTags();
JoinedQueryResult result;
if (!requiredTags.isEmpty()) {
Tag requiredTag = requiredTags.get(0);
int projectId = db.mapToValue(project);
result = db.queries.listAllItemsWithTag(projectId, projectId, requiredTag.getKey(), requiredTag.getValue());
} else {
log.warn("Unable to optimize filter; selecting all items. Filter={}", filter);
result = db.listAllItems();
}
List<ItemBase> items = mapItemsAndTags(project, secretProvider, db, result);
return applyFilter(items, filter);
} catch (SQLException e) {
throw new RepositoryException("Error fetching items", e);
} finally {
db.close();
}
}
use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.
the class KeystoneRepositoryAuthenticator method getUserFromToken.
@Override
public UserEntity getUserFromToken(String userIdString, byte[] tokenSecret) throws AuthenticatorException {
int userId;
try {
userId = Integer.parseInt(userIdString);
} catch (NumberFormatException e) {
throw new AuthenticatorException("Invalid user id", e);
}
if (tokenSecret.length < 1) {
throw new IllegalArgumentException();
}
CryptoKey userSecret = authenticationSecrets.decryptSecretFromToken(tokenSecret);
if (userSecret == null) {
throw new AuthenticatorException("Authentication timed out");
}
UserEntity user;
try {
user = repository.findUserById(userId);
} catch (RepositoryException e) {
throw new AuthenticatorException("Error while authenticating user", e);
}
user.unlock(userSecret);
if (user.isLocked()) {
return null;
}
return user;
}
use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.
the class TokenHelpers method buildAccess.
public Access buildAccess(UserEntity user) {
byte[] tokenSecret = authSecrets.buildToken(user.getUserSecret());
TokenInfo token = buildToken("" + user.getId(), tokenSecret);
Access access = new Access();
// response.access.serviceCatalog = serviceMapper.getServices(userInfo,
// project);
access.token = new Token();
access.token.expires = token.expiration;
access.token.id = tokenService.encodeToken(token);
access.projects = Lists.newArrayList();
try {
for (ProjectEntity project : userAuthenticator.listProjects(user)) {
access.projects.add(project.getName());
}
} catch (RepositoryException e) {
log.warn("Error while listing projects for user: " + user.key, e);
throw new IllegalStateException("Error listing projects for user", e);
}
return access;
}
use of org.platformlayer.RepositoryException in project platformlayer by platformlayer.
the class SimpleOperationQueue method jobFinished.
// @Override
// public JobExecutionList listRecentExecutions(ProjectId projectId) {
// JobExecutionList ret = JobExecutionList.create();
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// ret.add(job.getJobExecution());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData jobExecution : recentJobs) {
// if (!jobExecution.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(jobExecution);
// }
// }
//
// return ret;
// }
// @Override
// public List<JobData> listRecentJobs(ProjectId projectId) {
// List<JobData> ret = Lists.newArrayList();
//
// synchronized (activeJobs) {
// for (ActiveJobExecution job : activeJobs.values()) {
// if (!Objects.equal(job.getTargetItemKey().getProject(), projectId)) {
// continue;
// }
// JobExecutionData execution = job.getJobExecution();
// ret.add(execution.getJob());
// }
// }
//
// synchronized (recentJobs) {
// for (JobExecutionData job : recentJobs) {
// if (!job.getJobKey().getProject().equals(projectId)) {
// continue;
// }
// ret.add(job.getJob());
// }
// }
//
// return ret;
// }
// private static final int RECENT_JOB_COUNT = 100;
@Override
public void jobFinished(JobExecutionData jobExecutionData, JobState state, JobLogger logger) throws OpsException {
PlatformLayerKey jobKey = jobExecutionData.getJobKey();
String executionId = jobExecutionData.getExecutionId();
Date startTime = jobExecutionData.getStartedAt();
Date endTime = new Date();
synchronized (activeJobs) {
activeJobs.remove(jobKey);
}
try {
String logCookie = jobLogStore.saveJobLog(jobKey, executionId, startTime, logger);
jobRepository.recordJobEnd(jobKey, executionId, endTime, state, logCookie);
} catch (RepositoryException e) {
throw new OpsException("Error writing job to repository", e);
} catch (Exception e) {
throw new OpsException("Error writing job log", e);
}
}
Aggregations