hokutor commented on a change in pull request #5753: URL: https://github.com/apache/camel/pull/5753#discussion_r660841796
########## File path: components/camel-huawei/camel-huaweicloud-obs/src/main/java/org/apache/camel/component/huaweicloud/obs/OBSProducer.java ########## @@ -0,0 +1,225 @@ +/* + * 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.obs; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.obs.services.ObsClient; +import com.obs.services.exception.ObsException; +import com.obs.services.model.CreateBucketRequest; +import com.obs.services.model.HeaderResponse; +import com.obs.services.model.ListBucketsRequest; +import com.obs.services.model.ListBucketsResult; +import com.obs.services.model.ObsBucket; +import org.apache.camel.Exchange; +import org.apache.camel.component.huaweicloud.obs.constants.OBSConstants; +import org.apache.camel.component.huaweicloud.obs.constants.OBSOperations; +import org.apache.camel.component.huaweicloud.obs.constants.OBSProperties; +import org.apache.camel.component.huaweicloud.obs.models.ClientConfigurations; +import org.apache.camel.component.huaweicloud.obs.models.OBSRegion; +import org.apache.camel.support.DefaultProducer; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OBSProducer extends DefaultProducer { + private static final Logger LOG = LoggerFactory.getLogger(OBSProducer.class); + private OBSEndpoint endpoint; + private ClientConfigurations clientConfigurations; + private ObsClient obsClient; + private Gson gson; + + public OBSProducer(OBSEndpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + @Override + protected void doInit() throws Exception { + super.doInit(); + this.clientConfigurations = new ClientConfigurations(); + this.obsClient = this.endpoint.initClient(); + this.gson = new Gson(); + } + + public void process(Exchange exchange) throws Exception { + updateClientConfigs(exchange); + + try { + switch (clientConfigurations.getOperation()) { + case OBSOperations.LIST_BUCKETS: + listBuckets(exchange); + break; + case OBSOperations.CREATE_BUCKET: + createBucket(exchange); + break; + case OBSOperations.DELETE_BUCKET: + deleteBucket(exchange); + break; + default: + throw new UnsupportedOperationException( + String.format("%s is not a supported operation", clientConfigurations.getOperation())); + } + } catch (ObsException e) { + if (LOG.isErrorEnabled()) { + LOG.error("Failed to perform operation"); + LOG.error("HTTP Code: " + e.getResponseCode()); + LOG.error("Error Code: " + e.getErrorCode()); + LOG.error("Error Message: " + e.getErrorMessage()); + LOG.error("Request ID: " + e.getErrorRequestId()); + LOG.error("Host ID: " + e.getErrorHostId()); + } + throw e; + } + } + + /** + * Perform list buckets operation + * + * @param exchange + */ + private void listBuckets(Exchange exchange) throws ObsException { + // invoke list buckets method and map response object to exchange body + ListBucketsRequest request = new ListBucketsRequest(); + ListBucketsResult response = obsClient.listBucketsV2(request); + exchange.getMessage().setBody(gson.toJson(response.getBuckets())); + } + + /** + * Perform create bucket operation + * + * @param exchange + */ + private void createBucket(Exchange exchange) throws ObsException { + CreateBucketRequest request = null; + + // checking if user inputted exchange body containing bucket information. Body must be a CreateBucketRequest or a valid JSON string (Advanced users) + Object exchangeBody = exchange.getMessage().getBody(); + if (exchangeBody instanceof CreateBucketRequest) { + request = (CreateBucketRequest) exchangeBody; + } else if (exchangeBody instanceof String) { + String strBody = (String) exchangeBody; + try { + request = new ObjectMapper().readValue(strBody, CreateBucketRequest.class); + } catch (JsonProcessingException e) { + if (LOG.isWarnEnabled()) { + LOG.warn( + "String request body must be a valid JSON representation of a CreateBucketRequest. Attempting to create a bucket from endpoint parameters"); + } + } + } + + // if no CreateBucketRequest was found in the exchange body, then create one from endpoint parameters (Basic users) + if (request == null) { + // check for bucket name, which is mandatory to create a new bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketName())) { + if (LOG.isErrorEnabled()) { Review comment: Done. Removed LOG error/warn checks -- 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-unsubscr...@camel.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org