You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/04/27 12:36:22 UTC

[GitHub] [camel-quarkus] jamesnetherton commented on a change in pull request #2505: Google Storage support #2421

jamesnetherton commented on a change in pull request #2505:
URL: https://github.com/apache/camel-quarkus/pull/2505#discussion_r621163948



##########
File path: integration-tests/google-storage/src/test/java/org/apache/camel/quarkus/component/google/storage/it/GoogleStorageTestResource.java
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.quarkus.component.google.storage.it;
+
+import java.util.Collections;
+import java.util.Map;
+
+import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
+import org.apache.camel.util.CollectionHelper;
+import org.testcontainers.containers.GenericContainer;
+
+public class GoogleStorageTestResource implements QuarkusTestResourceLifecycleManager {
+
+    public static final int PORT = 4443;
+    public static final String CONTAINER_NAME = "fsouza/fake-gcs-server";
+
+    private GenericContainer<?> container;
+
+    @Override
+    public Map<String, String> start() {
+        //if there is a real configuration, don't start container

Review comment:
       We should probably add a call to `MockBackendUtils`. See example from AWS tests here:
   
   https://github.com/apache/camel-quarkus/blob/c50a7ae86148f34e286e218bba8fbff48b4c0d68/integration-tests-support/aws2/src/main/java/org/apache/camel/quarkus/test/support/aws2/Aws2TestResource.java#L47-L48

##########
File path: integration-tests/google-storage/src/main/java/org/apache/camel/quarkus/component/google/storage/it/GoogleStorageResource.java
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.quarkus.component.google.storage.it;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.google.cloud.storage.Blob;
+import com.google.cloud.storage.Bucket;
+import com.google.cloud.storage.CopyWriter;
+import com.google.cloud.storage.StorageOptions;
+import io.quarkiverse.googlecloudservices.storage.runtime.StorageProducer;
+import io.quarkus.arc.Unremovable;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.google.storage.GoogleCloudStorageComponent;
+import org.apache.camel.component.google.storage.GoogleCloudStorageConstants;
+import org.apache.camel.component.google.storage.GoogleCloudStorageOperations;
+import org.jboss.logging.Logger;
+
+@Path("/google-storage")
+@ApplicationScoped
+public class GoogleStorageResource {
+
+    public static final String DIRECT_POLLING = "direct:polling";
+
+    public static final String PARAM_PORT = "org.apache.camel.quarkus.component.googlr.storage.it.GoogleStorageClientProducer_port";
+
+    public static final String QUERY_OBJECT_NAME = "objectName";
+    public static final String QUERY_BUCKET = "bucketName";
+    public static final String QUERY_OPERATION = "operation";
+    public static final String QUERY_DESTINATION_BUCKET = "destinationBucket";
+    public static final String QUERY_DIRECT = "fromDirect";
+    public static final String QUERY_POLLING_ACTION = "pollingAction";
+
+    private static final Logger LOG = Logger.getLogger(GoogleStorageResource.class);
+    private static final String COMPONENT_GOOGLE_STORAGE = "google-storage";
+
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Inject
+    ConsumerTemplate consumerTemplate;
+
+    @Inject
+    StorageProducer sp;
+
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    @Named(COMPONENT_GOOGLE_STORAGE)
+    GoogleCloudStorageComponent produceComponent() throws IOException {
+        GoogleCloudStorageComponent gsc = new GoogleCloudStorageComponent();
+        if (!GoogleStorageHelper.isRealAccount()) {
+            String port = System.getProperty(GoogleStorageResource.PARAM_PORT);
+            gsc.getConfiguration().setStorageClient(StorageOptions.newBuilder()
+                    .setHost("http://localhost:" + port)
+                    .setProjectId("dummy-project-for-testing")
+                    .build()
+                    .getService());
+        } else {
+            gsc.getConfiguration().setStorageClient(sp.storage());
+        }
+        return gsc;
+    }
+
+    @Path("/operation")
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String operation(Map<String, Object> parameters,
+            @QueryParam(QUERY_OPERATION) String operation,
+            @QueryParam(QUERY_BUCKET) String bucketName) throws Exception {
+        GoogleCloudStorageOperations op = GoogleCloudStorageOperations.valueOf(operation);
+        String url = getBaseUrl(bucketName, "operation=" + op.toString());
+        final Object response = producerTemplate.requestBodyAndHeaders(url, null, parameters, Object.class);
+        if (response instanceof Blob) {
+            return new String(((Blob) response).getContent());
+        }
+        if (response instanceof CopyWriter) {
+            return new String(((CopyWriter) response).getResult().getContent());
+        }
+        if (response instanceof List) {
+            List l = (List) response;
+            return (String) l.stream().map(o -> {
+                if (o instanceof Bucket) {
+                    return ((Bucket) o).getName();
+                }
+                if (o instanceof Blob) {
+                    return ((Blob) o).getName();
+                }
+                return "null";
+            }).collect(Collectors.joining(","));
+        }
+        return String.valueOf(response);
+    }
+
+    @Path("/putObject")
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public Response putObject(String body,
+            @QueryParam(QUERY_BUCKET) String bucketName,
+            @QueryParam(QUERY_OBJECT_NAME) String objectName) throws Exception {
+        String url = getBaseUrl(bucketName, "autoCreateBucket=true");
+        final Blob response = producerTemplate.requestBodyAndHeader(url,
+                body,
+                GoogleCloudStorageConstants.OBJECT_NAME, objectName, Blob.class);
+        return Response
+                .created(new URI("https://camel.apache.org/"))
+                .entity(response.getName())
+                .build();
+    }
+
+    @Path("/startPolling")
+    @POST
+    public void startPolling(@QueryParam(QUERY_BUCKET) String bucketName,
+            @QueryParam(QUERY_POLLING_ACTION) String pollingAction,
+            @QueryParam(QUERY_DESTINATION_BUCKET) String destinationBucket) {
+        // use another thread for polling consumer to demonstrate that we can wait before
+        // the message is sent to the queue
+        Executors.newSingleThreadExecutor().execute(() -> {

Review comment:
       For this kind of thing, it's far easier to create a `RouteBuilder`, configure the `google-storage` consumer and direct any messages to a `seda` or `mock` endpoint which you can then retrieve via a `ConsumerTemplate` in the JAX-RS resource class.
   
   It makes the test flow much better...
   
   1. Create some storage object
   2. Retrieve storage object
   
   There's no need for executors, polling etc. 

##########
File path: extensions/google-storage/deployment/src/main/java/org/apache/camel/quarkus/component/google/storage/deployment/GoogleStorageProcessor.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.quarkus.component.google.storage.deployment;
+
+import com.google.api.client.googleapis.json.GoogleJsonError;
+import com.google.api.client.http.HttpHeaders;
+import com.google.api.client.json.GenericJson;
+import com.google.api.client.json.webtoken.JsonWebSignature;
+import com.google.api.client.json.webtoken.JsonWebToken;
+import com.google.api.client.util.GenericData;
+import com.google.api.services.storage.Storage;
+import com.google.api.services.storage.StorageRequest;
+import com.google.cloud.storage.Bucket;
+import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
+import io.quarkus.deployment.annotations.BuildProducer;
+import io.quarkus.deployment.annotations.BuildStep;
+import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
+import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem;
+import io.quarkus.deployment.builditem.FeatureBuildItem;
+import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
+import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
+import org.apache.camel.quarkus.component.google.storage.GoogleStorageEnforcer;
+import org.jboss.jandex.DotName;
+import org.jboss.jandex.IndexView;
+
+class GoogleStorageProcessor {
+
+    private static final String FEATURE = "camel-google-storage";
+
+    @BuildStep
+    FeatureBuildItem feature() {
+        return new FeatureBuildItem(FEATURE);
+    }
+
+    @BuildStep
+    ExtensionSslNativeSupportBuildItem activateSslNativeSupport() {
+        return new ExtensionSslNativeSupportBuildItem(FEATURE);
+    }
+
+    @BuildStep
+    public AdditionalBeanBuildItem storageEnforcer() {
+        return new AdditionalBeanBuildItem(GoogleStorageEnforcer.class);
+    }
+
+    @BuildStep
+    void registerForReflection(CombinedIndexBuildItem combinedIndex,

Review comment:
       Is this needed anymore? I was hoping the quarkiverse extension would take care of this.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org