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); } }); }
@Override public Response redirectToLatestVersion(String id) { try { IResourceStore.IResourceId currentResourceId = getCurrentResourceId(id); String path = URI.create(resourceURI).getPath(); return Response.seeOther(URI.create(path + id + versionQueryParam + currentResourceId.getVersion())).build(); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage()); } }
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(); } }
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);
IResourceId packageResourceId = RestUtilities.extractResourceId(packageUri); String packageId = packageResourceId.getId(); String packageVersion = String.valueOf(packageResourceId.getVersion());
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) {
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.";
for (IResourceId packageId : packagesContainingResource) { if (packageId.getVersion() < getCurrentResourceId(packageId.getId()).getVersion()) { continue; packageId.getVersion()));
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 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 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()); }
@Override public Integer getCurrentVersion(String id) { try { IResourceStore.IResourceId currentResourceId = getCurrentResourceId(id); return currentResourceId.getVersion(); } catch (IResourceStore.ResourceNotFoundException e) { throw new NotFoundException(e.getLocalizedMessage(), e); } }
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; }
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; } }