From 6de97a358b1836c67dde75daa9cdc04f1f7c4465 Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Sat, 18 May 2019 13:04:14 +0200 Subject: [PATCH] chore(cleanup): remove platform stream handler --- .../k/support/PlatformStreamHandler.java | 175 ------------------ .../k/support/PlatformStreamHandlerTest.java | 82 -------- .../org/apache/camel/k/jvm/Application.java | 11 -- .../org/apache/camel/k/jvm/RuntimeTest.java | 21 --- 4 files changed, 289 deletions(-) delete mode 100644 camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java delete mode 100644 camel-k-runtime-core/src/test/java/org/apache/camel/k/support/PlatformStreamHandlerTest.java 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 deleted file mode 100644 index 838877fd0..000000000 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PlatformStreamHandler.java +++ /dev/null @@ -1,175 +0,0 @@ -/** - * 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.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Base64; -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; - -public class PlatformStreamHandler extends URLStreamHandler { - public static void configure() { - URL.setURLStreamHandlerFactory(protocol -> "platform".equals(protocol) ? new PlatformStreamHandler() : null); - } - - @Override - protected URLConnection openConnection(URL url) throws IOException { - return new URLConnection(url) { - @Override - public void connect() throws IOException { - } - - @Override - public InputStream getInputStream() throws IOException { - String path = url.getPath(); - String type = StringHelper.before(path, ":"); - String query = StringHelper.after(path, "?"); - - if (ObjectHelper.isNotEmpty(type)) { - path = StringHelper.after(path, ":"); - } - if (ObjectHelper.isNotEmpty(query)) { - path = StringHelper.before(path, "?"); - } - - 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 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 deleted file mode 100644 index 0b75d9349..000000000 --- a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/PlatformStreamHandlerTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * 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-jvm/src/main/java/org/apache/camel/k/jvm/Application.java b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java index c28d5f916..4cf65a116 100644 --- a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java +++ b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java @@ -19,7 +19,6 @@ import java.util.ServiceLoader; import org.apache.camel.k.Runtime; -import org.apache.camel.k.support.PlatformStreamHandler; import org.apache.camel.k.support.RuntimeSupport; public class Application { @@ -33,16 +32,6 @@ public class Application { // We now support setting the logging level only // ApplicationSupport.configureLogging(); - - // - // Install a custom protocol handler to support discovering resources - // from the platform i.e. in knative, resources are provided through - // env var as it is not possible to mount config maps / secrets. - // - // TODO: we should remove as soon as we get a knative version that - // includes https://github.com/knative/serving/pull/3061 - // - PlatformStreamHandler.configure(); } public static void main(String[] args) throws Exception { diff --git a/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java b/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java index 0070e9864..805fcd516 100644 --- a/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java +++ b/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java @@ -27,7 +27,6 @@ import org.apache.camel.k.listener.ContextConfigurer; import org.apache.camel.k.listener.ContextLifecycleConfigurer; import org.apache.camel.k.listener.RoutesConfigurer; -import org.apache.camel.k.support.PlatformStreamHandler; import org.apache.camel.model.ModelCamelContext; import org.apache.camel.util.ObjectHelper; import org.apache.commons.io.IOUtils; @@ -84,24 +83,4 @@ void testLoadRouteAndRest() throws Exception { runtime.stop(); } } - - - @Test - void testLoadResource() throws Exception { - PlatformStreamHandler.configure(); - - CamelContext context = new ApplicationRuntime().getContext(); - - try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:my-resource.txt")) { - String content = IOUtils.toString(is, Charset.defaultCharset()); - - assertThat(content).isEqualTo("value from file resource"); - } - - try (InputStream is = Resources.resolveResourceAsInputStream(context, "platform:my-other-resource.txt")) { - String content = IOUtils.toString(is, Charset.defaultCharset()); - - assertThat(content).isEqualTo("value from env"); - } - } }