This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7bcd037048aff892692a37ee6698b08d04b14a8f Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Thu Apr 25 12:46:37 2024 +0200 (chores) camel-core: cleanup duplicated code --- .../camel/catalog/impl/AbstractCamelCatalog.java | 8 ++- .../org/apache/camel/builder/BuilderSupport.java | 17 ++--- .../org/apache/camel/processor/PollEnricher.java | 37 +---------- .../apache/camel/processor/ProcessorHelper.java | 74 ++++++++++++++++++++++ .../camel/processor/RecipientListProcessor.java | 34 +--------- .../org/apache/camel/processor/RoutingSlip.java | 36 +---------- .../camel/processor/SendDynamicProcessor.java | 15 +---- .../apache/camel/reifier/ConvertHeaderReifier.java | 9 +-- .../camel/reifier/ConvertVariableReifier.java | 9 +-- .../camel/support/builder/ExpressionBuilder.java | 6 +- 10 files changed, 94 insertions(+), 151 deletions(-) diff --git a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java index 124c11d49c2..cf8e961913f 100644 --- a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java +++ b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/AbstractCamelCatalog.java @@ -1269,7 +1269,7 @@ public abstract class AbstractCamelCatalog { if (!optionPlaceholder && !lookup && javaType != null && (javaType.startsWith("java.util.Map") || javaType.startsWith("java.util.Properties"))) { // there must be a valid suffix - if (suffix == null || suffix.isEmpty() || suffix.equals(".")) { + if (isValidSuffix(suffix)) { result.addInvalidMap(longKey, value); } else if (suffix.startsWith("[") && !suffix.contains("]")) { result.addInvalidMap(longKey, value); @@ -1277,7 +1277,7 @@ public abstract class AbstractCamelCatalog { } if (!optionPlaceholder && !lookup && javaType != null && "array".equals(row.getType())) { // there must be a suffix and it must be using [] style - if (suffix == null || suffix.isEmpty() || suffix.equals(".")) { + if (isValidSuffix(suffix)) { result.addInvalidArray(longKey, value); } else if (!suffix.startsWith("[") && !suffix.contains("]")) { result.addInvalidArray(longKey, value); @@ -1293,6 +1293,10 @@ public abstract class AbstractCamelCatalog { } } + private static boolean isValidSuffix(String suffix) { + return suffix == null || suffix.isEmpty() || suffix.equals("."); + } + private static boolean acceptConfigurationPropertyKey(String key) { if (key == null) { return false; diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java index 4567c61cc62..f71664b26db 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/BuilderSupport.java @@ -24,10 +24,6 @@ import org.apache.camel.Expression; import org.apache.camel.NoSuchEndpointException; import org.apache.camel.RuntimeCamelException; import org.apache.camel.model.language.DatasonnetExpression; -import org.apache.camel.model.language.ExchangePropertyExpression; -import org.apache.camel.model.language.HeaderExpression; -import org.apache.camel.model.language.SimpleExpression; -import org.apache.camel.model.language.VariableExpression; import org.apache.camel.model.language.XPathExpression; import org.apache.camel.spi.TransactedPolicy; import org.apache.camel.support.builder.Namespaces; @@ -62,16 +58,14 @@ public abstract class BuilderSupport implements CamelContextAware { * Returns a value builder for the given header */ public ValueBuilder header(String name) { - Expression exp = new HeaderExpression(name); - return new ValueBuilder(exp); + return Builder.header(name); } /** * Returns a value builder for the given exchange property */ public ValueBuilder exchangeProperty(String name) { - Expression exp = new ExchangePropertyExpression(name); - return new ValueBuilder(exp); + return Builder.exchangeProperty(name); } /** @@ -92,8 +86,7 @@ public abstract class BuilderSupport implements CamelContextAware { * Returns a value builder for the given variable */ public ValueBuilder variable(String name) { - Expression exp = new VariableExpression(name); - return new ValueBuilder(exp); + return Builder.variable(name); } /** @@ -246,9 +239,7 @@ public abstract class BuilderSupport implements CamelContextAware { * Returns a simple expression value builder */ public ValueBuilder simple(String value, Class<?> resultType) { - SimpleExpression exp = new SimpleExpression(value); - exp.setResultType(resultType); - return new ValueBuilder(exp); + return Builder.simple(value, resultType); } /** diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java index 4b7dd9af37c..4e4dd8c895f 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/PollEnricher.java @@ -28,7 +28,6 @@ import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.ExchangePropertyKey; import org.apache.camel.Expression; -import org.apache.camel.ExtendedCamelContext; import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.PollingConsumer; import org.apache.camel.spi.ConsumerCache; @@ -36,7 +35,6 @@ import org.apache.camel.spi.EndpointUtilizationStatistics; import org.apache.camel.spi.ExceptionHandler; import org.apache.camel.spi.HeadersMapFactory; import org.apache.camel.spi.IdAware; -import org.apache.camel.spi.NormalizedEndpointUri; import org.apache.camel.spi.RouteIdAware; import org.apache.camel.support.AsyncProcessorSupport; import org.apache.camel.support.BridgeExceptionHandlerToErrorHandler; @@ -404,42 +402,11 @@ public class PollEnricher extends AsyncProcessorSupport implements IdAware, Rout } protected static Object prepareRecipient(Exchange exchange, Object recipient) throws NoTypeConversionAvailableException { - if (recipient instanceof Endpoint || recipient instanceof NormalizedEndpointUri) { - return recipient; - } else if (recipient instanceof String) { - // trim strings as end users might have added spaces between separators - recipient = ((String) recipient).trim(); - } - if (recipient != null) { - CamelContext ecc = exchange.getContext(); - String uri; - if (recipient instanceof String) { - uri = (String) recipient; - } else { - // convert to a string type we can work with - uri = ecc.getTypeConverter().mandatoryConvertTo(String.class, exchange, recipient); - } - // optimize and normalize endpoint - return ecc.getCamelContextExtension().normalizeUri(uri); - } - return null; + return ProcessorHelper.prepareRecipient(exchange, recipient); } protected static Endpoint getExistingEndpoint(CamelContext context, Object recipient) { - if (recipient instanceof Endpoint) { - return (Endpoint) recipient; - } - if (recipient != null) { - if (recipient instanceof NormalizedEndpointUri) { - NormalizedEndpointUri nu = (NormalizedEndpointUri) recipient; - ExtendedCamelContext ecc = context.getCamelContextExtension(); - return ecc.hasEndpoint(nu); - } else { - String uri = recipient.toString(); - return context.hasEndpoint(uri); - } - } - return null; + return ProcessorHelper.getExistingEndpoint(context, recipient); } protected static Endpoint resolveEndpoint(CamelContext camelContext, Object recipient, boolean prototype) { diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java new file mode 100644 index 00000000000..64c270ca3dc --- /dev/null +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ProcessorHelper.java @@ -0,0 +1,74 @@ +/* + * 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.processor; + +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.spi.NormalizedEndpointUri; + +final class ProcessorHelper { + + private ProcessorHelper() { + } + + static Object prepareRecipient(Exchange exchange, Object recipient) throws NoTypeConversionAvailableException { + if (recipient instanceof Endpoint || recipient instanceof NormalizedEndpointUri) { + return recipient; + } else if (recipient instanceof String) { + // trim strings as end users might have added spaces between separators + recipient = ((String) recipient).trim(); + } + if (recipient != null) { + CamelContext ecc = exchange.getContext(); + String uri; + if (recipient instanceof String) { + uri = (String) recipient; + } else { + // convert to a string type we can work with + uri = ecc.getTypeConverter().mandatoryConvertTo(String.class, exchange, recipient); + } + // optimize and normalize endpoint + return ecc.getCamelContextExtension().normalizeUri(uri); + } + return null; + } + + static Endpoint getExistingEndpoint(Exchange exchange, Object recipient) { + return getExistingEndpoint(exchange.getContext(), recipient); + } + + static Endpoint getExistingEndpoint(CamelContext context, Object recipient) { + if (recipient instanceof Endpoint) { + return (Endpoint) recipient; + } + if (recipient != null) { + if (recipient instanceof NormalizedEndpointUri) { + NormalizedEndpointUri nu = (NormalizedEndpointUri) recipient; + ExtendedCamelContext ecc = context.getCamelContextExtension(); + return ecc.hasEndpoint(nu); + } else { + String uri = recipient.toString(); + return context.hasEndpoint(uri); + } + } + return null; + } +} diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java index 3cacec99784..c12ea0b4873 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RecipientListProcessor.java @@ -327,41 +327,11 @@ public class RecipientListProcessor extends MulticastProcessor { } protected static Object prepareRecipient(Exchange exchange, Object recipient) throws NoTypeConversionAvailableException { - if (recipient instanceof Endpoint || recipient instanceof NormalizedEndpointUri) { - return recipient; - } else if (recipient instanceof String) { - // trim strings as end users might have added spaces between separators - recipient = ((String) recipient).trim(); - } - if (recipient != null) { - CamelContext ecc = exchange.getContext(); - String uri; - if (recipient instanceof String) { - uri = (String) recipient; - } else { - // convert to a string type we can work with - uri = ecc.getTypeConverter().mandatoryConvertTo(String.class, exchange, recipient); - } - // optimize and normalize endpoint - return ecc.getCamelContextExtension().normalizeUri(uri); - } - return null; + return ProcessorHelper.prepareRecipient(exchange, recipient); } protected static Endpoint getExistingEndpoint(Exchange exchange, Object recipient) { - if (recipient instanceof Endpoint) { - return (Endpoint) recipient; - } - if (recipient != null) { - if (recipient instanceof NormalizedEndpointUri nu) { - CamelContext ecc = exchange.getContext(); - return ecc.getCamelContextExtension().hasEndpoint(nu); - } else { - String uri = recipient.toString().trim(); - return exchange.getContext().hasEndpoint(uri); - } - } - return null; + return ProcessorHelper.getExistingEndpoint(exchange, recipient); } protected static Endpoint resolveEndpoint(Exchange exchange, Object recipient, boolean prototype) { diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java index b9544f5a462..5e6c97a75c9 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RoutingSlip.java @@ -31,7 +31,6 @@ import org.apache.camel.Route; import org.apache.camel.Traceable; import org.apache.camel.spi.EndpointUtilizationStatistics; import org.apache.camel.spi.IdAware; -import org.apache.camel.spi.NormalizedEndpointUri; import org.apache.camel.spi.ProducerCache; import org.apache.camel.spi.RouteIdAware; import org.apache.camel.support.AsyncProcessorSupport; @@ -317,42 +316,11 @@ public class RoutingSlip extends AsyncProcessorSupport implements Traceable, IdA } protected static Object prepareRecipient(Exchange exchange, Object recipient) throws NoTypeConversionAvailableException { - if (recipient instanceof Endpoint || recipient instanceof NormalizedEndpointUri) { - return recipient; - } else if (recipient instanceof String) { - // trim strings as end users might have added spaces between separators - recipient = ((String) recipient).trim(); - } - if (recipient != null) { - CamelContext ecc = exchange.getContext(); - String uri; - if (recipient instanceof String) { - uri = (String) recipient; - } else { - // convert to a string type we can work with - uri = ecc.getTypeConverter().mandatoryConvertTo(String.class, exchange, recipient); - } - // optimize and normalize endpoint - return ecc.getCamelContextExtension().normalizeUri(uri); - } - return null; + return ProcessorHelper.prepareRecipient(exchange, recipient); } protected static Endpoint getExistingEndpoint(Exchange exchange, Object recipient) { - if (recipient instanceof Endpoint) { - return (Endpoint) recipient; - } - if (recipient != null) { - if (recipient instanceof NormalizedEndpointUri) { - NormalizedEndpointUri nu = (NormalizedEndpointUri) recipient; - CamelContext ecc = exchange.getContext(); - return ecc.getCamelContextExtension().hasEndpoint(nu); - } else { - String uri = recipient.toString(); - return exchange.getContext().hasEndpoint(uri); - } - } - return null; + return ProcessorHelper.getExistingEndpoint(exchange, recipient); } protected Endpoint resolveEndpoint(Exchange exchange, Object recipient, boolean prototype) throws Exception { diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java index decf336be57..54fca416cec 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/SendDynamicProcessor.java @@ -306,20 +306,7 @@ public class SendDynamicProcessor extends AsyncProcessorSupport implements IdAwa } protected static Endpoint getExistingEndpoint(Exchange exchange, Object recipient) { - if (recipient instanceof Endpoint) { - return (Endpoint) recipient; - } - if (recipient != null) { - if (recipient instanceof NormalizedEndpointUri) { - NormalizedEndpointUri nu = (NormalizedEndpointUri) recipient; - CamelContext ecc = exchange.getContext(); - return ecc.getCamelContextExtension().hasEndpoint(nu); - } else { - String uri = recipient.toString(); - return exchange.getContext().hasEndpoint(uri); - } - } - return null; + return ProcessorHelper.getExistingEndpoint(exchange, recipient); } protected static Endpoint resolveEndpoint(Exchange exchange, Object recipient, boolean prototype) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertHeaderReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertHeaderReifier.java index f184409b4fa..9a6be56eaca 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertHeaderReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertHeaderReifier.java @@ -16,7 +16,6 @@ */ package org.apache.camel.reifier; -import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import org.apache.camel.Expression; @@ -65,13 +64,7 @@ public class ConvertHeaderReifier extends ProcessorReifier<ConvertHeaderDefiniti } public static String validateCharset(String charset) throws UnsupportedCharsetException { - if (charset != null) { - if (Charset.isSupported(charset)) { - return Charset.forName(charset).name(); - } - throw new UnsupportedCharsetException(charset); - } - return null; + return ConvertBodyReifier.validateCharset(charset); } } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertVariableReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertVariableReifier.java index 395cf43d695..19b5258eb90 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertVariableReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ConvertVariableReifier.java @@ -16,7 +16,6 @@ */ package org.apache.camel.reifier; -import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import org.apache.camel.Expression; @@ -65,13 +64,7 @@ public class ConvertVariableReifier extends ProcessorReifier<ConvertVariableDefi } public static String validateCharset(String charset) throws UnsupportedCharsetException { - if (charset != null) { - if (Charset.isSupported(charset)) { - return Charset.forName(charset).name(); - } - throw new UnsupportedCharsetException(charset); - } - return null; + return ConvertBodyReifier.validateCharset(charset); } } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java index 838ba1bd66f..fa2e6d0ad00 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java @@ -411,11 +411,7 @@ public class ExpressionBuilder { return new ExpressionAdapter() { @Override public Object evaluate(Exchange exchange) { - Exception exception = exchange.getException(); - if (exception == null) { - exception = exchange.getProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, Exception.class); - } - return exception; + return LanguageHelper.exception(exchange); } @Override