You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/09/24 09:48:18 UTC
[camel] branch main updated: CAMEL-16932: API components should use
configuration options that are set via the component. The camel-as2
component did not work with that,
so you could only set all options via message headers or endpoint uri
options. Not globally on the component. Thanks to Dmitrii Kriukov for
reporting.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 413b753 CAMEL-16932: API components should use configuration options that are set via the component. The camel-as2 component did not work with that, so you could only set all options via message headers or endpoint uri options. Not globally on the component. Thanks to Dmitrii Kriukov for reporting.
413b753 is described below
commit 413b753158c45e9a5e9fdb22869b7dc994909782
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Sep 24 11:47:38 2021 +0200
CAMEL-16932: API components should use configuration options that are set via the component. The camel-as2 component did not work with that, so you could only set all options via message headers or endpoint uri options. Not globally on the component. Thanks to Dmitrii Kriukov for reporting.
---
.../camel/component/as2/AS2ClientManagerIT.java | 89 ++++++++++++++++++++++
.../as2/CreateEndpointManualIssueTest.java | 84 ++++++++++++++++++++
.../support/component/AbstractApiEndpoint.java | 21 ++++-
.../support/component/AbstractApiProducer.java | 9 ++-
.../camel/support/component/ApiMethodHelper.java | 1 +
.../component/ApiMethodPropertiesHelper.java | 24 ++++++
6 files changed, 222 insertions(+), 6 deletions(-)
diff --git a/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/AS2ClientManagerIT.java b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/AS2ClientManagerIT.java
index 9a07ba6..57941b7 100644
--- a/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/AS2ClientManagerIT.java
+++ b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/AS2ClientManagerIT.java
@@ -306,6 +306,85 @@ public class AS2ClientManagerIT extends AbstractAS2ITSupport {
}
@Test
+ public void plainMessageSend2Test() throws Exception {
+ final Map<String, Object> headers = new HashMap<>();
+ // parameter type is String
+ headers.put("CamelAS2.requestUri", REQUEST_URI);
+ // parameter type is String
+ headers.put("CamelAS2.subject", SUBJECT);
+ // parameter type is String
+ headers.put("CamelAS2.from", FROM);
+ // parameter type is org.apache.camel.component.as2.api.AS2MessageStructure
+ headers.put("CamelAS2.as2MessageStructure", AS2MessageStructure.PLAIN);
+ // parameter type is org.apache.http.entity.ContentType
+ headers.put("CamelAS2.ediMessageContentType",
+ ContentType.create(AS2MediaType.APPLICATION_EDIFACT, AS2Charset.US_ASCII));
+ // parameter type is String
+ headers.put("CamelAS2.ediMessageTransferEncoding", EDI_MESSAGE_CONTENT_TRANSFER_ENCODING);
+ // parameter type is String
+ headers.put("CamelAS2.dispositionNotificationTo", "mrAS2@example.com");
+ // parameter type is String
+ headers.put("CamelAS2.attachedFileName", "");
+
+ final Triple<HttpEntity, HttpRequest, HttpResponse> result = executeRequest2(headers);
+ HttpEntity responseEntity = result.getLeft();
+ HttpRequest request = result.getMiddle();
+ HttpResponse response = result.getRight();
+
+ assertNotNull(result, "send result");
+ LOG.debug("send: " + result);
+ assertNotNull(request, "Request");
+ assertTrue(request instanceof HttpEntityEnclosingRequest, "Request does not contain body");
+ HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+ assertNotNull(entity, "Request body");
+ assertTrue(entity instanceof ApplicationEDIEntity, "Request body does not contain EDI entity");
+ String ediMessage = ((ApplicationEDIEntity) entity).getEdiMessage();
+ assertEquals(EDI_MESSAGE.replaceAll("[\n\r]", ""), ediMessage.replaceAll("[\n\r]", ""), "EDI message is different");
+
+ assertNotNull(response, "Response");
+ assertTrue(HttpMessageUtils.getHeaderValue(response, AS2Header.CONTENT_TYPE).startsWith(AS2MimeType.MULTIPART_REPORT),
+ "Unexpected response type");
+ assertEquals(AS2Constants.MIME_VERSION, HttpMessageUtils.getHeaderValue(response, AS2Header.MIME_VERSION),
+ "Unexpected mime version");
+ assertEquals(EXPECTED_AS2_VERSION, HttpMessageUtils.getHeaderValue(response, AS2Header.AS2_VERSION),
+ "Unexpected AS2 version");
+ assertEquals(EXPECTED_MDN_SUBJECT, HttpMessageUtils.getHeaderValue(response, AS2Header.SUBJECT),
+ "Unexpected MDN subject");
+ assertEquals(MDN_FROM, HttpMessageUtils.getHeaderValue(response, AS2Header.FROM), "Unexpected MDN from");
+ assertEquals(AS2_NAME, HttpMessageUtils.getHeaderValue(response, AS2Header.AS2_FROM), "Unexpected AS2 from");
+ assertEquals(AS2_NAME, HttpMessageUtils.getHeaderValue(response, AS2Header.AS2_TO), "Unexpected AS2 to");
+ assertNotNull(HttpMessageUtils.getHeaderValue(response, AS2Header.MESSAGE_ID), "Missing message id");
+
+ assertNotNull(responseEntity, "Response entity");
+ assertTrue(responseEntity instanceof DispositionNotificationMultipartReportEntity, "Unexpected response entity type");
+ DispositionNotificationMultipartReportEntity reportEntity
+ = (DispositionNotificationMultipartReportEntity) responseEntity;
+ assertEquals(2, reportEntity.getPartCount(), "Unexpected number of body parts in report");
+ MimeEntity firstPart = reportEntity.getPart(0);
+ assertEquals(ContentType.create(AS2MimeType.TEXT_PLAIN, AS2Charset.US_ASCII).toString(),
+ firstPart.getContentTypeValue(), "Unexpected content type in first body part of report");
+ MimeEntity secondPart = reportEntity.getPart(1);
+ assertEquals(ContentType.create(AS2MimeType.MESSAGE_DISPOSITION_NOTIFICATION, AS2Charset.US_ASCII).toString(),
+ secondPart.getContentTypeValue(),
+ "Unexpected content type in second body part of report");
+
+ assertTrue(secondPart instanceof AS2MessageDispositionNotificationEntity, "");
+ AS2MessageDispositionNotificationEntity messageDispositionNotificationEntity
+ = (AS2MessageDispositionNotificationEntity) secondPart;
+ assertEquals(ORIGIN_SERVER_NAME, messageDispositionNotificationEntity.getReportingUA(),
+ "Unexpected value for reporting UA");
+ assertEquals(AS2_NAME, messageDispositionNotificationEntity.getFinalRecipient(),
+ "Unexpected value for final recipient");
+ assertEquals(HttpMessageUtils.getHeaderValue(request, AS2Header.MESSAGE_ID),
+ messageDispositionNotificationEntity.getOriginalMessageId(), "Unexpected value for original message ID");
+ assertEquals(DispositionMode.AUTOMATIC_ACTION_MDN_SENT_AUTOMATICALLY,
+ messageDispositionNotificationEntity.getDispositionMode(), "Unexpected value for disposition mode");
+ assertEquals(AS2DispositionType.PROCESSED, messageDispositionNotificationEntity.getDispositionType(),
+ "Unexpected value for disposition type");
+
+ }
+
+ @Test
public void envelopedMessageSendTest() throws Exception {
final Map<String, Object> headers = new HashMap<>();
// parameter type is String
@@ -705,6 +784,12 @@ public class AS2ClientManagerIT extends AbstractAS2ITSupport {
return new ImmutableTriple(responseEntity, requestHandler.getRequest(), requestHandler.getResponse());
}
+ private Triple<HttpEntity, HttpRequest, HttpResponse> executeRequest2(Map<String, Object> headers) throws Exception {
+ HttpEntity responseEntity = requestBodyAndHeaders("direct://SEND2", EDI_MESSAGE, headers);
+
+ return new ImmutableTriple(responseEntity, requestHandler.getRequest(), requestHandler.getResponse());
+ }
+
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@@ -719,6 +804,10 @@ public class AS2ClientManagerIT extends AbstractAS2ITSupport {
// test route for send
from("direct://SEND").to("as2://" + PATH_PREFIX + "/send?inBody=ediMessage");
+ // test route for send2
+ from("direct://SEND2").toF("as2://" + PATH_PREFIX + "/send?inBody=ediMessage&as2From=%s&as2To=%s", AS2_NAME,
+ AS2_NAME);
+
from("jetty:http://localhost:" + MDN_TARGET_PORT + "/handle-receipts").process(proc);
}
diff --git a/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/CreateEndpointManualIssueTest.java b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/CreateEndpointManualIssueTest.java
new file mode 100644
index 0000000..c3a03a7
--- /dev/null
+++ b/components/camel-as2/camel-as2-component/src/test/java/org/apache/camel/component/as2/CreateEndpointManualIssueTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.as2;
+
+import java.nio.charset.Charset;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.as2.api.AS2MessageStructure;
+import org.apache.camel.component.as2.api.AS2SignatureAlgorithm;
+import org.apache.camel.component.as2.internal.AS2ApiName;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * A bad way to create the endpoint reported by Camel user.
+ */
+public class CreateEndpointManualIssueTest {
+
+ @Test
+ public void testCreateEndpoint() throws Exception {
+ CamelContext camelContext = new DefaultCamelContext();
+ camelContext.start();
+
+ org.apache.http.entity.ContentType contentTypeEdifact
+ = org.apache.http.entity.ContentType.create("application/edifact", (Charset) null);
+
+ String methodName = "send";
+ AS2ApiName as2ApiNameClient = AS2ApiName.CLIENT;
+
+ AS2Configuration endpointConfiguration = new AS2Configuration();
+ endpointConfiguration.setApiName(as2ApiNameClient);
+ endpointConfiguration.setMethodName(methodName);
+ endpointConfiguration.setRequestUri("/as2/HttpReceiver");
+ endpointConfiguration.setAs2MessageStructure(AS2MessageStructure.PLAIN);
+
+ endpointConfiguration.setAs2Version("1.0");
+ endpointConfiguration.setAs2To("mendelsontestAS2");
+ endpointConfiguration.setAs2From("mycompanyAS2");
+ endpointConfiguration.setEdiMessageType(contentTypeEdifact);
+ endpointConfiguration.setFrom("dk2kEdi");
+ endpointConfiguration.setSubject("mysubject");
+ endpointConfiguration.setSubject("mysubject");
+ endpointConfiguration.setSigningAlgorithm(AS2SignatureAlgorithm.MD2WITHRSA);
+ endpointConfiguration.setEdiMessageTransferEncoding("7bit");
+
+ AS2Component as2Component = new AS2Component();
+ as2Component.setCamelContext(camelContext);
+ as2Component.setConfiguration(endpointConfiguration);
+ as2Component.start();
+
+ AS2Endpoint endpoint = (AS2Endpoint) as2Component
+ .createEndpoint("as2://client/send?targetHostName=testas2.mendelson-e-c.com"
+ + "&targetPortNumber=8080&inBody=ediMessage&requestUri=/as2/HttpReceiver");
+
+ Assertions.assertEquals("mycompanyAS2", endpoint.getAs2From());
+ Assertions.assertEquals("mendelsontestAS2", endpoint.getAs2To());
+ Assertions.assertEquals("dk2kEdi", endpoint.getFrom());
+
+ // should fail but that there are some missing options, but not the ones we set on configuration
+ Exchange out
+ = camelContext.createProducerTemplate().request(endpoint, exchange -> exchange.getIn().setBody("Hello World"));
+ Throwable cause = out.getException();
+ Assertions.assertNotNull(cause);
+
+ Assertions.assertTrue(cause.getMessage().contains("Missing properties for send, need one or more from (9 args)"));
+ Assertions.assertFalse(cause.getMessage().contains("ediMessageType"));
+ }
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
index c2d1499..9b1ade9 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
@@ -79,6 +79,8 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T>
// cached property names and values
private Set<String> endpointPropertyNames;
private Map<String, Object> endpointProperties;
+ private Set<String> configurationPropertyNames;
+ private Map<String, Object> configurationProperties;
public AbstractApiEndpoint(String endpointUri, Component component,
E apiName, String methodName, ApiMethodHelper<? extends ApiMethod> methodHelper,
@@ -146,13 +148,18 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T>
*/
private void initState() {
- // compute endpoint property names and values
- final HashMap<String, Object> properties = new HashMap<>();
+ // compute configuration & endpoint property names and values
+ HashMap<String, Object> properties = new HashMap<>();
+ getPropertiesHelper().getConfigurationProperties(getCamelContext(), configuration, properties);
+ this.configurationProperties = Collections.unmodifiableMap(properties);
+ this.configurationPropertyNames = Collections.unmodifiableSet(properties.keySet());
+
+ properties = new HashMap<>();
getPropertiesHelper().getEndpointProperties(getCamelContext(), configuration, properties);
this.endpointProperties = Collections.unmodifiableMap(properties);
this.endpointPropertyNames = Collections.unmodifiableSet(properties.keySet());
- // get endpoint property names
+ // use only endpoint property names when looking for candidate methods
final Set<String> arguments = new HashSet<>(endpointPropertyNames);
// add inBody argument for producers
if (inBody != null) {
@@ -279,6 +286,14 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T>
return endpointProperties;
}
+ public Set<String> getConfigurationPropertyNames() {
+ return configurationPropertyNames;
+ }
+
+ public final Map<String, Object> getConfigurationProperties() {
+ return configurationProperties;
+ }
+
/**
* Returns an instance of an API Proxy based on apiName, method and args. Called by {@link AbstractApiConsumer} or
* {@link AbstractApiProducer}.
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
index ee4470d..628ab05 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiProducer.java
@@ -58,8 +58,9 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T>
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
- // properties for method arguments
+ // properties for method arguments should let exchange override
final Map<String, Object> properties = new HashMap<>();
+ properties.putAll(endpoint.getConfigurationProperties());
properties.putAll(endpoint.getEndpointProperties());
propertiesHelper.getExchangeProperties(exchange, properties);
@@ -147,10 +148,12 @@ public abstract class AbstractApiProducer<E extends Enum<E> & ApiName, T>
// get the method to call
if (filteredMethods.isEmpty()) {
+ Set<String> missing = methodHelper.getMissingProperties(endpoint.getMethodName(), argNames);
throw new RuntimeCamelException(
- String.format("Missing properties for %s, need one or more from %s",
+ String.format("Missing properties for %s, need one or more from (%s args) %s",
endpoint.getMethodName(),
- methodHelper.getMissingProperties(endpoint.getMethodName(), argNames)));
+ missing.size(),
+ missing));
} else if (filteredMethods.size() == 1) {
// found an exact match
method = filteredMethods.get(0);
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
index 6a02898..a019b2c 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodHelper.java
@@ -389,6 +389,7 @@ public final class ApiMethodHelper<T extends Enum<T> & ApiMethod> {
* @param argNames available arguments
* @return Set of missing argument names
*/
+
public Set<String> getMissingProperties(String methodName, Set<String> argNames) {
final List<Object> argsWithTypes = getArguments(methodName);
final Set<String> missingArgs = new HashSet<>();
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java
index 03e4792..9928c34 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java
@@ -162,4 +162,28 @@ public abstract class ApiMethodPropertiesHelper<C> {
return Collections.unmodifiableSet(fields);
}
+ public void getConfigurationProperties(CamelContext context, Object endpointConfiguration, Map<String, Object> properties) {
+ PropertyConfigurer configurer = context.adapt(ExtendedCamelContext.class).getConfigurerResolver()
+ .resolvePropertyConfigurer(endpointConfiguration.getClass().getName(), context);
+ // use reflection free configurer (if possible)
+ if (configurer instanceof ExtendedPropertyConfigurerGetter) {
+ ExtendedPropertyConfigurerGetter getter = (ExtendedPropertyConfigurerGetter) configurer;
+ Set<String> all = getter.getAllOptions(endpointConfiguration).keySet();
+ for (String name : all) {
+ Object value = getter.getOptionValue(endpointConfiguration, name, true);
+ if (value != null) {
+ // lower case the first letter which is what the properties map expects
+ String key = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+ properties.put(key, value);
+ }
+ }
+ } else {
+ context.adapt(ExtendedCamelContext.class).getBeanIntrospection().getProperties(endpointConfiguration, properties,
+ null, false);
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Found configuration properties {}", properties.keySet());
+ }
+ }
+
}