Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLING-11497 - convert all cnd files to repoinit statements #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

Expand All @@ -44,6 +45,7 @@ public class BundleSlingInitialContentExtractContext {
private final String runMode;
private final Manifest manifest;
private final JcrNamespaceRegistry namespaceRegistry;
private final String slingNodeTypes;
private final List<PathEntry> pathEntryList = new ArrayList<>();

public BundleSlingInitialContentExtractContext(@NotNull ContentPackage2FeatureModelConverter.SlingInitialContentPolicy slingInitialContentPolicy,
Expand All @@ -65,7 +67,8 @@ public BundleSlingInitialContentExtractContext(@NotNull ContentPackage2FeatureMo
jarFile,
converter.getFeaturesManager().getNamespaceUriByPrefix()
).provideRegistryFromBundle();

this.slingNodeTypes = this.manifest.getMainAttributes().getValue("Sling-Nodetypes");

Iterator<PathEntry> pathEntries = PathEntry.getContentPaths(manifest, -1);

if (pathEntries != null) {
Expand Down Expand Up @@ -117,4 +120,13 @@ public JcrNamespaceRegistry getNamespaceRegistry() {
public List<PathEntry> getPathEntryList() {
return new ArrayList<>(pathEntryList);
}

@NotNull
public boolean isSlingNodeTypesEntry(@NotNull JarEntry jarEntry) {
if(slingNodeTypes == null){
return false;
}

return jarEntry.getName().equals(slingNodeTypes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially a comma separated list. Also a unit test/IT covering this would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again a good point.. implemented this and pushed.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ void extractAndAddToAssembler(@NotNull BundleSlingInitialContentExtractContext c
throw new IOException("Can not convert " + file + " to enhanced DocView format", e);
}

// remap CND files to make sure they are picked up by the NodeTypesEntryHandler
if (context.getNamespaceRegistry().getRegisteredCndSystemIds().contains(file.getName())) {
contentPackageEntryPath = "/META-INF/vault/" + Text.getName(file.getName()) + ".cnd";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the approach to convert to cnd files in a package discarded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to have an effect afaik but I guess im wrong, il put it back in then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either put into repo init statements or into content packages, not both!

}


}

try (Archive virtualArchive = SingleFileArchive.fromPathOrInputStream(tmpDocViewInputFile, bundleFileInputStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -138,6 +141,11 @@ private void extractFile(JarEntry jarEntry, JarOutputStream bundleOutput) throws

SlingInitialContentBundleEntryMetaData bundleEntry = createSlingInitialContentBundleEntry(context, targetFile);
collectedSlingInitialContentBundleEntries.add(bundleEntry);
} else if (context.isSlingNodeTypesEntry(jarEntry)) {
//make sure to register all cnd files to the AclManager, so they get written as RepoInit.
try (Reader cndStatements = new InputStreamReader(Objects.requireNonNull(context.getJarFile().getInputStream(jarEntry)))) {
context.getConverter().getAclManager().addNodetypeRegistration(IOUtils.toString(cndStatements));
}
} else {
//write 'normal' content out to the normal bundle output
bundleOutput.putNextEntry(jarEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@
import org.apache.sling.feature.cpconverter.filtering.RegexBasedResourceFilter;
import org.apache.sling.feature.cpconverter.handlers.DefaultEntryHandlersManager;
import org.apache.sling.feature.cpconverter.handlers.EntryHandlersManager;
import org.apache.sling.feature.cpconverter.shared.ConverterConstants;
import org.apache.sling.feature.cpconverter.vltpkg.DefaultPackagesEventsEmitter;
import org.apache.sling.feature.io.json.FeatureJSONReader;
import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
import org.apache.sling.repoinit.parser.operations.Operation;
import org.apache.sling.repoinit.parser.operations.RegisterNodetypes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -369,6 +373,41 @@ public void convertContentPackagePutInDedicatedFolderContentTypePackagePolicy()
}
}

@Test
public void testNamespaceToRepoInitConversion() throws Exception {
URL packageUrl = getClass().getResource("mysite-slinginitialcontent-namespace.all-1.0.0-SNAPSHOT.zip");
File packageFile = FileUtils.toFile(packageUrl);

File outputDirectory = new File(System.getProperty("java.io.tmpdir"), getClass().getName() + '_' + System.currentTimeMillis());

converter.setFeaturesManager(new DefaultFeaturesManager(true, 5, outputDirectory, null, null, new HashMap<>(), new DefaultAclManager()))
.setBundlesDeployer(new LocalMavenRepositoryArtifactsDeployer(outputDirectory))
.setEmitter(DefaultPackagesEventsEmitter.open(outputDirectory))
.setContentTypePackagePolicy(PackagePolicy.PUT_IN_DEDICATED_FOLDER)
.setUnreferencedArtifactsDeployer(new LocalMavenRepositoryArtifactsDeployer( new File(outputDirectory, "mutable-content")))
.setEntryHandlersManager(
new DefaultEntryHandlersManager(Collections.emptyMap(), true,
ContentPackage2FeatureModelConverter.SlingInitialContentPolicy.EXTRACT_AND_REMOVE, ConverterConstants.SYSTEM_USER_REL_PATH_DEFAULT)
)
.convert(packageFile);

File featureFile = new File(outputDirectory, "mysite.all.json");
try (Reader reader = new FileReader(featureFile)) {
Feature feature = FeatureJSONReader.read(reader, "content1");
Extension repoInit = feature.getExtensions().getByName("repoinit");
RepoInitParserService repoInitParserService = new RepoInitParserService();

try(Reader repoInitReadder = new StringReader(repoInit.getText())){
List<Operation> operations = repoInitParserService.parse(repoInitReadder);

boolean found = operations.stream().anyMatch(o -> o instanceof RegisterNodetypes);
assertTrue("There must be a register node type extension in the feature model's repoinit", found);
}

}
System.out.println(outputDirectory.getAbsolutePath());
}

@Test
public void convertContentPackageRemoveInstallHooks() throws Exception {
URL packageUrl = getClass().getResource("test-with-install-hooks.zip");
Expand Down
Binary file not shown.