hokutor commented on a change in pull request #6107: URL: https://github.com/apache/camel/pull/6107#discussion_r712176428
########## 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(); + + boolean isImageContentSet = true; + boolean isImageUrlSet = true; + + String imageContent = exchange.getProperty(ImageRecognitionProperties.IMAGE_CONTENT, String.class); + if (!StringUtils.isEmpty(imageContent)) { + clientConfigurations.setImageContent(imageContent); + } else if (!StringUtils.isEmpty(this.endpoint.getImageContent())) { + clientConfigurations.setImageContent(this.endpoint.getImageContent()); + } else { + isImageContentSet = false; + } + + String imageUrl = exchange.getProperty(ImageRecognitionProperties.IMAGE_URL, String.class); + if (!StringUtils.isEmpty(imageUrl)) { + clientConfigurations.setImageUrl(imageUrl); + } else if (!StringUtils.isEmpty(this.endpoint.getImageUrl())) { + clientConfigurations.setImageUrl(this.endpoint.getImageUrl()); + } else { + isImageUrlSet = false; + } + if (!isImageContentSet && !isImageUrlSet) { + LOG.error("Image content and url are not set"); + throw new IllegalArgumentException("either image content or image url should be set"); + } + + String tagLanguageProperty = exchange.getProperty(ImageRecognitionProperties.TAG_LANGUAGE, String.class); + clientConfigurations.setTagLanguage( + StringUtils.isEmpty(tagLanguageProperty) ? this.endpoint.getTagLanguage() : tagLanguageProperty); + if (!ImageRecognitionConstants.TAG_LANGUAGE_ZH.equals(clientConfigurations.getTagLanguage()) + && !ImageRecognitionConstants.TAG_LANGUAGE_EN.equals(clientConfigurations.getTagLanguage())) { + LOG.error("Tag language is invalid: {}", clientConfigurations.getTagLanguage()); + throw new IllegalArgumentException("tag language can only be 'zh' or 'en'"); + } + + Integer tagLimitProperty = exchange.getProperty(ImageRecognitionProperties.TAG_LIMIT, Integer.class); + clientConfigurations.setTagLimit(tagLimitProperty == null ? endpoint.getTagLimit() : tagLimitProperty); + + Float thresholdProperty = exchange.getProperty(ImageRecognitionProperties.THRESHOLD, Float.class); + clientConfigurations.setThreshold(thresholdProperty == null ? endpoint.getThreshold() : thresholdProperty); + + if (clientConfigurations.getThreshold() == -1) { + clientConfigurations + .setThreshold(ImageRecognitionConstants.OPERATION_TAG_RECOGNITION.equals(endpoint.getOperation()) + ? ImageRecognitionConstants.DEFAULT_TAG_RECOGNITION_THRESHOLD + : ImageRecognitionConstants.DEFAULT_CELEBRITY_RECOGNITION_THRESHOLD); + } + validateThresholdValue(clientConfigurations.getThreshold(), endpoint.getOperation()); + } + + /** + * validate threshold value. for tagRecognition, threshold should be at 0~100. for celebrityRecognition, threshold + * should be at 0~1. + * + * @param threshold threshold value + * @param operation operation + */ + private void validateThresholdValue(float threshold, String operation) { + if (ImageRecognitionConstants.OPERATION_TAG_RECOGNITION.equals(operation)) { + if (threshold < 0 || threshold > ImageRecognitionConstants.TAG_RECOGNITION_THRESHOLD_MAX) { + LOG.error("Tag threshold is invalid: {}", threshold); + throw new IllegalArgumentException("tag recognition threshold should be at 0~100"); + } + } else { + if (threshold < 0 || threshold > ImageRecognitionConstants.CELEBRITY_RECOGNITION_THRESHOLD_MAX) { + LOG.error("Celebrity recognition threshold is invalid: {}", threshold); Review comment: done ########## File path: components/camel-huawei/camel-huaweicloud-imagerecognition/src/test/resources/log4j2.properties ########## @@ -0,0 +1,28 @@ +## --------------------------------------------------------------------------- +## 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-huaweicloud-iam-test.log Review comment: done -- 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