use of org.apache.sling.ide.filter.Filter 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.Filter 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.Filter 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.Filter in project sling by apache.
the class ProjectUtil method loadFilter.
/**
* Loads a filter for the specified project
*
* @param project the project to find a filter for
* @return the found filter or null
* @throws CoreException
*/
public static Filter loadFilter(final IProject project) throws CoreException {
FilterLocator filterLocator = Activator.getDefault().getFilterLocator();
IPath filterPath = findFilterPath(project);
if (filterPath == null) {
return null;
}
IFile filterFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(filterPath);
Filter filter = null;
if (filterFile != null && filterFile.exists()) {
try (InputStream contents = filterFile.getContents()) {
filter = filterLocator.loadFilter(contents);
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed loading filter file for project " + project.getName() + " from location " + filterFile, e));
}
}
return filter;
}
use of org.apache.sling.ide.filter.Filter 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;
}
});
}
Aggregations