throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e);
throw new InternalServerErrorException(e.getLocalizedMessage(), e);
@Override public ConversationMemorySnapshot readConversationLog(String conversationId) { RuntimeUtilities.checkNotNull(conversationId, "conversationId"); try { return conversationMemoryStore.loadConversationMemorySnapshot(conversationId); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(String.format("Conversation (%s) could not be found", conversationId)); } }
log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(); } catch (IResourceStore.ResourceNotFoundException e) {
log.error(e.getMessage(), e); throw new InternalServerErrorException(e.getMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NoLogWebApplicationException(e);
throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e);
log.error(e.getMessage(), e); throw new InternalServerErrorException(e.getMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e);
public Integer set(String id, Integer version, T content) throws ResourceStoreException, ResourceNotFoundException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); RuntimeUtilities.checkNotNull(content, "content"); IResourceStorage.IResource<T> resource = resourceStorage.read(id, version); try { if (resource == null) { IResourceStorage.IHistoryResource historyLatest = resourceStorage.readHistoryLatest(id); if (historyLatest == null || historyLatest.isDeleted() || version > historyLatest.getVersion()) { throw createResourceNotFoundException(id, version); } //it's a update request for a historized resource, so we update the history resource IResourceStorage.IResource<T> updatedResource = resourceStorage.newResource(id, version, content); IResourceStorage.IHistoryResource<T> updatedHistorizedResource = resourceStorage.newHistoryResourceFor(updatedResource, false); resourceStorage.store(updatedHistorizedResource); return version; } else { //it's a update request for the current resource, so we update the current resource IResourceStorage.IResource<T> updatedResource = resourceStorage.newResource(id, version, content); resourceStorage.store(updatedResource); return version; } } catch (IOException e) { throw new ResourceStoreException(e.getLocalizedMessage(), e); } }
@Override public RegularDictionaryConfiguration read(String id, Integer version, String filter, String order, Integer index, Integer limit) throws ResourceNotFoundException, ResourceStoreException { RuntimeUtilities.checkNotNull(filter, "filter"); RuntimeUtilities.checkNotNull(order, "order"); RuntimeUtilities.checkNotNull(index, "index"); RuntimeUtilities.checkNotNull(limit, "limit"); ResultManipulator<RegularDictionaryConfiguration.WordConfiguration> wordManipulator; ResultManipulator<RegularDictionaryConfiguration.PhraseConfiguration> phraseManipulator; RegularDictionaryConfiguration regularDictionary = regularDictionaryResourceStore.read(id, version); wordManipulator = new ResultManipulator<>(regularDictionary.getWords(), RegularDictionaryConfiguration.WordConfiguration.class); phraseManipulator = new ResultManipulator<>(regularDictionary.getPhrases(), RegularDictionaryConfiguration.PhraseConfiguration.class); try { wordManipulator.filterEntities(filter); phraseManipulator.filterEntities(filter); } catch (ResultManipulator.FilterEntriesException e) { throw new ResourceStoreException(e.getLocalizedMessage(), e); } wordManipulator.sortEntities(WORD_COMPARATOR, order); phraseManipulator.sortEntities(PHRASE_COMPARATOR, order); wordManipulator.limitEntities(index, limit); phraseManipulator.limitEntities(index, limit); return regularDictionary; }
@Override public boolean isUserAuthorized(String resourceId, Integer version, URI user, IAuthorization.Type authorizationType) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException { try { IPermissions permissions = permissionStore.readFilteredPermissions(resourceId); List<IAuthorization.Type> allowedAuthorizationTypes = getImplicitAuthorizationTypes(authorizationType); for (IAuthorization.Type type : allowedAuthorizationTypes) { AuthorizedSubjects authorizedSubjects = permissions.getPermissions().get(type); if (authorizedSubjects != null) { List<AuthorizedUser> authorizedUsers = PermissionUtilities.mergeAuthorizedSubjects(groupStore, authorizedSubjects); authorizedSubjects.getUsers().clear(); authorizedSubjects.getUsers().addAll(authorizedUsers); PermissionUtilities.filterAuthorizedSubjectsByUser(user, authorizedSubjects.getUsers()); for (AuthorizedUser authorizedUser : authorizedSubjects.getUsers()) { List<Integer> versions = authorizedUser.getVersions(); if (versions == null || versions.contains(version)) { return true; } } } } return false; } catch (IAuthorization.UnrecognizedAuthorizationTypeException e) { throw new IResourceStore.ResourceStoreException(e.getMessage(), e); } }
@Override public void autoDeployBots() throws AutoDeploymentException { try { log.info("Starting auto deployment of bots..."); deploymentStore.readDeploymentInfos().stream().filter( deploymentInfo -> deploymentInfo.getDeploymentStatus() == DeploymentInfo.DeploymentStatus.deployed). forEach(deploymentInfo -> { Environment environment = Environment.valueOf(deploymentInfo.getEnvironment().toString()); String botId = deploymentInfo.getBotId(); Integer botVersion = deploymentInfo.getBotVersion(); try { botFactory.deployBot(environment, botId, botVersion, null); } catch (ServiceException | IllegalAccessException | IllegalArgumentException e) { String message = "Error while auto deploying bot (environment=%s, botId=%s, version=%s)!\n"; log.error(String.format(message, environment, botId, botVersion)); log.error(e.getLocalizedMessage(), e); } }); log.info("Finished auto deployment of bots."); } catch (IResourceStore.ResourceStoreException e) { throw new AutoDeploymentException(e.getLocalizedMessage(), e); } } }
@Override public void patchDescriptor(String id, Integer version, PatchInstruction<SimpleTestCaseDescriptor> patchInstruction) { try { TestCaseDescriptor testCaseDescriptor = testCaseDescriptorStore.readDescriptor(id, version); SimpleTestCaseDescriptor simpleTestCaseDescriptor = patchInstruction.getDocument(); if (patchInstruction.getOperation().equals(PatchInstruction.PatchOperation.SET)) { testCaseDescriptor.setName(simpleTestCaseDescriptor.getName()); testCaseDescriptor.setDescription(simpleTestCaseDescriptor.getDescription()); } else { testCaseDescriptor.setName(""); testCaseDescriptor.setDescription(""); } testCaseDescriptorStore.setDescriptor(id, version, testCaseDescriptor); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e); throw new NotFoundException(e.getLocalizedMessage(), e); } }
@Override public UserConversation readUserConversation(String intent, String userId) { try { String cacheKey = calculateCacheKey(intent, userId); UserConversation userConversation = userConversationCache.get(cacheKey); if (userConversation == null) { userConversation = userConversationStore.readUserConversation(intent, userId); if (userConversation != null) { userConversationCache.put(cacheKey, userConversation); } } if (userConversation == null) { String message = "UserConversation with intent=%s and userId=%s does not exist."; message = String.format(message, intent, userId); throw new NotFoundException(message); } return userConversation; } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage()); } }
@Override public T read(String id, Integer version) throws IResourceStore.ResourceNotFoundException, IResourceStore.ResourceStoreException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); IResourceStorage.IResource<T> current = resourceStorage.read(id, version); if (current == null) { IResourceStorage.IHistoryResource historyResource = resourceStorage.readHistory(id, version); if (historyResource == null || historyResource.isDeleted()) { throw createResourceNotFoundException(id, version); } current = historyResource; } try { return current.getData(); } catch (IOException e) { String message = "Unable to deserialize resource (id=%s, version=%s)"; message = String.format(message, id, version); throw new IResourceStore.ResourceStoreException(message, e); } }
@Override public Response createTestCase(String id) { try { ConversationMemorySnapshot conversationMemorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(id); TestCase testCase = new TestCase(); testCase.setBotId(conversationMemorySnapshot.getBotId()); testCase.setBotVersion(conversationMemorySnapshot.getBotVersion()); testCase.setTestCaseState(TestCaseState.SUCCESS); testCase.setActual(conversationMemorySnapshot); testCase.setExpected(conversationMemorySnapshot); IResourceStore.IResourceId resourceId = testCaseStore.create(testCase); URI createdUri = RestUtilities.createURI(resourceURI, resourceId.getId(), versionQueryParam, 0); return Response.created(createdUri).build(); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e); } catch (IResourceStore.ResourceNotFoundException e) { log.debug(e.getLocalizedMessage(), e); throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } }
@Override public void patchDescriptor(String id, Integer version, PatchInstruction<DocumentDescriptor> patchInstruction) { try { DocumentDescriptor documentDescriptor = documentDescriptorStore.readDescriptor(id, version); DocumentDescriptor simpleDescriptor = patchInstruction.getDocument(); if (patchInstruction.getOperation().equals(PatchInstruction.PatchOperation.SET)) { documentDescriptor.setName(simpleDescriptor.getName()); documentDescriptor.setDescription(simpleDescriptor.getDescription()); } else { documentDescriptor.setName(""); documentDescriptor.setDescription(""); } documentDescriptorStore.setDescriptor(id, version, documentDescriptor); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e.getLocalizedMessage(), e); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } } }
private RegularDictionaryConfiguration patchDocument(RegularDictionaryConfiguration currentRegularDictionaryConfiguration, PatchInstruction<RegularDictionaryConfiguration>[] patchInstructions) throws IResourceStore.ResourceStoreException { for (PatchInstruction<RegularDictionaryConfiguration> patchInstruction : patchInstructions) { RegularDictionaryConfiguration regularDictionaryConfigurationPatch = patchInstruction.getDocument(); switch (patchInstruction.getOperation()) { case SET: currentRegularDictionaryConfiguration.getWords().removeAll(regularDictionaryConfigurationPatch.getWords()); currentRegularDictionaryConfiguration.getWords().addAll(regularDictionaryConfigurationPatch.getWords()); currentRegularDictionaryConfiguration.getPhrases().removeAll(regularDictionaryConfigurationPatch.getPhrases()); currentRegularDictionaryConfiguration.getPhrases().addAll(regularDictionaryConfigurationPatch.getPhrases()); break; case DELETE: currentRegularDictionaryConfiguration.getWords().removeAll(regularDictionaryConfigurationPatch.getWords()); currentRegularDictionaryConfiguration.getPhrases().removeAll(regularDictionaryConfigurationPatch.getPhrases()); break; default: throw new IResourceStore.ResourceStoreException("Patch operation must be either SET or DELETE!"); } } return currentRegularDictionaryConfiguration; }
@Override public String storeConversationMemorySnapshot(ConversationMemorySnapshot snapshot) throws IResourceStore.ResourceStoreException { try { String json = documentBuilder.toString(snapshot); Document document = Document.parse(json); document.remove("conversationId"); if (snapshot.getConversationId() != null) { document.put(OBJECT_ID, new ObjectId(snapshot.getConversationId())); conversationCollection.updateOne(new Document(OBJECT_ID, new ObjectId(snapshot.getConversationId())), new Document("$set", document), new UpdateOptions().upsert(true)); } else { conversationCollection.insertOne(document); } return document.get(OBJECT_ID).toString(); } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } }
@Override public Integer update(String id, Integer version, T content) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceModifiedException, IResourceStore.ResourceNotFoundException { RuntimeUtilities.checkNotNull(id, "id"); RuntimeUtilities.checkNotNull(version, "version"); RuntimeUtilities.checkNotNull(content, "content"); IResourceStorage.IResource resource = resourceStorage.read(id, version); checkIfFoundAndLatest(id, version, resource); IResourceStorage.IHistoryResource history = resourceStorage.newHistoryResourceFor(resource, false); resourceStorage.store(history); try { Integer newVersion = resource.getVersion() + 1; IResourceStorage.IResource newResource = resourceStorage.newResource(resource.getId(), newVersion, content); resourceStorage.store(newResource); return newVersion; } catch (IOException e) { throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e); } }
@Override public OutputConfigurationSet read(String id, Integer version, String filter, String order, Integer index, Integer limit) throws ResourceNotFoundException, ResourceStoreException { RuntimeUtilities.checkNotNull(filter, "filter"); RuntimeUtilities.checkNotNull(order, "order"); RuntimeUtilities.checkNotNull(index, "index"); RuntimeUtilities.checkNotNull(limit, "limit"); OutputConfigurationSet outputConfigurationSet = outputResourceStore.read(id, version); ResultManipulator<OutputConfiguration> outputManipulator; outputManipulator = new ResultManipulator<>(outputConfigurationSet.getOutputSet(), OutputConfiguration.class); try { outputManipulator.filterEntities(filter); } catch (ResultManipulator.FilterEntriesException e) { throw new ResourceStoreException(e.getLocalizedMessage(), e); } outputManipulator.sortEntities(OUTPUT_COMPARATOR, order); outputManipulator.limitEntities(index, limit); return outputConfigurationSet; }