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 2022/06/10 13:31:08 UTC

[GitHub] [camel] orpiske commented on a diff in pull request #7683: ✨ camel-whatsapp component

orpiske commented on code in PR #7683:
URL: https://github.com/apache/camel/pull/7683#discussion_r894511196


##########
components/camel-whatsapp/src/main/java/org/apache/camel/component/whatsapp/WhatsAppComponent.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.component.whatsapp;
+
+import java.net.http.HttpClient;
+import java.util.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.DefaultComponent;
+
+@Component("whatsapp")
+public class WhatsAppComponent extends DefaultComponent {
+
+    public static final String API_DEFAULT_URL = "https://graph.facebook.com";
+    public static final String API_DEFAULT_VERSION = "v13.0";
+
+    @Metadata(required = true, description = "Phone Number ID taken from WhatsApp Meta for Developers Dashboard")
+    private String phoneNumberId;
+    @Metadata(label = "security", secret = true, required = true,
+              description = "Authorization Token taken from WhatsApp Meta for Developers Dashboard")
+    private String authorizationToken;
+
+    @Metadata(label = "advanced", description = "Java 11 HttpClient implementation")
+    private HttpClient client;
+    @Metadata(label = "advanced", defaultValue = API_DEFAULT_URL,
+              description = "Can be used to set an alternative base URI, e.g. when you want to test the component against a mock WhatsApp API")
+    private String baseUri = API_DEFAULT_URL;
+    @Metadata(label = "advanced", defaultValue = API_DEFAULT_VERSION, description = "WhatsApp Cloud API version")
+    private String apiVersion = API_DEFAULT_VERSION;
+    @Metadata(description = "Webhook verify token", label = "advanced", secret = true)
+    private String webhookVerifyToken;
+
+    public WhatsAppComponent() {
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        WhatsAppConfiguration configuration = new WhatsAppConfiguration();
+
+        if (configuration.getBaseUri() == null) {
+            configuration.setBaseUri(baseUri);
+        }
+        if (configuration.getApiVersion() == null) {
+            configuration.setApiVersion(apiVersion);
+        }
+        if (configuration.getWebhookVerifyToken() == null) {
+            configuration.setWebhookVerifyToken(webhookVerifyToken);
+        }
+
+        configuration.setAuthorizationToken(authorizationToken);
+
+        if (remaining.endsWith("/")) {
+            remaining = remaining.substring(0, remaining.length() - 1);
+        }
+        configuration.setPhoneNumberId(remaining);
+
+        WhatsAppEndpoint endpoint = new WhatsAppEndpoint(uri, this, configuration, client);
+
+        setProperties(endpoint, parameters);
+
+        if (endpoint.getConfiguration().getAuthorizationToken() == null) {

Review Comment:
   Maybe use `ObjectHelper.isEmpty` here



##########
components/camel-whatsapp/src/main/java/org/apache/camel/component/whatsapp/WhatsAppEndpoint.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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.component.whatsapp;
+
+import java.net.http.HttpClient;
+import java.time.Duration;
+import java.util.List;
+
+import org.apache.camel.Category;
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.component.webhook.WebhookCapableEndpoint;
+import org.apache.camel.component.webhook.WebhookConfiguration;
+import org.apache.camel.component.whatsapp.service.WhatsAppServiceRestAPIAdapter;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.support.ScheduledPollEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Send messages.
+ */
+@UriEndpoint(firstVersion = "3.18.0", scheme = "whatsapp", title = "WhatsApp", syntax = "whatsapp:type",
+             category = {
+                     Category.CLOUD, Category.API,
+                     Category.CHAT },
+             headersClass = WhatsAppConstants.class)
+public class WhatsAppEndpoint extends ScheduledPollEndpoint implements WebhookCapableEndpoint {
+    private static final Logger LOG = LoggerFactory.getLogger(WhatsAppEndpoint.class);
+
+    @UriParam
+    private WhatsAppConfiguration configuration;
+
+    @UriParam(label = "advanced", description = "HttpClient implementation")
+    private HttpClient httpClient;
+    @UriParam(label = "advanced", description = "WhatsApp service implementation")
+    private WhatsAppService whatsappService;
+
+    private WebhookConfiguration webhookConfiguration;
+
+    public WhatsAppEndpoint(String endpointUri, Component component, WhatsAppConfiguration configuration, HttpClient client) {
+        super(endpointUri, component);
+        this.configuration = configuration;
+        this.httpClient = client;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        if (httpClient == null) {
+            httpClient
+                    = HttpClient.newBuilder().version(HttpClient.Version.HTTP_2).connectTimeout(Duration.ofSeconds(10)).build();
+        }
+        if (whatsappService == null) {
+            whatsappService = new WhatsAppServiceRestAPIAdapter(
+                    httpClient, configuration.getBaseUri(), configuration.getApiVersion(), configuration.getPhoneNumberId(),
+                    configuration.getAuthorizationToken());
+        }
+        LOG.debug("client {}" + httpClient);
+        LOG.debug("whatsappService {}" + whatsappService);

Review Comment:
   The logging is incorrect on these 2 lines. It should be:
   
   ```
   LOG.debug("client {}", httpClient);
   LOG.debug("whatsappService {}", whatsappService);
   ```



##########
components/camel-whatsapp/src/test/resources/log4j2.properties:
##########
@@ -0,0 +1,32 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = target/camel-whatsapp.log
+appender.file.layout.type = PatternLayout
+appender.file.layout.pattern = %d %-5p %c{1} - %m %n
+appender.console.type = Console
+appender.console.name = console
+appender.console.layout.type = PatternLayout
+appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
+logger.whatsapp.name = org.apache.camel.component.whatsapp
+logger.whatsapp.level = DEBUG
+rootLogger.level = INFO
+
+rootLogger.appenderRef.file.ref = file
+#rootLogger.appenderRef.console.ref = console

Review Comment:
   You can remove this line.



##########
components/camel-whatsapp/src/main/java/org/apache/camel/component/whatsapp/model/UploadMedia.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.component.whatsapp.model;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Objects;
+
+import org.apache.camel.RuntimeCamelException;
+
+public class UploadMedia {
+
+    private File file;
+    private String contentType;
+    private String name;
+    private InputStream fileStream;
+
+    public UploadMedia(File file, String contentType) {
+        Objects.nonNull(file);
+        Objects.nonNull(contentType);

Review Comment:
   I am not sure if that's what you intend here. The method `Objects.nonNull` returns a boolean ... maybe you want you to use `this.file = Objects.requireNonNull(file)`? Alternatively ... if this class is intended to be used only internally and both `file` and `contentType` are (almost) guaranteed to be non-null, an `assert file != null` can do the trick as well.



##########
components/camel-whatsapp/src/main/java/org/apache/camel/component/whatsapp/model/UploadMedia.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.component.whatsapp.model;
+
+import java.io.File;
+import java.io.InputStream;
+import java.util.Objects;
+
+import org.apache.camel.RuntimeCamelException;
+
+public class UploadMedia {
+
+    private File file;
+    private String contentType;
+    private String name;
+    private InputStream fileStream;
+
+    public UploadMedia(File file, String contentType) {
+        Objects.nonNull(file);
+        Objects.nonNull(contentType);
+        if (!file.exists()) {
+            throw new RuntimeCamelException("The file provided does not exists");
+        }
+
+        this.file = file;
+        this.contentType = contentType;
+    }
+
+    public UploadMedia(String name, InputStream fileStream, String contentType) {
+        Objects.nonNull(name);
+        Objects.nonNull(fileStream);
+        Objects.nonNull(contentType);

Review Comment:
   Please see my comment about `Objects.nonNull` above.



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@camel.apache.org

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