hokutor commented on a change in pull request #6107: URL: https://github.com/apache/camel/pull/6107#discussion_r713080006
########## File path: components/camel-huawei/camel-huaweicloud-imagerecognition/src/main/java/org/apache/camel/component/huaweicloud/image/ImageRecognitionProducer.java ########## @@ -0,0 +1,327 @@ +/* + * 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.image; + +import com.huaweicloud.sdk.core.auth.BasicCredentials; +import com.huaweicloud.sdk.core.http.HttpConfig; +import com.huaweicloud.sdk.core.utils.StringUtils; +import com.huaweicloud.sdk.image.v2.ImageClient; +import com.huaweicloud.sdk.image.v2.model.CelebrityRecognitionReq; +import com.huaweicloud.sdk.image.v2.model.ImageTaggingReq; +import com.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionRequest; +import com.huaweicloud.sdk.image.v2.model.RunCelebrityRecognitionResponse; +import com.huaweicloud.sdk.image.v2.model.RunImageTaggingRequest; +import com.huaweicloud.sdk.image.v2.model.RunImageTaggingResponse; +import com.huaweicloud.sdk.image.v2.region.ImageRegion; +import org.apache.camel.Exchange; +import org.apache.camel.component.huaweicloud.image.constants.ImageRecognitionConstants; +import org.apache.camel.component.huaweicloud.image.constants.ImageRecognitionProperties; +import org.apache.camel.component.huaweicloud.image.models.ClientConfigurations; +import org.apache.camel.support.DefaultProducer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ImageRecognitionProducer extends DefaultProducer { + private static final Logger LOG = LoggerFactory.getLogger(ImageRecognitionProducer.class); + + private ImageClient imageClient; + + private ClientConfigurations clientConfigurations; + + private ImageRecognitionEndpoint endpoint; + + public ImageRecognitionProducer(ImageRecognitionEndpoint endpoint) { + super(endpoint); + this.endpoint = endpoint; + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + this.clientConfigurations = initializeConfigurations(this.endpoint); + this.imageClient = initializeClient(this.endpoint); + } + + /** + * initialize ClientConfigurations + * + * @param endpoint ImageRecognitionEndpoint + * @return ClientConfigurations + */ + private ClientConfigurations initializeConfigurations(ImageRecognitionEndpoint endpoint) { + ClientConfigurations clientConfigurations = new ClientConfigurations(); + + clientConfigurations.setAccessKey(getAccessKey(endpoint)); + clientConfigurations.setSecretKey(getSecretKey(endpoint)); + clientConfigurations.setProjectId(getProjectId(endpoint)); + clientConfigurations.setEndpoint(getEndpoint(endpoint)); + + if (StringUtils.isEmpty(endpoint.getImageContent()) && StringUtils.isEmpty(endpoint.getImageUrl())) { + if (StringUtils.isEmpty(endpoint.getRegion())) { + LOG.error("image and url not found"); + throw new IllegalArgumentException("either image or url should be set"); + } + } + clientConfigurations.setIgnoreSslVerification(endpoint.isIgnoreSslVerification()); + if (clientConfigurations.isIgnoreSslVerification()) { + LOG.warn("SSL verification is ignored. This is unsafe in production environment"); + } + if (!StringUtils.isEmpty(endpoint.getProxyHost())) { + clientConfigurations.setProxyHost(endpoint.getProxyHost()); + clientConfigurations.setProxyPort(endpoint.getProxyPort()); + clientConfigurations.setProxyUser(endpoint.getProxyUser()); + clientConfigurations.setProxyPassword(endpoint.getProxyPassword()); + } + return clientConfigurations; + } + + /** + * initialize image client. this is lazily initialized on the first message + * + * @param endpoint ImageRecognitionEndpoint + * @return ImageClient + */ + private ImageClient initializeClient(ImageRecognitionEndpoint endpoint) { + if (endpoint.getImageClient() != null) { + LOG.warn( + "Instance of ImageClient was set on the endpoint. Skipping creation of ImageClient from endpoint parameters"); + return endpoint.getImageClient(); + } + HttpConfig httpConfig = null; + if (clientConfigurations.getProxyHost() != null) { + httpConfig = HttpConfig.getDefaultHttpConfig() + .withProxyHost(clientConfigurations.getProxyHost()) + .withProxyPort(clientConfigurations.getProxyPort()) + .withIgnoreSSLVerification(clientConfigurations.isIgnoreSslVerification()); + + if (clientConfigurations.getProxyUser() != null) { + httpConfig.setProxyUsername(clientConfigurations.getProxyUser()); + httpConfig.setProxyPassword(clientConfigurations.getProxyPassword()); + } + } + + BasicCredentials credentials = new BasicCredentials().withAk(clientConfigurations.getAccessKey()) + .withSk(clientConfigurations.getSecretKey()) + .withProjectId(clientConfigurations.getProjectId()); + + ImageClient client = ImageClient.newBuilder() + .withCredential(credentials) + .withHttpConfig(httpConfig) + .withEndpoint(clientConfigurations.getEndpoint()) + .build(); + + if (LOG.isDebugEnabled()) { + LOG.debug("Successfully initialized Image client"); + } + return client; + } + + public void process(Exchange exchange) { + String operation = ((ImageRecognitionEndpoint) super.getEndpoint()).getOperation(); + if (StringUtils.isEmpty(operation)) { + LOG.error("Operation is empty"); + throw new IllegalStateException("operation name cannot be empty"); + } + switch (operation) { + case ImageRecognitionConstants.OPERATION_CELEBRITY_RECOGNITION: + if (LOG.isDebugEnabled()) { + LOG.debug("Performing celebrity recognition"); + } + performCelebrityRecognitionOperation(exchange); + break; + case ImageRecognitionConstants.OPERATION_TAG_RECOGNITION: + if (LOG.isDebugEnabled()) { + LOG.debug("Performing tag recognition"); + } + performTagRecognitionOperation(exchange); + break; + default: + LOG.error("Unsupported operation: {}", operation); + throw new UnsupportedOperationException( + "operation can only be either tagRecognition or celebrityRecognition"); + } + } + + /** + * perform celebrity recognition + * + * @param exchange camel exchange + */ + private void performCelebrityRecognitionOperation(Exchange exchange) { + updateClientConfigurations(exchange); + + CelebrityRecognitionReq reqBody = new CelebrityRecognitionReq().withImage(this.clientConfigurations.getImageContent()) + .withUrl(this.clientConfigurations.getImageUrl()) + .withThreshold(this.clientConfigurations.getThreshold()); + + RunCelebrityRecognitionResponse response + = this.imageClient.runCelebrityRecognition(new RunCelebrityRecognitionRequest().withBody(reqBody)); + + exchange.getMessage().setBody(response.getResult()); + } + + /** + * perform tag recognition + * + * @param exchange camel exchange + */ + private void performTagRecognitionOperation(Exchange exchange) { + updateClientConfigurations(exchange); + + ImageTaggingReq reqBody = new ImageTaggingReq().withImage(this.clientConfigurations.getImageContent()) + .withUrl(this.clientConfigurations.getImageUrl()) + .withThreshold(this.clientConfigurations.getThreshold()) + .withLanguage(this.clientConfigurations.getTagLanguage()) + .withLimit(this.clientConfigurations.getTagLimit()); + + RunImageTaggingResponse response = this.imageClient.runImageTagging(new RunImageTaggingRequest().withBody(reqBody)); + + exchange.getMessage().setBody(response.getResult()); + } + + /** + * Update dynamic client configurations. Some endpoint parameters (imageContent, imageUrl, tagLanguage, tagLimit and + * threshold) 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 camel exchange + */ + private void updateClientConfigurations(Exchange exchange) { + resetDynamicConfigurations(); Review comment: > This is not thread safe! you can have concurrent exchanges with different configurations that then both alter this shared configuration @davsclaus - thanks for pointing this out. The issue persists in other huawei cloud components as well. Raised a JIRA for fix pack in those components too. Raised a JIRA https://issues.apache.org/jira/browse/CAMEL-16991 . I will work on that shortly -- 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