mathewsreji commented on a change in pull request #4900:
URL: https://github.com/apache/camel/pull/4900#discussion_r565029333



##########
File path: 
components/camel-huaweicloud-smn/src/main/java/org/apache/camel/component/huaweicloud/smn/SimpleNotificationProducer.java
##########
@@ -0,0 +1,366 @@
+/*
+ * 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.huaweicloud.smn;
+
+import java.util.HashMap;
+
+import com.huaweicloud.sdk.core.auth.BasicCredentials;
+import com.huaweicloud.sdk.core.http.HttpConfig;
+import com.huaweicloud.sdk.smn.v2.SmnClient;
+import com.huaweicloud.sdk.smn.v2.model.PublishMessageRequest;
+import com.huaweicloud.sdk.smn.v2.model.PublishMessageRequestBody;
+import com.huaweicloud.sdk.smn.v2.model.PublishMessageResponse;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.huaweicloud.smn.constants.SmnConstants;
+import org.apache.camel.component.huaweicloud.smn.constants.SmnOperations;
+import org.apache.camel.component.huaweicloud.smn.constants.SmnServices;
+import org.apache.camel.component.huaweicloud.smn.models.ClientConfigurations;
+import org.apache.camel.support.DefaultProducer;
+import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SimpleNotificationProducer extends DefaultProducer {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SimpleNotificationProducer.class);
+    private SimpleNotificationEndpoint simpleNotificationEndpoint;
+    private SmnClient smnClient;
+    private ClientConfigurations clientConfigurations;
+
+    public SimpleNotificationProducer(SimpleNotificationEndpoint endpoint) {
+        super(endpoint);
+        this.simpleNotificationEndpoint = endpoint;
+    }
+
+    public void process(Exchange exchange) throws Exception {
+
+        /**
+         * the produce method is invoked when a message arrives at producer 
endpoint (lazy init of SmnClient on first
+         * message)
+         */
+        if (this.smnClient == null) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Initializing the SmnClient");
+            }
+            validateAndInitializeSmnClient(simpleNotificationEndpoint);
+        }
+
+        String service = simpleNotificationEndpoint.getSmnService();
+
+        if (!ObjectHelper.isEmpty(service)) {
+            switch (service) {
+                case SmnServices.PUBLISH_MESSAGE:
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Using message publishing service");
+                    }
+                    
performPublishMessageServiceOperations(simpleNotificationEndpoint, exchange);
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Completed publishing message");
+                    }
+                    break;
+                default:
+                    if (LOG.isErrorEnabled()) {
+                        LOG.error("Unsupported service name {}", service);
+                    }
+                    throw new 
UnsupportedOperationException(String.format("service %s is not a supported 
service", service));
+            }
+        } else {
+            if (LOG.isErrorEnabled()) {
+                LOG.error("Service name is null/empty");
+            }
+            throw new IllegalStateException("service name cannot be 
null/empty");
+        }
+    }
+
+    /**
+     * Publish message service operations
+     *
+     * @param endpoint
+     * @param exchange
+     */
+    private void 
performPublishMessageServiceOperations(SimpleNotificationEndpoint endpoint, 
Exchange exchange) {
+        PublishMessageResponse response;
+
+        PublishMessageRequestBody apiBody;
+        this.clientConfigurations = validateServiceConfigurations(endpoint, 
exchange);
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Checking operation name");
+        }
+        switch (clientConfigurations.getOperation()) {
+
+            case SmnOperations.PUBLISH_AS_TEXT_MESSAGE:
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Publishing as text message");
+                }
+                apiBody = new PublishMessageRequestBody()
+                        
.withMessage(exchange.getMessage().getBody(String.class))
+                        .withSubject(clientConfigurations.getSubject())
+                        
.withTimeToLive(String.valueOf(clientConfigurations.getMessageTtl()));
+
+                response = smnClient.publishMessage(new PublishMessageRequest()
+                        .withBody(apiBody)
+                        .withTopicUrn(clientConfigurations.getTopicUrn()));
+                break;
+
+            case SmnOperations.PUBLISH_AS_TEMPLATED_MESSAGE:
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Publishing as templated message");
+                }
+                apiBody = new PublishMessageRequestBody()
+                        
.withMessage(exchange.getMessage().getBody(String.class))
+                        .withSubject(clientConfigurations.getSubject())
+                        
.withTimeToLive(String.valueOf(clientConfigurations.getMessageTtl()))
+                        .withMessageTemplateName((String) 
exchange.getProperty("CamelHwCloudSmnTemplateName"))
+                        .withTags((HashMap<String, String>) 
exchange.getProperty("CamelHwCloudSmnTemplateTags"))
+                        
.withTimeToLive(String.valueOf(clientConfigurations.getMessageTtl()));
+
+                response = smnClient.publishMessage(new PublishMessageRequest()
+                        .withBody(apiBody)
+                        .withTopicUrn(clientConfigurations.getTopicUrn()));
+                break;
+
+            default:
+                throw new UnsupportedOperationException(

Review comment:
       > also here a default operation instead of throwing an error
   
   @omarsmak 
   After analysis, I suggest it would be nice to keep it concrete with 
mandatory/right operation name because input for each operations are slightly 
different. if  user misses out on correct inputs/properties, then API might 
error out. Sdk team will be expanding support for more services and operations. 
so once we have a bigger list, we could analyze on a good candidate to become a 
default service/operation




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to