Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions javascript-modules-engine-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down Expand Up @@ -94,6 +104,11 @@
<artifactId>graal-sdk</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jahia.server</groupId>
<artifactId>jahia-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jahia.modules.javascript.modules.engine.jshandler.parsers.AbstractFileParser;
import org.jahia.modules.javascript.modules.engine.jshandler.parsers.CndFileParser;
import org.jahia.modules.javascript.modules.engine.jshandler.parsers.JCRImportXmlFileParser;
import org.jahia.modules.javascript.modules.engine.jshandler.parsers.ParsingContext;
import org.ops4j.pax.swissbox.bnd.BndUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -67,16 +71,23 @@ public InputStream getInputStream() throws IOException {
logger.info("Handling JS module using javascript protocol wrapper for package: {}", wrappedUrl);
File outputDir = Files.createTempDirectory("javascript.").toFile();
TarUtils.unTar(new GZIPInputStream(wrappedUrl.openStream()), outputDir);
Properties instructions = new Properties();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

File packageDir = new File(outputDir, "package");
Collection<File> files = FileUtils.listFiles(packageDir, null, true);
// Capabilities context and parsers
ParsingContext parsingContext = new ParsingContext();
CndFileParser cndFileParser = new CndFileParser();
cndFileParser.setLogger(logger);
JCRImportXmlFileParser importXmlFileParser = new JCRImportXmlFileParser();
importXmlFileParser.setLogger(logger);
Map<String, Object> packageJson = null;
List<File> cndFiles = new ArrayList<>();
File finalCndFile = null;
try (JarOutputStream jos = new JarOutputStream(byteArrayOutputStream)) {
Set<ZipEntry> processedImages = new HashSet<>();

// Process files of the packages
List<File> cndFiles = new ArrayList<>();
for (File file : files) {
if (file.getName().endsWith(".cnd")) {
// Postpone processing of CND files
Expand All @@ -87,16 +98,14 @@ public InputStream getInputStream() throws IOException {
// Calculate relative path of the file in the package
String packageRelativePath = packageDir.toURI().relativize(file.toURI()).getPath();

// Extract instructions from package.json
// Copy file path (try to detect good path for file in the final package jar.)
if (packageRelativePath.equals("package.json")) {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> properties = mapper.readValue(file, Map.class);
Map<String, Object> jahiaProps = (Map<String, Object>) properties.getOrDefault("jahia", new HashMap<>());
instructions.putAll(generateInstructions(properties, jahiaProps));
}

// Copy file path (try to detect good path for file in the final package jar.)
if (packageRelativePath.equals("import.xml")) {
packageJson = mapper.readValue(file, Map.class);
jos.putNextEntry(new ZipEntry(packageRelativePath));
} else if (packageRelativePath.equals("import.xml")) {
// Extract required nodetypes from xml
extractNodetypes(file, parsingContext, importXmlFileParser);
jos.putNextEntry(new ZipEntry("META-INF/" + packageRelativePath));
} else if (packageRelativePath.startsWith("settings/")) {
// Special mapping settings/content-editor-forms to META-INF/jahia-content-editor-forms
Expand All @@ -117,6 +126,10 @@ else if (packageRelativePath.equals("settings/template-thumbnail.png")) {
}
// Map everything else in settings/ to META-INF/
else {
// Extract required nodetypes from imported xml files.
if (packageRelativePath.endsWith(".xml")) {
extractNodetypes(file, parsingContext, importXmlFileParser);
}
jos.putNextEntry(new ZipEntry("META-INF/" + StringUtils.substringAfter(packageRelativePath, "settings/")));
}
} else if (packageRelativePath.startsWith("components") && packageRelativePath.endsWith(".png")) {
Expand Down Expand Up @@ -144,47 +157,65 @@ else if (packageRelativePath.equals("settings/template-thumbnail.png")) {
}
}

if (packageJson == null) {
throw new IOException("Invalid package: package.json not found");
}
// Process CND files (merging them in a single file if necessary)
if (!cndFiles.isEmpty()) {
if (cndFiles.size() == 1) {
// Single cnd file, just copy it, no need for merge
if (logger.isDebugEnabled()) {
logger.debug("Single CND file detected in the package.");
}
File cndFile = cndFiles.get(0);
jos.putNextEntry(new ZipEntry("META-INF/definitions.cnd"));
try (FileInputStream input = new FileInputStream(cndFile)) {
IOUtils.copy(input, jos);
}
finalCndFile = cndFiles.get(0);

} else {
// Multiple cnd files, merge them
if (logger.isDebugEnabled()) {
logger.debug("Multiple CND files detected in the package, they will be merged into a single file");
}
File mergedDefinitionFile = mergeDefinitionFiles(cndFiles, packageDir);
if (mergedDefinitionFile != null) {
jos.putNextEntry(new ZipEntry("META-INF/definitions.cnd"));
try (FileInputStream input = new FileInputStream(mergedDefinitionFile)) {
IOUtils.copy(input, jos);
} finally {
FileUtils.delete(mergedDefinitionFile);
}
}
finalCndFile = mergeDefinitionFiles(cndFiles, packageDir);
}
}

} catch (Exception e) {
logger.error("An error occurred during JS module transformation", e);
// Extract nodetype capabilities from the CND file
extractNodetypes(finalCndFile, parsingContext, cndFileParser);

jos.putNextEntry(new ZipEntry("META-INF/definitions.cnd"));
try (FileInputStream input = new FileInputStream(finalCndFile)) {
IOUtils.copy(input, jos);
}
}
} finally {
if (finalCndFile != null) {
// cndFile may be generated when merged, clean it up
FileUtils.deleteQuietly(finalCndFile);
}
// Clean up work dir
FileUtils.deleteDirectory(outputDir);
}

FileUtils.deleteDirectory(outputDir);

Properties instructions = new Properties();
// Extract instructions from package.json and nodetype capabilities
instructions.putAll(generateInstructions(packageJson, parsingContext));
if (logger.isDebugEnabled()) {
logger.debug("JS module transformed to bundle using instructions: {}", instructions);
}

return BndUtils.createBundle(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()), instructions, wrappedUrl.toExternalForm());
}

private Properties generateInstructions(Map<String, Object> properties, Map<String, Object> jahiaProps) {
private void extractNodetypes(File fileToBeParsed, ParsingContext parsingContext, AbstractFileParser parser) throws IOException {
try (InputStream inputStream = new FileInputStream(fileToBeParsed)) {
logger.info("Extracting node types from {}", fileToBeParsed.getAbsolutePath());
if (parser.parse(fileToBeParsed.getName(), inputStream, fileToBeParsed.getParent(), false, false, null, parsingContext)) {
logger.info("Successfully extracted node types from {}", fileToBeParsed.getAbsolutePath());
};
}
}

private Properties generateInstructions(Map<String, Object> properties, ParsingContext parsingContext) {
Map<String, Object> jahiaProps = (Map<String, Object>) properties.getOrDefault("jahia", new HashMap<>());
Properties instructions = new Properties();

// First let's setup Bundle headers
Expand Down Expand Up @@ -213,6 +244,16 @@ private Properties generateInstructions(Map<String, Object> properties, Map<Stri
// always include "/static" as folder for the static resources
instructions.put("Jahia-Static-Resources", StringUtils.defaultIfEmpty((String) jahiaProps.get("static-resources"), "/css,/icons,/images,/img,/javascript") + ",/static");
instructions.put("-removeheaders", "Private-Package, Export-Package");
// Add Provide-Capability for provided node types
if (!parsingContext.getContentTypeDefinitions().isEmpty()) {
String provideCapability = String.join(",", parsingContext.getContentTypeDefinitions());
instructions.put("Provide-Capability", "com.jahia.services.content; nodetypes:List<String>=\"" + provideCapability + "\"");
}
// Add Require-Capability for required node types
if (!parsingContext.getContentTypeReferences().isEmpty()) {
String nodeTypeRequirements = String.join("\"),com.jahia.services.content; filter:=\"(nodetypes=", parsingContext.getContentTypeReferences());
instructions.put("Require-Capability", "com.jahia.services.content; filter:=\"(nodetypes=" + nodeTypeRequirements + "\")");
}
return instructions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2002-2023 Jahia Solutions Group SA. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jahia.modules.javascript.modules.engine.jshandler.parsers;

import org.slf4j.Logger;

/**
* Abstract file parser
*
* TEMPORARY WORKAROUND - DO NOT USE
* This class duplicates poor legacy code to provide backward compatibility.
* Marked for immediate replacement and removal.
*
* @deprecated since 1.0.0 Technical debt. Will be removed in next major version.
*/
@Deprecated(since = "1.0.0")
public abstract class AbstractFileParser implements FileParser, Comparable<AbstractFileParser> {

private Logger logger;
protected int priority = 0;

public void setLogger(Logger logger) {
this.logger = logger;
}

public Logger getLogger() {
return logger;
}

public int getPriority() {
return priority;
}

public void setPriority(int priority) {
this.priority = priority;
}

public int compareTo(AbstractFileParser o) {
int priorityDiff = priority - o.priority;
if (priorityDiff != 0) {
return priorityDiff;
}
return this.getClass().getName().compareTo(o.getClass().getName());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

AbstractFileParser that = (AbstractFileParser) o;

if (priority != that.priority) return false;
return getClass().getName().equals(o.getClass().getName());
}

@Override
public int hashCode() {
int result = getClass().getName().hashCode();
result = 31 * result + priority;
return result;
}
}
Loading
Loading