You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2019/03/12 10:27:30 UTC

[camel-k-runtime] branch master updated: fix platform stream handler

This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git


The following commit(s) were added to refs/heads/master by this push:
     new ca28518  fix platform stream handler
     new 0fe8b58  Merge pull request #33 from lburgazzoli/platform-stream-handler
ca28518 is described below

commit ca28518d910aa8383b4d3640c2fd289006a0eed8
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Mar 12 08:22:00 2019 +0100

    fix platform stream handler
---
 camel-k-runtime-core/pom.xml                       |  33 ++++
 .../camel/k/support/PlatformStreamHandler.java     | 169 ++++++++++++++-------
 .../camel/k/support/PlatformStreamHandlerTest.java |  82 ++++++++++
 .../src/test/resources/log4j2-test.xml             |  17 +++
 .../src/test/resources/my-cp-resource.txt          |   1 +
 .../src/test/resources/my-file-resource.txt        |   1 +
 6 files changed, 248 insertions(+), 55 deletions(-)

diff --git a/camel-k-runtime-core/pom.xml b/camel-k-runtime-core/pom.xml
index 19e96e7..50e41e4 100644
--- a/camel-k-runtime-core/pom.xml
+++ b/camel-k-runtime-core/pom.xml
@@ -82,8 +82,41 @@
             <version>${assertj.version}</version>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-core</artifactId>
