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 95f1951  SLING-9670: add a new unpack extension for the feature model.
95f1951 is described below

commit 95f1951d6ec7310b018bca2f3af8f799e6ccf21c
Author: Karl Pauls <[email protected]>
AuthorDate: Thu Aug 20 14:49:40 2020 +0200

    SLING-9670: add a new unpack extension for the feature model.
---
 .../sling/feature/extension/unpack/Unpack.java     |  3 +-
 .../extension/unpack/impl/converter/Converter.java | 89 ++++++++++++++++++++++
 .../unpack/impl/converter/ConverterTest.java       | 53 +++++++++++++
 3 files changed, 143 insertions(+), 2 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 5b9172e..4ffe5df 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
@@ -66,8 +66,7 @@ public class Unpack {
         return handle(extension, provider, this::unpack);
     }
 
-    public boolean handle(Extension extension, ArtifactProvider provider, 
BiConsumer<URL, Map<String, Object>> handler)
-    {
+    public boolean handle(Extension extension, ArtifactProvider provider, 
BiConsumer<URL, Map<String, Object>> handler) {
         if (extension.getType() == ExtensionType.ARTIFACTS &&
             this.registry.containsKey(extension.getName())) {
             for (Artifact artifact : extension.getArtifacts()) {
diff --git 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/converter/Converter.java
 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/converter/Converter.java
new file mode 100644
index 0000000..13bb9d2
--- /dev/null
+++ 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/converter/Converter.java
@@ -0,0 +1,89 @@
+/*
+ * 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.impl.converter;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.io.json.FeatureJSONWriter;
+
+public class Converter {
+    public static void main(String[] args) throws Exception {
+        if (args.length > 1) {
+            File base = new File(args[0]);
+            if (!base.isDirectory() && !base.mkdirs()) {
+                throw new IOException("Unable to create base dir: " + base);
+            }
+            Feature feature = new Feature(new ArtifactId("cm", "cm-fonts", 
"0.0.1", null,  "slingosgifeature"));
+            Extension extension = new Extension(ExtensionType.ARTIFACTS, 
"user-fonts", ExtensionState.REQUIRED);
+            for (int i = 1; i < args.length; i++) {
+                String arg = args[i];
+                URL url = new URL(arg);
+                File tmp = File.createTempFile("fonts", ".zip");
+                try (DigestInputStream inputStream = new 
DigestInputStream(url.openStream(), MessageDigest.getInstance("SHA-512"))) {
+                    Files.copy(inputStream, tmp.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+                    String digest = 
bytesToHex(inputStream.getMessageDigest().digest());
+
+                    Artifact artifact = new Artifact(new ArtifactId("cm", 
"cm-fonts", "0.0.1", digest,  "zip"));
+                    extension.getArtifacts().add(artifact);
+                    File target = new File(base, artifact.getId().toMvnPath());
+                    if (!target.getParentFile().isDirectory() && 
!target.getParentFile().mkdirs()) {
+                        throw new IOException("Unable to create parent dir: " 
+ target.getParentFile());
+                    }
+                    Files.move(tmp.toPath(), target.toPath());
+                }
+            }
+            feature.getExtensions().add(extension);
+            File target = new File(base, feature.getId().toMvnPath());
+            if (!target.getParentFile().isDirectory() && 
!target.getParentFile().mkdirs()) {
+                throw new IOException("Unable to create parent dir: " + 
target.getParentFile());
+            }
+            try (Writer writer = new OutputStreamWriter(new 
FileOutputStream(target), StandardCharsets.UTF_8)) {
+                FeatureJSONWriter.write(writer, feature);
+            }
+        }
+    }
+
+    private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes();
+
+    static String bytesToHex(byte[] bytes) {
+        byte[] hexChars = new byte[bytes.length * 2];
+        for (int j = 0; j < bytes.length; j++) {
+            int v = bytes[j] & 0xFF;
+            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
+            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
+        }
+        return new String(hexChars, StandardCharsets.UTF_8);
+    }
+}
diff --git 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/converter/ConverterTest.java
 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/converter/ConverterTest.java
new file mode 100644
index 0000000..533f57a
--- /dev/null
+++ 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/converter/ConverterTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.impl.converter;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+
+import org.apache.sling.feature.ArtifactId;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ConverterTest
+{
+    @Test
+    public void testConverter() throws Exception
+    {
+        File base = File.createTempFile("test", "fonts");
+        base.delete();
+        base.mkdirs();
+
+        File repo = new File(base, "repository");
+        File fontZip = new File(base, "fonts one-1.0.0 bar.zip");
+        String digest;
+        try (DigestOutputStream outputStream = new DigestOutputStream(new 
FileOutputStream(fontZip), MessageDigest.getInstance("SHA-512"))) {
+            outputStream.write(0xff);
+            digest = 
Converter.bytesToHex(outputStream.getMessageDigest().digest());
+        }
+
+        Converter.main(new String[]{repo.getPath(), 
fontZip.toURI().toURL().toString()});
+
+        Assert.assertTrue(new File(repo, 
ArtifactId.fromMvnId("cm:cm-fonts:slingosgifeature:0.0.1").toMvnPath()).exists());
+
+        Assert.assertTrue(new File(repo, 
ArtifactId.fromMvnId("cm:cm-fonts:zip:" + digest + 
":0.0.1").toMvnPath()).exists());
+    }
+}

Reply via email to