IResourceStore.IResourceId resourceId = RestUtilities.extractResourceId(uri); if (resourceId.getId() != null) { Principal principal = securityContext.getUserPrincipal(); URI user = getUser(principal); if (!authorizationManager.isUserAuthorized(resourceId.getId(), resourceId.getVersion(), user, authorizationType)) { String username = principal == null ? "anonymous" : principal.getName(); String message = "User %s does not have %s permission to access the requested resource."; message = String.format(message, resourceId.getId()); throw new NotFoundException(message);
testCaseDescriptorStore.createDescriptor(resourceId.getId(), resourceId.getVersion(), createTestCaseDescriptor(createdResourceURI, userURI)); } else if (resourceLocationUri.startsWith("eddi://ai.labs.conversation")) { conversationDescriptorStore.createDescriptor(resourceId.getId(), resourceId.getVersion(), createConversationDescriptor(createdResourceURI, userURI)); } else if (isResourceIdValid(resourceId)) { documentDescriptorStore.createDescriptor(resourceId.getId(), resourceId.getVersion(), createDocumentDescriptor(createdResourceURI, userURI)); ResourceDescriptor resourceDescriptor = (ResourceDescriptor) descriptorStore.readDescriptor(resourceId.getId(), resourceId.getVersion() - 1); resourceDescriptor.setLastModifiedOn(new Date(System.currentTimeMillis())); resourceDescriptor.setLastModifiedBy(UserUtilities.getUserURI(userStore, SecurityUtilities.getPrincipal(ThreadContext.getSubject()))); resourceDescriptor.setResource(createNewVersionOfResource(resourceDescriptor.getResource(), resourceId.getVersion())); descriptorStore.updateDescriptor(resourceId.getId(), resourceId.getVersion() - 1, resourceDescriptor); IResourceStore.IResourceId resourceId = RestUtilities.extractResourceId(URI.create(currentResourceURI)); if (isResourceIdValid(resourceId)) { ResourceDescriptor resourceDescriptor = (ResourceDescriptor) descriptorStore.readDescriptor(resourceId.getId(), resourceId.getVersion()); resourceDescriptor.setDeleted(true); descriptorStore.setDescriptor(resourceId.getId(), resourceId.getVersion(), resourceDescriptor);
RestUtilities.extractResourceId(conversationDescriptor.getResource()); try { memorySnapshot = conversationMemoryStore.loadConversationMemorySnapshot(resourceId.getId()); } catch (IResourceStore.ResourceNotFoundException e) { String message = "Resource referenced in descriptor does not exist (anymore) [%s]. "; URI createdBy = conversationDescriptor.getCreatedBy(); if (createdBy != null) { User user = userStore.readUser(RestUtilities.extractResourceId(createdBy).getId()); conversationDescriptor.setCreatedByUserName(user.getDisplayName());
private void parsePackage(String targetDirPath, URI packageUri, BotConfiguration botConfiguration, AsyncResponse response) { try { IResourceId packageResourceId = RestUtilities.extractResourceId(packageUri); String packageId = packageResourceId.getId(); String packageVersion = String.valueOf(packageResourceId.getVersion());
permissionStore.createPermissions(respondedResourceId.getId(), PermissionUtilities.createDefaultPermissions(respondedResourceURI)); } else { Principal userPrincipal = SecurityUtilities.getPrincipal(ThreadContext.getSubject()); if (methodName.equals(METHOD_NAME_START_CONVERSATION)) { IResourceStore.IResourceId resourceId = RestUtilities.extractResourceId(URI.create(httpServletRequest.getRequestURI())); Permissions permissions = permissionStore.readPermissions(resourceId.getId()); PermissionUtilities.addAuthorizedUser(permissions, IAuthorization.Type.WRITE, new AuthorizedUser(userURI, null)); permissionStore.createPermissions(respondedResourceId.getId(), permissions); } else if (methodName.equals(METHOD_NAME_CREATE_TESTCASE)) { ITestCaseStore testCaseStore = injector.getInstance(ITestCaseStore.class); TestCase testCase = testCaseStore.loadTestCase(respondedResourceId.getId()); Permissions permissions = permissionStore.readPermissions(testCase.getBotId()); PermissionUtilities.addAuthorizedUser(permissions, IAuthorization.Type.ADMINISTRATION, new AuthorizedUser(userURI, null)); permissionStore.createPermissions(respondedResourceId.getId(), permissions); } else { permissionStore.createPermissions(respondedResourceId.getId(), PermissionUtilities.createDefaultPermissions(userURI));
resourceIds = readBehaviorRuleSetResourceIds(packageConfiguration); for (IResourceStore.IResourceId resourceId : resourceIds) { BehaviorConfiguration behaviorConfiguration = behaviorStore.read(resourceId.getId(), resourceId.getVersion()); for (BehaviorGroupConfiguration groupConfiguration : behaviorConfiguration.getBehaviorGroups()) { for (BehaviorRuleConfiguration behaviorRuleConfiguration : groupConfiguration.getBehaviorRules()) { List<String> outputKeys = outputStore.readOutputActions(resourceId.getId(), resourceId.getVersion(), filter, "asc", limit); CollectionUtilities.addAllWithoutDuplicates(retOutputKeys, outputKeys); if (retOutputKeys.size() >= limit) {
for (IResourceId packageId : packagesContainingResource) { if (packageId.getVersion() < getCurrentResourceId(packageId.getId()).getVersion()) { continue; return id.equals(packageId.getId()); }). findFirst().isEmpty(); packageId.getId(), packageId.getVersion()));
testCaseDescriptors = testCaseDescriptorStore.readDescriptors("ai.labs.testcases", null, index, limit, false); for (TestCaseDescriptor testCaseDescriptor : testCaseDescriptors) { TestCase testCase = readTestCase(RestUtilities.extractResourceId(testCaseDescriptor.getResource()).getId()); if (!RuntimeUtilities.isNullOrEmpty(botId)) { if (!botId.equals(testCase.getBotId())) {
PackageConfiguration packageConfiguration = packageConfigurations.get(resourceId); String packageConfigurationString = jsonSerialization.serialize(packageConfiguration); Path packagePath = writeDirAndDocument(resourceId.getId(), resourceId.getVersion(), packageConfigurationString, botPath, PACKAGE_EXT); writeDocumentDescriptor(packagePath, resourceId.getId(), resourceId.getVersion());
public List<DocumentDescriptor> getBotDescriptorsContainingPackage(String packageId, Integer packageVersion, boolean includePreviousVersions) throws ResourceNotFoundException, ResourceStoreException { List<DocumentDescriptor> ret = new LinkedList<>(); do { List<IResourceId> botIdsContainingPackageUri = botResourceStore.getBotIdsContainingPackage(packageId, packageVersion); for (IResourceId botId : botIdsContainingPackageUri) { if (botId.getVersion() < getCurrentResourceId(botId.getId()).getVersion()) { continue; } boolean alreadyContainsResource = !ret.stream().filter( descriptor -> URIUtilities.extractResourceId(descriptor.getResource()).getId().equals(botId.getId())). findFirst().isEmpty(); if (alreadyContainsResource) { continue; } ret.add(documentDescriptorStore.readDescriptor(botId.getId(), botId.getVersion())); } packageVersion--; } while (includePreviousVersions && packageVersion >= 1); return ret; }
@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); } }
private void updateDocumentDescriptor(Path directoryPath, List<URI> oldUris, List<URI> newUris) throws RestInterfaceFactory.RestInterfaceFactoryException { IRestDocumentDescriptorStore restDocumentDescriptorStore = getRestResourceStore(IRestDocumentDescriptorStore.class); IntStream.range(0, oldUris.size()).forEach(idx -> { try { URI oldUri = oldUris.get(idx); IResourceId oldResourceId = RestUtilities.extractResourceId(oldUri); DocumentDescriptor oldDocumentDescriptor = readDocumentDescriptorFromFile(directoryPath, oldResourceId); URI newUri = newUris.get(idx); IResourceId newResourceId = RestUtilities.extractResourceId(newUri); PatchInstruction<DocumentDescriptor> patchInstruction = new PatchInstruction<>(); patchInstruction.setOperation(PatchInstruction.PatchOperation.SET); patchInstruction.setDocument(oldDocumentDescriptor); restDocumentDescriptorStore.patchDescriptor(newResourceId.getId(), newResourceId.getVersion(), patchInstruction); } catch (IOException e) { log.error(e.getLocalizedMessage(), e); } }); }
private UserConversation createNewConversation(String intent, String userId) throws CannotCreateConversationException { BotTriggerConfiguration botTriggerConfiguration = getBotTrigger(intent); BotDeployment botDeployment = getRandom(botTriggerConfiguration.getBotDeployments()); Response botResponse = restBotEngine.startConversationWithContext(botDeployment.getEnvironment(), botDeployment.getBotId(), userId, botDeployment.getInitialContext()); int responseHttpCode = botResponse.getStatus(); if (responseHttpCode == 201) { URI locationUri = URI.create(botResponse.getHeaders().get("location").get(0).toString()); IResourceId resourceId = RestUtilities.extractResourceId(locationUri); return createUserConversation(intent, userId, botDeployment, resourceId.getId()); } else { throw new CannotCreateConversationException( String.format("Cannot create conversation for botId=%s in environment=%s (httpCode=%s)", botDeployment.getBotId(), botDeployment.getEnvironment(), responseHttpCode)); } }
public static List<AuthorizedUser> mergeAuthorizedSubjects(IGroupStore groupStore, AuthorizedSubjects authorizedSubjects) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException { List<AuthorizedUser> authorizedUsers = new LinkedList<AuthorizedUser>(authorizedSubjects.getUsers()); List<AuthorizedGroup> groups = authorizedSubjects.getGroups(); for (AuthorizedGroup authorizedGroup : groups) { URI group = authorizedGroup.getGroup(); if(group == null) { authorizedUsers.add(new AuthorizedUser(null, null)); continue; } Group userGroup = groupStore.readGroup(RestUtilities.extractResourceId(group).getId()); for (URI userURI : userGroup.getUsers()) { authorizedUsers.add(new AuthorizedUser(userURI, authorizedGroup.getVersions())); } } return authorizedUsers; }
@Override public List<String> readExpressions(String packageId, Integer packageVersion, String filter, Integer limit) { List<String> retExpressions = new LinkedList<>(); try { PackageConfiguration packageConfiguration = packageStore.read(packageId, packageVersion); List<IResourceStore.IResourceId> resourceIds = readDictionaryResourceIds(packageConfiguration); for (IResourceStore.IResourceId resourceId : resourceIds) { List<String> expressions = regularDictionaryStore.readExpressions(resourceId.getId(), resourceId.getVersion(), filter, "asc", 0, limit); CollectionUtilities.addAllWithoutDuplicates(retExpressions, expressions); } return retExpressions; } catch (IResourceStore.ResourceNotFoundException e) { throw new NoLogWebApplicationException(Response.Status.NOT_FOUND); } catch (IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(e); } }
private void writeConfigs(Path path, Map<IResourceId, String> configs, String fileExtension) { configs.forEach((resourceId, value) -> { String filename = MessageFormat.format("{0}.{1}.json", resourceId.getId(), fileExtension); Path filePath = Paths.get(path.toString(), filename); try { deleteFileIfExists(filePath); try (BufferedWriter writer = Files.newBufferedWriter(filePath)) { writer.write(value); writeDocumentDescriptor(path, resourceId.getId(), resourceId.getVersion()); } } catch (IOException | IResourceStore.ResourceStoreException | IResourceStore.ResourceNotFoundException e) { log.error(e.getLocalizedMessage(), e); } }); }
private <T> List<T> readResources(List<URI> uris, Path packagePath, String extension, Class<T> clazz) { return uris.stream().map(uri -> { try { IResourceId resourceId = RestUtilities.extractResourceId(uri); Path resourcePath = createResourcePath(packagePath, resourceId.getId(), extension); String resourceContent = readFile(resourcePath); return jsonSerialization.deserialize(resourceContent, clazz); } catch (IOException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); }
@Override public List<DocumentDescriptor> readBotDescriptors(String filter, Integer index, Integer limit, String containingPackageUri, Boolean includePreviousVersions) { IResourceStore.IResourceId validatedResourceId = ResourceUtilities.validateUri(containingPackageUri); if (validatedResourceId == null || !containingPackageUri.startsWith(PACKAGE_URI)) { return createMalFormattedResourceUriException(containingPackageUri); } try { return botStore.getBotDescriptorsContainingPackage( validatedResourceId.getId(), validatedResourceId.getVersion(), includePreviousVersions); } catch (IResourceStore.ResourceNotFoundException | IResourceStore.ResourceStoreException e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(); } }
private DocumentDescriptor readDocumentDescriptorFromFile(Path packagePath, IResourceId resourceId) throws IOException { Path filePath = Paths.get(FileUtilities.buildPath(packagePath.toString(), resourceId.getId() + ".descriptor.json")); String oldDocumentDescriptorFile = readFile(filePath); return jsonSerialization.deserialize(oldDocumentDescriptorFile, DocumentDescriptor.class); }
private static boolean isResourceIdValid(IResourceStore.IResourceId resourceId) { return resourceId.getId() != null && resourceId.getVersion() > 0; } }