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()); }
protected Response create(T obj) { try { IResourceStore.IResourceId resourceId = resourceStore.create(obj); URI createdUri = RestUtilities.createURI(resourceURI, resourceId.getId(), versionQueryParam, resourceId.getVersion()); return Response.created(createdUri).location(createdUri).build(); } catch (Exception e) { log.error(e.getLocalizedMessage(), e); throw new InternalServerErrorException(); } }
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) {
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);
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; }
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); } }); }
@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 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); } }
@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; } }
public static NoLogWebApplicationException createConflictException(String containerUri, IResourceStore.IResourceId currentId) { URI resourceUri = RestUtilities.createURI(containerUri, currentId.getId(), versionQueryParam, currentId.getVersion()); ResponseBuilderImpl builder = new ResponseBuilderImpl(); builder.status(Response.Status.CONFLICT); builder.entity(resourceUri.toString()); builder.type(MediaType.TEXT_PLAIN); return new NoLogWebApplicationException(builder.build()); }
public static IResourceId validateUri(String resourceUriString) { if (resourceUriString.startsWith("eddi://")) { URI resourceUri = URI.create(resourceUriString); IResourceId resourceId = RestUtilities.extractResourceId(resourceUri); if (!isNullOrEmpty(resourceId.getId()) && !isNullOrEmpty(resourceId.getVersion()) && resourceId.getVersion() > 0) { return resourceId; } } return null; } }
private static <T> Map<IResourceId, T> readConfigs(IResourceStore<T> store, List<URI> configUris) throws IResourceStore.ResourceNotFoundException, IResourceStore.ResourceStoreException { Map<IResourceId, T> ret = new LinkedHashMap<>(); for (URI uri : configUris) { IResourceId resourceId = RestUtilities.extractResourceId(uri); ret.put(resourceId, store.read(resourceId.getId(), resourceId.getVersion())); } return ret; }