+            <version>${log4j2.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.logging.log4j</groupId>
+            <artifactId>log4j-slf4j-impl</artifactId>
+            <version>${log4j2.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <environmentVariables>
+                        <CAMEL_K_RESOURCE_001>my env content</CAMEL_K_RESOURCE_001>
+                        <MY_ENV_RESOURCE_TXT>env:CAMEL_K_RESOURCE_001</MY_ENV_RESOURCE_TXT>
+                        <CAMEL_K_RESOURCE_002>H4sIAFlah1wAA8utVEjOzy0oSi0uTk1RSM0rA3LzSlLzSrgAUbkzGRoAAAA=</CAMEL_K_RESOURCE_002>
+                        <MY_COMPRESSED_ENV_RESOURCE_TXT>env:CAMEL_K_RESOURCE_002?compression=true</MY_COMPRESSED_ENV_RESOURCE_TXT>
+                    </environmentVariables>
+                    <systemPropertyVariables>
+                        <root>${project.basedir}</root>
+                    </systemPropertyVariables>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
     <profiles>
         <profile>
             <id>camel3</id>
diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java
index fbd0403..838877f 100644
--- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java
@@ -17,7 +17,6 @@
 package org.apache.camel.k.support;
 
 import java.io.ByteArrayInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
@@ -32,8 +31,8 @@ import java.util.Map;
 import java.util.zip.GZIPInputStream;
 
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
-import org.apache.commons.lang3.StringUtils;
 
 public class PlatformStreamHandler extends URLStreamHandler {
     public static void configure() {
@@ -49,68 +48,128 @@ public class PlatformStreamHandler extends URLStreamHandler {
 
             @Override
             public InputStream getInputStream() throws IOException {
-                InputStream is = null;
+                String path = url.getPath();
+                String type = StringHelper.before(path, ":");
+                String query = StringHelper.after(path, "?");
 
-                // check if the file exists
-                Path path = Paths.get(url.getPath());
-                if (Files.exists(path)) {
-                    is = Files.newInputStream(path);
+                if (ObjectHelper.isNotEmpty(type)) {
+                    path = StringHelper.after(path, ":");
                 }
-
-                // check if the file exists in classpath
-                if (is == null) {
-                    is = ObjectHelper.loadResourceAsStream(url.getPath());
+                if (ObjectHelper.isNotEmpty(query)) {
+                    path = StringHelper.before(path, "?");
                 }
 
-                if (is == null) {
-                    String name = getURL().getPath().toUpperCase();
-                    name = name.replace(" ", "_");
-                    name = name.replace(".", "_");
-                    name = name.replace("-", "_");
-
-                    String envName = System.getenv(name);
-                    String envType = StringUtils.substringBefore(envName, ":");
-                    String envQuery = StringUtils.substringAfter(envName, "?");
-
-                    envName = StringUtils.substringAfter(envName, ":");
-                    envName = StringUtils.substringBefore(envName, "?");
-
-                    if (envName != null) {
-                        try {
-                            final Map<String, Object> params = URISupport.parseQuery(envQuery);
-                            final boolean compression = Boolean.valueOf((String) params.get("compression"));
-
-                            if (StringUtils.equals(envType, "env")) {
-                                String data = System.getenv(envName);
-
-                                if (data == null) {
-                                    throw new IllegalArgumentException("Unknown env var: " + envName);
-                                }
-
-                                is = new ByteArrayInputStream(data.getBytes());
-                            } else if (StringUtils.equals(envType, "file")) {
-                                Path data = Paths.get(envName);
-
-                                if (!Files.exists(data)) {
-                                    throw new FileNotFoundException(envName);
-                                }
-
-                                is = Files.newInputStream(data);
-                            } else if (StringUtils.equals(envType, "classpath")) {
-                                is = ObjectHelper.loadResourceAsStream(envName);
-                            }
-
-                            if (is != null && compression) {
-                                is = new GZIPInputStream(Base64.getDecoder().wrap(is));
-                            }
-                        } catch (URISyntaxException e) {
-                            throw new IOException(e);
-                        }
+                boolean compression = hasCompression(query);
+
+                InputStream is;
+
+                if (type != null) {
+                    switch (type) {
+                    case "env":
+                        is = resolveEnv(path);
+                        break;
+                    case "file":
+                        is = resolveFile(path);
+                        break;
+                    case "classpath":
+                        is = resolveClasspath(path);
+                        break;
+                    default:
+                        throw new IllegalArgumentException("Unsupported delegated resolver: " + type);
+                    }
+                } else {
+                    is = resolveEnv(path);
+
+                    if (is == null) {
+                        is = resolveFile(path);
+                    }
+                    if (is == null) {
+                        is = resolveClasspath(path);
                     }
                 }
 
+                if (is != null && compression) {
+                    is = new GZIPInputStream(Base64.getDecoder().wrap(is));
+                }
+
                 return is;
             }
         };
     }
+
+    private static InputStream resolveEnv(String path) {
+        String name = path.toUpperCase();
+        name = name.replace(" ", "_");
+        name = name.replace(".", "_");
+        name = name.replace("-", "_");
+
+        String ref = System.getenv(name);
+
+        if (ref == null) {
+            return null;
+        }
+
+        String refType = StringHelper.before(ref, ":");
+        String refName = StringHelper.after(ref, ":");
+        String refQuery = StringHelper.after(refName, "?");
+        boolean compression = hasCompression(refQuery);
+
+        if (ObjectHelper.isNotEmpty(refQuery)) {
+            refName = StringHelper.before(refName, "?");
+        }
+
+        InputStream is;
+
+        switch (refType) {
+        case "env":
+            String content = System.getenv(refName);
+            is = new ByteArrayInputStream(content.getBytes());
+            break;
+        case "file":
+            is = resolveFile(refName);
+            break;
+        case "classpath":
+            is = resolveClasspath(refName);
+            break;
+        default:
+            throw new IllegalArgumentException("Unsupported delegated resolver: " + refName);
+        }
+
+        if (is != null && compression) {
+            try {
+                is = new GZIPInputStream(Base64.getDecoder().wrap(is));
+            } catch (IOException e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
+
+        return is;
+    }
+
+    private static InputStream resolveFile(String path) {
+        Path data = Paths.get(path);
+
+        if (!Files.exists(data)) {
+            return null;
+        }
+
+        try {
+            return Files.newInputStream(data);
+        } catch (IOException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
+    private static InputStream resolveClasspath(String path) {
+        return ObjectHelper.loadResourceAsStream(path);
+    }
+
+    private static boolean hasCompression(String query) {
+        try {
+            Map<String, Object> params = URISupport.parseQuery(query);
+            return Boolean.valueOf((String) params.get("compression"));
+        } catch (URISyntaxException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
 }
diff --git a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/PlatformStreamHandlerTest.java b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/PlatformStreamHandlerTest.java
new file mode 100644
index 0000000..0b75d93
--- /dev/null
+++ b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/PlatformStreamHandlerTest.java
@@ -0,0 +1,82 @@
+/**
+ * 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.camel.k.support;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.k.adapter.Resources;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PlatformStreamHandlerTest {
+    static {
+        PlatformStreamHandler.configure();
+    }
+
+    @Test
+    public void testClasspathHandler() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:my-cp-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my cp content");
+        }
+
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:classpath:my-cp-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my cp content");
+        }
+    }
+
+    @Test
+    public void testFileHandler() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        String root = System.getProperty("root") + "/src/test/resources";
+
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:" + root + "/my-file-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my file content");
+        }
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:file:" + root + "/my-file-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my file content");
+        }
+    }
+
+    @Test
+    public void testEnvHandler() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:my-env-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my env content");
+        }
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:env:my-env-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content).isEqualTo("my env content");
+        }
+        try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:env:my-compressed-env-resource.txt")) {
+            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
+            assertThat(content.trim()).isEqualTo("my compressed env content");
+        }
+    }
+}
diff --git a/camel-k-runtime-core/src/test/resources/log4j2-test.xml b/camel-k-runtime-core/src/test/resources/log4j2-test.xml
new file mode 100644
index 0000000..9af8521
--- /dev/null
+++ b/camel-k-runtime-core/src/test/resources/log4j2-test.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="INFO">
+  <Appenders>
+    <Console name="STDOUT" target="SYSTEM_OUT">
+      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%t|%c{1} - %msg%n"/>
+    </Console>
+    <Null name="NONE"/>
+  </Appenders>
+
+  <Loggers>
+    <Root level="INFO">
+      <!--<AppenderRef ref="STDOUT"/>-->
+      <AppenderRef ref="NONE"/>
+    </Root>
+  </Loggers>
+
+</Configuration>
\ No newline at end of file
diff --git a/camel-k-runtime-core/src/test/resources/my-cp-resource.txt b/camel-k-runtime-core/src/test/resources/my-cp-resource.txt
new file mode 100644
index 0000000..64ec491
--- /dev/null
+++ b/camel-k-runtime-core/src/test/resources/my-cp-resource.txt
@@ -0,0 +1 @@
+my cp content
\ No newline at end of file
diff --git a/camel-k-runtime-core/src/test/resources/my-file-resource.txt b/camel-k-runtime-core/src/test/resources/my-file-resource.txt
new file mode 100644
index 0000000..63d3cca
--- /dev/null
+++ b/camel-k-runtime-core/src/test/resources/my-file-resource.txt
@@ -0,0 +1 @@
+my file content
\ No newline at end of file