This is an automated email from the ASF dual-hosted git repository.
pauls pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
The following commit(s) were added to refs/heads/master by this push:
new 5ef3b18 SLING-9670: add a new unpack extension for the feature model.
5ef3b18 is described below
commit 5ef3b18ead6e5aadfb8941e296b31962c5627bac
Author: Karl Pauls <[email protected]>
AuthorDate: Mon Aug 17 16:46:38 2020 +0200
SLING-9670: add a new unpack extension for the feature model.
---
.../sling/feature/extension/unpack/Unpack.java | 14 +++-
.../sling/feature/extension/unpack/TestUnpack.java | 88 ++++++++++++++++++++++
.../sling/feature/extension/unpack/test1/index.txt | 4 +
.../feature/extension/unpack/test1/sub1/test1.txt | 1 +
.../feature/extension/unpack/test1/sub2/test1.txt | 1 +
.../feature/extension/unpack/test1/sub2/test2.txt | 1 +
.../sling/feature/extension/unpack/test1/test.txt | 1 +
7 files changed, 106 insertions(+), 4 deletions(-)
diff --git
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
index 235c570..93b3583 100644
---
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
+++
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
@@ -84,13 +84,19 @@ public class Unpack
try
{
String dir = context.get("dir");
- if (dir == null) {
- dir = defaultMapping;
+ boolean override;
+ if (dir == null && this.defaultMapping != null) {
+ dir = this.registry.get(defaultMapping).get("dir");
+ override =
Boolean.parseBoolean(this.registry.get(defaultMapping).get("override"));
+ }
+ else {
+ override = Boolean.parseBoolean(context.get("override"));
}
+
if (dir == null) {
throw new IllegalStateException("No target dir and no default
configured");
}
- unpack(dir, url, Boolean.parseBoolean(context.get("override")));
+ unpack(dir, url, override);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
@@ -120,7 +126,7 @@ public class Unpack
}
try (JarFile jarFile = IOUtils.getJarFileFromURL(url, true, null)) {
- jarFile.stream().filter(((Predicate<JarEntry>)
JarEntry::isDirectory).negate()).forEach(entry -> {
+ jarFile.stream().filter(entry -> !entry.isDirectory() &&
!entry.getName().toLowerCase().startsWith("meta-inf/")).forEach(entry -> {
File target = new File(base, entry.getName());
if (target.getParentFile().toPath().startsWith(base.toPath()))
{
if (target.getParentFile().isDirectory() ||
target.getParentFile().mkdirs()) {
diff --git
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
new file mode 100644
index 0000000..3911cd9
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.sling.feature.extension.unpack;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Collections;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import org.apache.sling.feature.io.IOUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUnpack
+{
+ @Test
+ public void testUnzip() throws IOException
+ {
+ File tmp = File.createTempFile("foo", "dir");
+ tmp.delete();
+ Unpack unpack = Unpack.fromMapping("test;default:=true;dir:=\"" +
tmp.getPath() + "\"");
+ URL url = createZipFile("test1");
+
+ unpack.unpack(url, Collections.emptyMap());
+
+ Assert.assertTrue(equals("test1", url));
+ }
+
+ private URL createZipFile(String base) throws IOException {
+ File tmp = File.createTempFile("foo", ".zip");
+ tmp.deleteOnExit();
+ Manifest mf = new Manifest();
+ mf.getMainAttributes().putValue("Manifest-Version", "1");
+ mf.getMainAttributes().putValue("binary", "1");
+ try (JarOutputStream jarOutputStream = new JarOutputStream(new
FileOutputStream(tmp), mf);
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(TestUnpack.class.getResourceAsStream(base + "/index.txt"),
"UTF-8")))
+ {
+ for (String line = reader.readLine(); line != null; line =
reader.readLine())
+ {
+ jarOutputStream.putNextEntry(new ZipEntry(line));
+ try (InputStream inputStream =
TestUnpack.class.getResourceAsStream(base + "/" + line)) {
+ byte[] buffer = new byte[64 * 1024];
+ for (int i = inputStream.read(buffer); i != -1; i =
inputStream.read(buffer)) {
+ jarOutputStream.write(buffer, 0, i);
+ }
+ }
+ }
+ }
+ return tmp.toURI().toURL();
+ }
+
+ private boolean equals(String base, URL jar) throws IOException {
+ try (JarFile jarFile = IOUtils.getJarFileFromURL(jar, false, null);
+ BufferedReader reader = new BufferedReader(new
InputStreamReader(TestUnpack.class.getResourceAsStream(base + "/index.txt"),
"UTF-8"))) {
+ for (String line = reader.readLine(); line != null; line =
reader.readLine())
+ {
+ if (jarFile.getEntry(line)==null) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+}
diff --git
a/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/index.txt
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/index.txt
new file mode 100644
index 0000000..819a62a
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/index.txt
@@ -0,0 +1,4 @@
+test.txt
+sub1/test1.txt
+sub2/test1.txt
+sub2/test2.txt
\ No newline at end of file
diff --git
a/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub1/test1.txt
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub1/test1.txt
new file mode 100644
index 0000000..f5482f7
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub1/test1.txt
@@ -0,0 +1 @@
+Hello2
diff --git
a/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test1.txt
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test1.txt
new file mode 100644
index 0000000..741e5be
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test1.txt
@@ -0,0 +1 @@
+Hello3
diff --git
a/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test2.txt
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test2.txt
new file mode 100644
index 0000000..b4b42d0
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/sub2/test2.txt
@@ -0,0 +1 @@
+Hello4
diff --git
a/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/test.txt
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/test.txt
new file mode 100644
index 0000000..e965047
--- /dev/null
+++
b/featuremodel-unpack-extension/src/test/resources/org/apache/sling/feature/extension/unpack/test1/test.txt
@@ -0,0 +1 @@
+Hello