You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/08 06:09:26 UTC
[camel] 01/04: CAMEL-18077: camel-github - Add gist resource resolver
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit b70c82f974247695e06fc6f4044c18d76861bed4
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat May 7 10:15:59 2022 +0200
CAMEL-18077: camel-github - Add gist resource resolver
---
.../org/apache/camel/resource-resolver/gist | 2 +
.../java/org/apache/camel/github/GistResource.java | 62 ++++++++++++++++++++
.../apache/camel/github/GistResourceResolver.java | 66 ++++++++++++++++++++++
.../camel/github/GistResourceResolverTest.java | 39 +++++++++++++
4 files changed, 169 insertions(+)
diff --git a/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist b/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist
new file mode 100644
index 00000000000..51ad8664f7b
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/generated/resources/META-INF/services/org/apache/camel/resource-resolver/gist
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.github.GistResourceResolver
diff --git a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java
new file mode 100644
index 00000000000..ce20cc2322e
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResource.java
@@ -0,0 +1,62 @@
+/*
+ * 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.github;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.ResourceSupport;
+
+public final class GistResource extends ResourceSupport {
+
+ private final CamelContext camelContext;
+ private byte[] cache;
+ private boolean init;
+
+ public GistResource(CamelContext camelContext, String location) {
+ super("gist", location);
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ public boolean exists() {
+ if (!init) {
+ try {
+ URL u = new URL(getLocation());
+ try (InputStream is = u.openStream()) {
+ cache = camelContext.getTypeConverter().tryConvertTo(byte[].class, is);
+ }
+ } catch (Exception e) {
+ // ignore
+ }
+ init = true;
+ }
+ return cache != null;
+ }
+
+ @Override
+ public InputStream getInputStream() throws IOException {
+ if (exists()) {
+ return new ByteArrayInputStream(cache);
+ } else {
+ return null;
+ }
+ }
+}
diff --git a/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
new file mode 100644
index 00000000000..b1a3f51eaad
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/main/java/org/apache/camel/github/GistResourceResolver.java
@@ -0,0 +1,66 @@
+/*
+ * 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.github;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.spi.annotations.ResourceResolver;
+import org.apache.camel.support.service.ServiceSupport;
+
+@ResourceResolver("gist")
+public class GistResourceResolver extends ServiceSupport implements org.apache.camel.spi.ResourceResolver {
+
+ // gist:davsclaus:477ddff5cdeb1ae03619aa544ce47e92
+ // https://gist.githubusercontent.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92/raw
+ private static final String GIST_URL = "https://gist.githubusercontent.com/%s/%s/raw";
+
+ private CamelContext camelContext;
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ public String getSupportedScheme() {
+ return "gist";
+ }
+
+ @Override
+ public Resource resolve(String location) {
+ String[] parts = location.split(":");
+ // scheme not in use as its gist
+ String user = null;
+ String gid = null;
+
+ if (parts.length == 3) {
+ user = parts[1];
+ gid = parts[2];
+ }
+ if (user == null || gid == null) {
+ throw new IllegalArgumentException(location);
+ }
+
+ String target = String.format(GIST_URL, user, gid);
+ return new GistResource(camelContext, target);
+ }
+}
diff --git a/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
new file mode 100644
index 00000000000..8f84784927f
--- /dev/null
+++ b/components/camel-resourceresolver-github/src/test/java/org/apache/camel/github/GistResourceResolverTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.github;
+
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.Resource;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class GistResourceResolverTest extends CamelTestSupport {
+
+ @Test
+ public void testGist() throws Exception {
+ ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+ Resource res = ecc.getResourceLoader().resolveResource("gist:davsclaus:123");
+ assertNotNull(res);
+ assertFalse(res.exists());
+ assertEquals("https://gist.githubusercontent.com/davsclaus/123/raw", res.getLocation());
+ }
+
+}