davsclaus commented on a change in pull request #5753: URL: https://github.com/apache/camel/pull/5753#discussion_r660088871
########## 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()) { + LOG.error("No bucket name given"); + } + throw new IllegalArgumentException("Bucket name is mandatory to create bucket"); + } + + // check for bucket location, which is optional to create a new bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketLocation())) { + if (LOG.isWarnEnabled()) { Review comment: Remove this as you have WARN logging for every requrest ########## File path: components/camel-huawei/camel-huaweicloud-obs/pom.xml ########## @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="UTF-8"?> Review comment: Add missing LICENSE header ########## File path: components/camel-huawei/camel-huaweicloud-obs/src/main/java/org/apache/camel/component/huaweicloud/obs/models/OBSRegion.java ########## @@ -0,0 +1,88 @@ +/* + * 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.models; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.util.ObjectHelper; + +/** + * Class containing Huawei Cloud OBS regions and endpoints + */ +public final class OBSRegion { Review comment: Is an ENUM not a better class for this? Then you can also use Camel's enum support with automatic type conversion and avoid any mixed case problems, eg CN-NORTH-1 vs cn-north-1 ########## File path: components/camel-huawei/camel-huaweicloud-obs/src/main/java/org/apache/camel/component/huaweicloud/obs/models/ClientConfigurations.java ########## @@ -0,0 +1,58 @@ +/* + * 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.models; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to combine parameters which can be passed through exchange properties and endpoint parameters to avoid checking + * both each time they are used + */ +public class ClientConfigurations { + private static final Logger LOG = LoggerFactory.getLogger(ClientConfigurations.class.getName()); Review comment: Remove not used logger ########## 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: Remove this ########## 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()) { + LOG.error("No bucket name given"); + } + throw new IllegalArgumentException("Bucket name is mandatory to create bucket"); + } + + // check for bucket location, which is optional to create a new bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketLocation())) { + if (LOG.isWarnEnabled()) { + LOG.warn("No bucket location given, defaulting to '" + OBSConstants.DEFAULT_LOCATION + "'"); + } + clientConfigurations.setBucketLocation(OBSConstants.DEFAULT_LOCATION); + } + // verify valid bucket location + OBSRegion.checkValidRegion(clientConfigurations.getBucketLocation()); + + request = new CreateBucketRequest(clientConfigurations.getBucketName(), clientConfigurations.getBucketLocation()); + } + + // invoke create bucket method and map response object to exchange body + ObsBucket response = obsClient.createBucket(request); + exchange.getMessage().setBody(gson.toJson(response)); + } + + /** + * Perform delete bucket operation + * + * @param exchange + */ + private void deleteBucket(Exchange exchange) throws ObsException { + // check for bucket name, which is mandatory to delete a bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketName())) { + if (LOG.isErrorEnabled()) { Review comment: Remove this ########## File path: parent/pom.xml ########## @@ -239,6 +239,7 @@ <httpcore4-version>4.4.14</httpcore4-version> <httpclient4-version>4.5.13</httpclient4-version> <httpasyncclient-version>4.1.4</httpasyncclient-version> + <huaweicloud-obs-version>3.21.4.1</huaweicloud-obs-version> Review comment: Use same indent as the other lines ########## 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()) { + LOG.error("No bucket name given"); + } + throw new IllegalArgumentException("Bucket name is mandatory to create bucket"); + } + + // check for bucket location, which is optional to create a new bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketLocation())) { + if (LOG.isWarnEnabled()) { + LOG.warn("No bucket location given, defaulting to '" + OBSConstants.DEFAULT_LOCATION + "'"); + } + clientConfigurations.setBucketLocation(OBSConstants.DEFAULT_LOCATION); + } + // verify valid bucket location + OBSRegion.checkValidRegion(clientConfigurations.getBucketLocation()); + + request = new CreateBucketRequest(clientConfigurations.getBucketName(), clientConfigurations.getBucketLocation()); + } + + // invoke create bucket method and map response object to exchange body + ObsBucket response = obsClient.createBucket(request); + exchange.getMessage().setBody(gson.toJson(response)); + } + + /** + * Perform delete bucket operation + * + * @param exchange + */ + private void deleteBucket(Exchange exchange) throws ObsException { + // check for bucket name, which is mandatory to delete a bucket + if (ObjectHelper.isEmpty(clientConfigurations.getBucketName())) { + if (LOG.isErrorEnabled()) { + LOG.error("No bucket name given"); + } + throw new IllegalArgumentException("Bucket name is mandatory to delete bucket"); + } + + // invoke delete bucket method and map response object to exchange body + HeaderResponse response = obsClient.deleteBucket(clientConfigurations.getBucketName()); + exchange.getMessage().setBody(gson.toJson(response.getResponseHeaders())); + } + + /** + * Update dynamic client configurations. Some endpoint parameters (operation, and bucket name and location) can also + * be passed via exchange properties, so they can be updated between each transaction. Since they can change, we + * must clear the previous transaction and update these parameters with their new values + * + * @param exchange + */ + private void updateClientConfigs(Exchange exchange) { + resetDynamicConfigs(); + + // checking for required operation (exchange overrides endpoint operation if both are provided) + if (ObjectHelper.isEmpty(exchange.getProperty(OBSProperties.OPERATION)) + && ObjectHelper.isEmpty(endpoint.getOperation())) { + if (LOG.isErrorEnabled()) { Review comment: Remove this ########## 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()) { Review comment: Do not LOG and re-throw. Remove this! ########## File path: components/camel-huawei/camel-huaweicloud-obs/src/test/resources/log4j2.properties ########## @@ -0,0 +1,23 @@ +## --------------------------------------------------------------------------- +## 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.out.type = Console Review comment: For logging we log by default to a test log file. See the other components and their log4j properties file -- 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