use of org.apache.sling.ide.filter.FilterResult in project sling by apache.
the class JcrNode method canCreateChild.
public boolean canCreateChild() {
try {
final IProject project = getProject();
final Filter filter = ProjectUtil.loadFilter(project);
final String relativeFilePath = getJcrPath();
// }
if (filter == null) {
Activator.getDefault().getPluginLogger().error("No filter.xml found for " + project);
return true;
} else {
final FilterResult result = filter.filter(relativeFilePath);
return result == FilterResult.ALLOW;
}
} catch (CoreException e) {
Logger logger = Activator.getDefault().getPluginLogger();
logger.error("Could not verify child node allowance: " + this, e);
return false;
}
}
use of org.apache.sling.ide.filter.FilterResult in project sling by apache.
the class ResourceChangeCommandFactory method removeFileCommand.
private Command<?> removeFileCommand(Repository repository, IResource resource) throws CoreException, IOException {
if (resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)) {
Activator.getDefault().getPluginLogger().trace("Skipping team-private resource {0}", resource);
return null;
}
if (ignoredFileNames.contains(resource.getName())) {
return null;
}
IFolder syncDirectory = ProjectUtil.getSyncDirectory(resource.getProject());
Filter filter = ProjectUtil.loadFilter(syncDirectory.getProject());
FilterResult filterResult = getFilterResult(resource, null, filter);
if (filterResult == FilterResult.DENY || filterResult == FilterResult.PREREQUISITE) {
return null;
}
String resourceLocation = getRepositoryPathForDeletedResource(resource, ProjectUtil.getSyncDirectoryFile(resource.getProject()));
// verify whether a resource being deleted does not signal that the content structure
// was rearranged under a covering parent aggregate
IPath serializationFilePath = Path.fromOSString(serializationManager.getSerializationFilePath(resourceLocation, SerializationKind.FOLDER));
ResourceProxy coveringParentData = findSerializationDataFromCoveringParent(resource, syncDirectory, resourceLocation, serializationFilePath);
if (coveringParentData != null) {
Activator.getDefault().getPluginLogger().trace("Found covering resource data ( repository path = {0} ) for resource at {1}, skipping deletion and performing an update instead", coveringParentData.getPath(), resource.getFullPath());
FileInfo info = createFileInfo(resource);
return repository.newAddOrUpdateNodeCommand(new CommandContext(filter), info, coveringParentData);
}
return repository.newDeleteNodeCommand(serializationManager.getRepositoryPath(resourceLocation));
}
use of org.apache.sling.ide.filter.FilterResult in project sling by apache.
the class ResourceChangeCommandFactory method buildResourceAndInfo.
/**
* Convenience method which builds a <tt>ResourceAndInfo</tt> info for a specific <tt>IResource</tt>
*
* @param resource the resource to process
* @param repository the repository, used to extract serialization information for different resource types
* @return the build object, or null if one could not be built
* @throws CoreException
* @throws SerializationException
* @throws IOException
*/
public ResourceAndInfo buildResourceAndInfo(IResource resource, Repository repository) throws CoreException, IOException {
if (ignoredFileNames.contains(resource.getName())) {
return null;
}
Long modificationTimestamp = (Long) resource.getSessionProperty(ResourceUtil.QN_IMPORT_MODIFICATION_TIMESTAMP);
if (modificationTimestamp != null && modificationTimestamp >= resource.getModificationStamp()) {
Activator.getDefault().getPluginLogger().trace("Change for resource {0} ignored as the import timestamp {1} >= modification timestamp {2}", resource, modificationTimestamp, resource.getModificationStamp());
return null;
}
if (resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)) {
Activator.getDefault().getPluginLogger().trace("Skipping team-private resource {0}", resource);
return null;
}
FileInfo info = createFileInfo(resource);
Activator.getDefault().getPluginLogger().trace("For {0} built fileInfo {1}", resource, info);
File syncDirectoryAsFile = ProjectUtil.getSyncDirectoryFullPath(resource.getProject()).toFile();
IFolder syncDirectory = ProjectUtil.getSyncDirectory(resource.getProject());
Filter filter = ProjectUtil.loadFilter(resource.getProject());
ResourceProxy resourceProxy = null;
if (serializationManager.isSerializationFile(resource.getLocation().toOSString())) {
IFile file = (IFile) resource;
try (InputStream contents = file.getContents()) {
String resourceLocation = file.getFullPath().makeRelativeTo(syncDirectory.getFullPath()).toPortableString();
resourceProxy = serializationManager.readSerializationData(resourceLocation, contents);
normaliseResourceChildren(file, resourceProxy, syncDirectory, repository);
// TODO - not sure if this 100% correct, but we definitely should not refer to the FileInfo as the
// .serialization file, since for nt:file/nt:resource nodes this will overwrite the file contents
String primaryType = (String) resourceProxy.getProperties().get(Repository.JCR_PRIMARY_TYPE);
if (Repository.NT_FILE.equals(primaryType)) {
// TODO move logic to serializationManager
File locationFile = new File(info.getLocation());
String locationFileParent = locationFile.getParent();
int endIndex = locationFileParent.length() - ".dir".length();
File actualFile = new File(locationFileParent.substring(0, endIndex));
String newLocation = actualFile.getAbsolutePath();
String newName = actualFile.getName();
String newRelativeLocation = actualFile.getAbsolutePath().substring(syncDirectoryAsFile.getAbsolutePath().length());
info = new FileInfo(newLocation, newRelativeLocation, newName);
Activator.getDefault().getPluginLogger().trace("Adjusted original location from {0} to {1}", resourceLocation, newLocation);
}
} catch (IOException e) {
Status s = new Status(Status.WARNING, Activator.PLUGIN_ID, "Failed reading file at " + resource.getFullPath(), e);
StatusManager.getManager().handle(s, StatusManager.LOG | StatusManager.SHOW);
return null;
}
} else {
// possible .dir serialization holder
if (resource.getType() == IResource.FOLDER && resource.getName().endsWith(".dir")) {
IFolder folder = (IFolder) resource;
IResource contentXml = folder.findMember(".content.xml");
// .dir serialization holder ; nothing to process here, the .content.xml will trigger the actual work
if (contentXml != null && contentXml.exists() && serializationManager.isSerializationFile(contentXml.getLocation().toOSString())) {
return null;
}
}
resourceProxy = buildResourceProxyForPlainFileOrFolder(resource, syncDirectory, repository);
}
FilterResult filterResult = getFilterResult(resource, resourceProxy, filter);
switch(filterResult) {
case ALLOW:
return new ResourceAndInfo(resourceProxy, info);
case PREREQUISITE:
// never try to 'create' the root node, we assume it exists
if (!resourceProxy.getPath().equals("/")) {
// suited one ( typically nt:unstructured )
return new ResourceAndInfo(new ResourceProxy(resourceProxy.getPath()), null, true);
}
// falls through
case DENY:
default:
return null;
}
}
use of org.apache.sling.ide.filter.FilterResult in project sling by apache.
the class AddOrUpdateNodeCommandTest method nodeNotPresentButOutsideOfFilterIsNotRemoved.
@Test
public void nodeNotPresentButOutsideOfFilterIsNotRemoved() throws Exception {
final CommandContext context = new CommandContext(new Filter() {
@Override
public FilterResult filter(String repositoryPath) {
if (repositoryPath.equals("/content/not-included-child")) {
return FilterResult.DENY;
}
return FilterResult.ALLOW;
}
});
doWithTransientRepository(new CallableWithSession() {
@Override
public Void call() throws Exception {
Node content = session().getRootNode().addNode("content", "nt:unstructured");
content.addNode("included-child");
content.addNode("not-included-child");
session().save();
ResourceProxy resource = newResource("/content", "nt:unstructured");
resource.addChild(newResource("/content/included-child", "nt:unstructured"));
AddOrUpdateNodeCommand cmd = new AddOrUpdateNodeCommand(repo(), credentials(), context, null, resource, logger);
cmd.execute().get();
session().refresh(false);
content = session().getRootNode().getNode("content");
content.getNode("included-child");
content.getNode("not-included-child");
return null;
}
});
}
use of org.apache.sling.ide.filter.FilterResult in project sling by apache.
the class ImportRepositoryContentAction method crawlChildrenAndImport.
/**
* Crawls the repository and recursively imports founds resources
* @param path the current path to import from
* @param tracer
* @throws JSONException
* @throws RepositoryException
* @throws CoreException
* @throws IOException
*/
// TODO: This probably should be pushed into the service layer
private void crawlChildrenAndImport(String path) throws RepositoryException, CoreException, IOException, SerializationException {
logger.trace("crawlChildrenAndImport({0}, {1}, {2}, {3}", repository, path, project, projectRelativePath);
ResourceProxy resource = executeCommand(repository.newListChildrenNodeCommand(path));
SerializationData serializationData = builder.buildSerializationData(contentSyncRoot, resource);
logger.trace("For resource at path {0} got serialization data {1}", resource.getPath(), serializationData);
final List<ResourceProxy> resourceChildren = new LinkedList<>(resource.getChildren());
if (serializationData != null) {
IPath serializationFolderPath = contentSyncRootDir.getProjectRelativePath().append(serializationData.getFolderPath());
switch(serializationData.getSerializationKind()) {
case FILE:
{
byte[] contents = executeCommand(repository.newGetNodeCommand(path));
createFile(project, getPathForPlainFileNode(resource, serializationFolderPath), contents);
if (serializationData.hasContents()) {
createFolder(project, serializationFolderPath);
createFile(project, serializationFolderPath.append(serializationData.getFileName()), serializationData.getContents());
// special processing for nt:resource nodes
for (Iterator<ResourceProxy> it = resourceChildren.iterator(); it.hasNext(); ) {
ResourceProxy child = it.next();
if (Repository.NT_RESOURCE.equals(child.getProperties().get(Repository.JCR_PRIMARY_TYPE))) {
ResourceProxy reloadedChildResource = executeCommand(repository.newListChildrenNodeCommand(child.getPath()));
logger.trace("Skipping direct handling of {0} node at {1} ; will additionally handle {2} direct children", Repository.NT_RESOURCE, child.getPath(), reloadedChildResource.getChildren().size());
if (reloadedChildResource.getChildren().size() != 0) {
String pathName = Text.getName(reloadedChildResource.getPath());
pathName = serializationManager.getOsPath(pathName);
createFolder(project, serializationFolderPath.append(pathName));
// 2. recursively handle all resources
for (ResourceProxy grandChild : reloadedChildResource.getChildren()) {
crawlChildrenAndImport(grandChild.getPath());
}
}
it.remove();
break;
}
}
}
break;
}
case FOLDER:
case METADATA_PARTIAL:
{
IFolder folder = createFolder(project, serializationFolderPath);
parseIgnoreFiles(folder, path);
if (serializationData.hasContents()) {
createFile(project, serializationFolderPath.append(serializationData.getFileName()), serializationData.getContents());
}
break;
}
case METADATA_FULL:
{
if (serializationData.hasContents()) {
createFile(project, serializationFolderPath.append(serializationData.getFileName()), serializationData.getContents());
}
break;
}
}
logger.trace("Resource at {0} has children: {1}", resource.getPath(), resourceChildren);
if (serializationData.getSerializationKind() == SerializationKind.METADATA_FULL) {
return;
}
} else {
logger.trace("No serialization data found for {0}", resource.getPath());
}
ProgressUtils.advance(monitor, 1);
for (ResourceProxy child : resourceChildren) {
if (ignoredResources.isIgnored(child.getPath())) {
continue;
}
if (filter != null) {
FilterResult filterResult = filter.filter(child.getPath());
if (filterResult == FilterResult.DENY) {
continue;
}
}
crawlChildrenAndImport(child.getPath());
}
}
Aggregations