This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-13678 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3829daea8d627a93c54c4031624c1713e23708dc Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Jul 7 20:08:07 2019 +0200 CAMEL-13678: Move attachments out of camel-core into camel-attachments. --- components/camel-attachments/pom.xml | 86 +++++++ .../org/apache/camel/attachment}/Attachment.java | 7 +- .../camel/attachment}/AttachmentConverter.java | 13 +- .../apache/camel/attachment}/AttachmentMap.java | 4 +- .../apache/camel/attachment/AttachmentMessage.java | 111 ++++++++ .../camel/attachment}/DefaultAttachment.java | 3 +- .../camel/attachment/DefaultAttachmentMessage.java | 279 +++++++++++++++++++++ .../camel-attachments/src/test/data/message1.xml | 24 ++ .../camel-attachments/src/test/data/message2.xml | 24 ++ .../attachment/BeanMethodWithExchangeTest.java | 67 +++++ .../BeanWithAttachmentAnnotationTest.java | 47 ++-- .../attachment}/BodyAndHeaderConvertTest.java | 24 +- .../MessageWithAttachmentRedeliveryIssueTest.java | 20 +- .../src/test/resources/log4j2.properties | 28 +++ .../src/main/java/org/apache/camel/Message.java | 97 +------ .../processor/ClaimCheckAggregationStrategy.java | 33 --- .../java/org/apache/camel/processor/Splitter.java | 6 +- .../org/apache/camel/builder/ExpressionClause.java | 7 - .../camel/builder/ExpressionClauseSupport.java | 7 - .../apache/camel/builder/ExpressionClauseTest.java | 92 ------- .../component/bean/BeanMethodWithExchangeTest.java | 77 ------ .../org/apache/camel/processor/PipelineTest.java | 41 --- .../apache/camel/support/DefaultMessageTest.java | 40 --- .../org/apache/camel/support/DefaultExchange.java | 4 - .../org/apache/camel/support/DefaultMessage.java | 108 -------- .../org/apache/camel/support/MessageSupport.java | 22 -- .../camel/support/builder/ExpressionBuilder.java | 72 ------ .../maven/packaging/SpringBootStarterMojo.java | 1 + 28 files changed, 683 insertions(+), 661 deletions(-) diff --git a/components/camel-attachments/pom.xml b/components/camel-attachments/pom.xml new file mode 100644 index 0000000..4f8151e --- /dev/null +++ b/components/camel-attachments/pom.xml @@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>3.0.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-attachments</artifactId> + <packaging>jar</packaging> + + <name>Camel :: Attachments</name> + <description>Java Attachments support for Camel Message</description> + + <properties> + <firstVersion>3.0.0</firstVersion> + <label>core</label> + </properties> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-support</artifactId> + <version>${project.version}</version> + </dependency> + + <!-- attachments api --> + <dependency> + <groupId>com.sun.activation</groupId> + <artifactId>javax.activation</artifactId> + <version>1.2.0</version> + <!-- TODO: move version to parent --> + </dependency> + + <!-- testing --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> +</project> diff --git a/core/camel-api/src/main/java/org/apache/camel/Attachment.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/Attachment.java similarity index 95% rename from core/camel-api/src/main/java/org/apache/camel/Attachment.java rename to components/camel-attachments/src/main/java/org/apache/camel/attachment/Attachment.java index fb9ff3f..9ad220a 100644 --- a/core/camel-api/src/main/java/org/apache/camel/Attachment.java +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/Attachment.java @@ -14,17 +14,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel; +package org.apache.camel.attachment; import java.util.Collection; import java.util.List; import javax.activation.DataHandler; +import org.apache.camel.Message; + /** * Represents an attachment as part of a {@link Message}. */ public interface Attachment { + + // TODO: Move this to camel-attachment + /** * Return a DataHandler for the content within this attachment. * diff --git a/core/camel-base/src/main/java/org/apache/camel/converter/AttachmentConverter.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentConverter.java similarity index 84% rename from core/camel-base/src/main/java/org/apache/camel/converter/AttachmentConverter.java rename to components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentConverter.java index e57afc2..4481be0 100644 --- a/core/camel-base/src/main/java/org/apache/camel/converter/AttachmentConverter.java +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentConverter.java @@ -14,17 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.converter; +package org.apache.camel.attachment; import javax.activation.DataHandler; -import org.apache.camel.Attachment; import org.apache.camel.Converter; +import org.apache.camel.Message; -/** - * Some useful converters for {@link Attachment} - * to a {@link DataHandler} - */ @Converter(loader = true) public final class AttachmentConverter { @@ -35,6 +31,11 @@ public final class AttachmentConverter { } @Converter + public static AttachmentMessage toAttachmentMessage(final Message message) { + return new DefaultAttachmentMessage(message); + } + + @Converter public static DataHandler toDataHandler(final Attachment attachment) { return attachment.getDataHandler(); } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/AttachmentMap.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMap.java similarity index 98% rename from core/camel-support/src/main/java/org/apache/camel/support/AttachmentMap.java rename to components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMap.java index a187fb0..121b50a 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/AttachmentMap.java +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMap.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.support; +package org.apache.camel.attachment; import java.util.AbstractMap; import java.util.AbstractSet; @@ -24,7 +24,7 @@ import java.util.Set; import javax.activation.DataHandler; -import org.apache.camel.Attachment; +// TODO: Use this map? /** * The AttachmentMap class provides a transparent Map<String, DataHandler> diff --git a/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMessage.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMessage.java new file mode 100644 index 0000000..dd89b35 --- /dev/null +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentMessage.java @@ -0,0 +1,111 @@ +/* + * 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.attachment; + +import java.util.Map; +import java.util.Set; +import javax.activation.DataHandler; + +import org.apache.camel.Message; + +/** + * Extended {@link Message} for Java Attachment Support (with javax.activation). + */ +public interface AttachmentMessage extends Message { + + /** + * Returns the attachment specified by the id + * + * @param id the id under which the attachment is stored + * @return the data handler for this attachment or <tt>null</tt> + */ + DataHandler getAttachment(String id); + + /** + * Returns the attachment specified by the id + * + * @param id the id under which the attachment is stored + * @return the attachment or <tt>null</tt> + */ + Attachment getAttachmentObject(String id); + + /** + * Returns a set of attachment names of the message + * + * @return a set of attachment names + */ + Set<String> getAttachmentNames(); + + /** + * Removes the attachment specified by the id + * + * @param id the id of the attachment to remove + */ + void removeAttachment(String id); + + /** + * Adds an attachment to the message using the id + * + * @param id the id to store the attachment under + * @param content the data handler for the attachment + */ + void addAttachment(String id, DataHandler content); + + /** + * Adds an attachment to the message using the id + * + * @param id the id to store the attachment under + * @param content the attachment + */ + void addAttachmentObject(String id, Attachment content); + + /** + * Returns all attachments of the message + * + * @return the attachments in a map or <tt>null</tt> + */ + Map<String, DataHandler> getAttachments(); + + /** + * Returns all attachments of the message + * + * @return the attachments in a map or <tt>null</tt> + */ + Map<String, Attachment> getAttachmentObjects(); + + /** + * Set all the attachments associated with this message + * + * @param attachments the attachments + */ + void setAttachments(Map<String, DataHandler> attachments); + + /** + * Set all the attachments associated with this message + * + * @param attachments the attachments + */ + void setAttachmentObjects(Map<String, Attachment> attachments); + + /** + * Returns whether this message has attachments. + * + * @return <tt>true</tt> if this message has any attachments. + */ + boolean hasAttachments(); + +} diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultAttachment.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachment.java similarity index 98% rename from core/camel-support/src/main/java/org/apache/camel/support/DefaultAttachment.java rename to components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachment.java index c18f0ec..466e317 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultAttachment.java +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachment.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.support; +package org.apache.camel.attachment; import java.util.Collection; import java.util.Collections; @@ -25,7 +25,6 @@ import java.util.TreeMap; import javax.activation.DataHandler; import javax.activation.DataSource; -import org.apache.camel.Attachment; import org.apache.camel.util.CollectionHelper; public class DefaultAttachment implements Attachment { diff --git a/components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachmentMessage.java b/components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachmentMessage.java new file mode 100644 index 0000000..8db9f32 --- /dev/null +++ b/components/camel-attachments/src/main/java/org/apache/camel/attachment/DefaultAttachmentMessage.java @@ -0,0 +1,279 @@ +/* + * 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.attachment; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.function.Supplier; + +import javax.activation.DataHandler; + +import org.apache.camel.Exchange; +import org.apache.camel.InvalidPayloadException; +import org.apache.camel.Message; + +public final class DefaultAttachmentMessage implements AttachmentMessage { + + public static final String ATTACHMENT_OBJECTS = "CamelAttachmentObjects"; + + private final Message delegate; + + public DefaultAttachmentMessage(Message delegate) { + this.delegate = delegate; + } + + @Override + public String getMessageId() { + return delegate.getMessageId(); + } + + @Override + public void setMessageId(String messageId) { + delegate.setMessageId(messageId); + } + + @Override + public Exchange getExchange() { + return delegate.getExchange(); + } + + @Override + public boolean isFault() { + return delegate.isFault(); + } + + @Override + public void setFault(boolean fault) { + delegate.setFault(fault); + } + + @Override + public Object getHeader(String name) { + return delegate.getHeader(name); + } + + @Override + public Object getHeader(String name, Object defaultValue) { + return delegate.getHeader(name, defaultValue); + } + + @Override + public Object getHeader(String name, Supplier<Object> defaultValueSupplier) { + return delegate.getHeader(name, defaultValueSupplier); + } + + @Override + public <T> T getHeader(String name, Class<T> type) { + return delegate.getHeader(name, type); + } + + @Override + public <T> T getHeader(String name, Object defaultValue, Class<T> type) { + return delegate.getHeader(name, defaultValue, type); + } + + @Override + public <T> T getHeader(String name, Supplier<Object> defaultValueSupplier, Class<T> type) { + return delegate.getHeader(name, defaultValueSupplier, type); + } + + @Override + public void setHeader(String name, Object value) { + delegate.setHeader(name, value); + } + + @Override + public Object removeHeader(String name) { + return delegate.removeHeader(name); + } + + @Override + public boolean removeHeaders(String pattern) { + return delegate.removeHeaders(pattern); + } + + @Override + public boolean removeHeaders(String pattern, String... excludePatterns) { + return delegate.removeHeaders(pattern, excludePatterns); + } + + @Override + public Map<String, Object> getHeaders() { + return delegate.getHeaders(); + } + + @Override + public void setHeaders(Map<String, Object> headers) { + delegate.setHeaders(headers); + } + + @Override + public boolean hasHeaders() { + return delegate.hasHeaders(); + } + + @Override + public Object getBody() { + return delegate.getBody(); + } + + @Override + public Object getMandatoryBody() throws InvalidPayloadException { + return delegate.getMandatoryBody(); + } + + @Override + public <T> T getBody(Class<T> type) { + return delegate.getBody(type); + } + + @Override + public <T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException { + return delegate.getMandatoryBody(type); + } + + @Override + public void setBody(Object body) { + delegate.setBody(body); + } + + @Override + public <T> void setBody(Object body, Class<T> type) { + delegate.setBody(body, type); + } + + @Override + public Message copy() { + return delegate.copy(); + } + + @Override + public void copyFrom(Message message) { + delegate.copyFrom(message); + } + + @Override + public void copyFromWithNewBody(Message message, Object newBody) { + delegate.copyFromWithNewBody(message, newBody); + } + + // TODO: implement these + + @Override + @SuppressWarnings("unchecked") + public DataHandler getAttachment(String id) { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map != null) { + Attachment att = map.get(id); + if (att != null) { + return att.getDataHandler(); + } + } + return null; + } + + @Override + @SuppressWarnings("unchecked") + public Attachment getAttachmentObject(String id) { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map != null) { + return map.get(id); + } + return null; + } + + @Override + @SuppressWarnings("unchecked") + public Set<String> getAttachmentNames() { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map != null) { + return map.keySet(); + } + return null; + } + + @Override + @SuppressWarnings("unchecked") + public void removeAttachment(String id) { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map != null) { + map.remove(id); + } + } + + @Override + @SuppressWarnings("unchecked") + public void addAttachment(String id, DataHandler content) { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map == null) { + map = new LinkedHashMap<>(); + getExchange().setProperty(ATTACHMENT_OBJECTS, map); + } + map.put(id, new DefaultAttachment(content)); + } + + @Override + @SuppressWarnings("unchecked") + public void addAttachmentObject(String id, Attachment content) { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map == null) { + map = new LinkedHashMap<>(); + getExchange().setProperty(ATTACHMENT_OBJECTS, map); + } + map.put(id, content); + } + + @Override + @SuppressWarnings("unchecked") + public Map<String, DataHandler> getAttachments() { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + if (map != null) { + Map<String, DataHandler> answer = new HashMap<>(); + map.forEach((id, att) -> answer.put(id, att.getDataHandler())); + return answer; + } + return null; + } + + @Override + @SuppressWarnings("unchecked") + public Map<String, Attachment> getAttachmentObjects() { + return getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + } + + @Override + public void setAttachments(Map<String, DataHandler> attachments) { + Map<String, Attachment> map = new HashMap<>(); + attachments.forEach((id, dh) -> map.put(id, new DefaultAttachment(dh))); + getExchange().setProperty(ATTACHMENT_OBJECTS, map); + } + + @Override + public void setAttachmentObjects(Map<String, Attachment> attachments) { + getExchange().setProperty(ATTACHMENT_OBJECTS, attachments); + } + + @Override + @SuppressWarnings("unchecked") + public boolean hasAttachments() { + Map<String, Attachment> map = getExchange().getProperty(ATTACHMENT_OBJECTS, Map.class); + return map != null && !map.isEmpty(); + } + +} diff --git a/components/camel-attachments/src/test/data/message1.xml b/components/camel-attachments/src/test/data/message1.xml new file mode 100644 index 0000000..1a85d06 --- /dev/null +++ b/components/camel-attachments/src/test/data/message1.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<person user="james"> + <firstName>James</firstName> + <lastName>Strachan</lastName> + <city>London</city> +</person> \ No newline at end of file diff --git a/components/camel-attachments/src/test/data/message2.xml b/components/camel-attachments/src/test/data/message2.xml new file mode 100644 index 0000000..73c88bf --- /dev/null +++ b/components/camel-attachments/src/test/data/message2.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<person user="hiram"> + <firstName>Hiram</firstName> + <lastName>Chirino</lastName> + <city>Tampa</city> +</person> \ No newline at end of file diff --git a/components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanMethodWithExchangeTest.java b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanMethodWithExchangeTest.java new file mode 100644 index 0000000..24ea353 --- /dev/null +++ b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanMethodWithExchangeTest.java @@ -0,0 +1,67 @@ +/* + * 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.attachment; + +import javax.activation.DataHandler; +import javax.activation.FileDataSource; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Assert; +import org.junit.Test; + +public class BeanMethodWithExchangeTest extends CamelTestSupport { + + @Test + public void testBeanWithAnnotationAndExchangeTest() throws Exception { + Exchange result = template.request("direct:start1", new Processor() { + + public void process(Exchange exchange) throws Exception { + AttachmentMessage m = exchange.getIn(AttachmentMessage.class); + m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/attachment/BeanMethodWithExchangeTest.java"))); + } + + }); + + Assert.assertTrue(result.getMessage(AttachmentMessage.class).getAttachmentObjects().containsKey("attachment2")); + Assert.assertTrue(result.getMessage(AttachmentMessage.class).getAttachments().containsKey("attachment1")); + assertEquals("attachmentValue1", result.getMessage(AttachmentMessage.class).getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1")); + Assert.assertFalse(result.getMessage(AttachmentMessage.class).getAttachments().containsKey("attachment")); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("direct:start1").process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // remove the old attachment + exchange.getMessage(AttachmentMessage.class).removeAttachment("attachment"); + // and add 2 new attachments + Attachment att = new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/attachment/BeanMethodWithExchangeTest.java")); + att.addHeader("attachmentHeader1", "attachmentValue1"); + exchange.getMessage(AttachmentMessage.class).addAttachmentObject("attachment1", att); + exchange.getMessage(AttachmentMessage.class).addAttachment("attachment2", new DataHandler(new FileDataSource("src/test/org/apache/camel/support/attachments/BeanMethodWithExchangeTest.java"))); + } + }); + } + }; + } + +} diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanWithAttachmentAnnotationTest.java similarity index 71% rename from core/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java rename to components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanWithAttachmentAnnotationTest.java index 36f9f87..c3d2d30 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java +++ b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BeanWithAttachmentAnnotationTest.java @@ -14,29 +14,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.bean; +package org.apache.camel.attachment; import java.util.Map; - import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.naming.Context; -import org.apache.camel.Attachment; import org.apache.camel.AttachmentObjects; import org.apache.camel.Attachments; -import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; import org.apache.camel.ExchangePattern; -import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.support.DefaultAttachment; import org.apache.camel.support.jndi.JndiContext; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; -public class BeanWithAttachmentAnnotationTest extends ContextTestSupport { +@Ignore("Need to make camel-bean support this") +public class BeanWithAttachmentAnnotationTest extends CamelTestSupport { @Test public void testBeanWithOldAnnotationAndExchangeTest() throws Exception { @@ -47,8 +46,8 @@ public class BeanWithAttachmentAnnotationTest extends ContextTestSupport { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); - Message m = exchange.getIn(); - m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java"))); + AttachmentMessage m = exchange.getIn(AttachmentMessage.class); + m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/attachment/BeanWithAttachmentAnnotationTest.java"))); } }); @@ -65,8 +64,8 @@ public class BeanWithAttachmentAnnotationTest extends ContextTestSupport { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); - Message m = exchange.getIn(); - m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java"))); + AttachmentMessage m = exchange.getIn(AttachmentMessage.class); + m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/attachment/BeanWithAttachmentAnnotationTest.java"))); } }); @@ -93,14 +92,14 @@ public class BeanWithAttachmentAnnotationTest extends ContextTestSupport { public static class AttachmentProcessorOld { // START SNIPPET: e1 public String doSomething(@Attachments Map<String, DataHandler> attachments) { - assertNotNull(attachments); - assertEquals("The attache size is wrong", 1, attachments.size()); + Assert.assertNotNull(attachments); + Assert.assertEquals("The attache size is wrong", 1, attachments.size()); String key = attachments.keySet().iterator().next(); - assertNotNull(key); - assertNotNull(attachments.get(key)); + Assert.assertNotNull(key); + Assert.assertNotNull(attachments.get(key)); DataHandler handler = attachments.get(key); - assertNotNull(handler); - assertTrue("The data source should be a instance of FileDataSource", handler.getDataSource() instanceof FileDataSource); + Assert.assertNotNull(handler); + Assert.assertTrue("The data source should be a instance of FileDataSource", handler.getDataSource() instanceof FileDataSource); return key; } // END SNIPPET: e1 @@ -109,16 +108,16 @@ public class BeanWithAttachmentAnnotationTest extends ContextTestSupport { public static class AttachmentProcessor { // START SNIPPET: e2 public String doSomething(@AttachmentObjects Map<String, Attachment> attachments) { - assertNotNull(attachments); - assertEquals("The attache size is wrong", 1, attachments.size()); + Assert.assertNotNull(attachments); + Assert.assertEquals("The attache size is wrong", 1, attachments.size()); String key = attachments.keySet().iterator().next(); - assertNotNull(key); - assertNotNull(attachments.get(key)); + Assert.assertNotNull(key); + Assert.assertNotNull(attachments.get(key)); Attachment attachment = attachments.get(key); - assertNotNull(attachment); + Assert.assertNotNull(attachment); DataHandler handler = attachment.getDataHandler(); - assertNotNull(handler); - assertTrue("The data source should be a instance of FileDataSource", handler.getDataSource() instanceof FileDataSource); + Assert.assertNotNull(handler); + Assert.assertTrue("The data source should be a instance of FileDataSource", handler.getDataSource() instanceof FileDataSource); return key; } // END SNIPPET: e2 diff --git a/core/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BodyAndHeaderConvertTest.java similarity index 75% rename from core/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java rename to components/camel-attachments/src/test/java/org/apache/camel/attachment/BodyAndHeaderConvertTest.java index f2a76cf..640b78a 100644 --- a/core/camel-core/src/test/java/org/apache/camel/BodyAndHeaderConvertTest.java +++ b/components/camel-attachments/src/test/java/org/apache/camel/attachment/BodyAndHeaderConvertTest.java @@ -14,18 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel; +package org.apache.camel.attachment; import java.net.URL; - import javax.activation.DataHandler; import javax.activation.URLDataSource; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.apache.camel.Exchange; import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.support.DefaultAttachment; import org.apache.camel.support.DefaultExchange; import org.junit.Assert; import org.junit.Before; @@ -37,37 +36,36 @@ public class BodyAndHeaderConvertTest extends Assert { @Test public void testConversionOfBody() throws Exception { Document document = exchange.getIn().getBody(Document.class); - assertNotNull(document); + Assert.assertNotNull(document); Element element = document.getDocumentElement(); - assertEquals("Root element name", "hello", element.getLocalName()); + Assert.assertEquals("Root element name", "hello", element.getLocalName()); } @Test public void testConversionOfExchangeProperties() throws Exception { String text = exchange.getProperty("foo", String.class); - assertEquals("foo property", "1234", text); + Assert.assertEquals("foo property", "1234", text); } @Test public void testConversionOfMessageHeaders() throws Exception { String text = exchange.getIn().getHeader("bar", String.class); - assertEquals("bar header", "567", text); + Assert.assertEquals("bar header", "567", text); } @Test public void testConversionOfMessageAttachments() throws Exception { - DataHandler handler = exchange.getIn().getAttachment("att"); - assertNotNull("attachment got lost", handler); - Attachment attachment = exchange.getIn().getAttachmentObject("att"); - assertNotNull("attachment got lost", attachment); + DataHandler handler = exchange.getIn(AttachmentMessage.class).getAttachment("att"); + Assert.assertNotNull("attachment got lost", handler); + Attachment attachment = exchange.getIn(AttachmentMessage.class).getAttachmentObject("att"); + Assert.assertNotNull("attachment got lost", attachment); } @Before public void setUp() throws Exception { - exchange = new DefaultExchange(new DefaultCamelContext()); exchange.setProperty("foo", 1234); - Message message = exchange.getIn(); + AttachmentMessage message = exchange.getIn(AttachmentMessage.class); message.setBody("<hello>world!</hello>"); message.setHeader("bar", 567); message.addAttachmentObject("att", new DefaultAttachment(new URLDataSource(new URL("http://camel.apache.org/message.html")))); diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java b/components/camel-attachments/src/test/java/org/apache/camel/attachment/MessageWithAttachmentRedeliveryIssueTest.java similarity index 75% rename from core/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java rename to components/camel-attachments/src/test/java/org/apache/camel/attachment/MessageWithAttachmentRedeliveryIssueTest.java index c6869aa..c72c851 100644 --- a/core/camel-core/src/test/java/org/apache/camel/issues/MessageWithAttachmentRedeliveryIssueTest.java +++ b/components/camel-attachments/src/test/java/org/apache/camel/attachment/MessageWithAttachmentRedeliveryIssueTest.java @@ -14,25 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.issues; +package org.apache.camel.attachment; import java.io.File; - import javax.activation.DataHandler; import javax.activation.FileDataSource; -import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; -import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.support.DefaultAttachment; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Assert; import org.junit.Test; /** * */ -public class MessageWithAttachmentRedeliveryIssueTest extends ContextTestSupport { +public class MessageWithAttachmentRedeliveryIssueTest extends CamelTestSupport { @Test public void testMessageWithAttachmentRedeliveryIssue() throws Exception { @@ -42,18 +40,18 @@ public class MessageWithAttachmentRedeliveryIssueTest extends ContextTestSupport @Override public void process(Exchange exchange) throws Exception { exchange.getIn().setBody("Hello World"); - exchange.getIn().addAttachment("message1.xml", new DataHandler(new FileDataSource(new File("src/test/data/message1.xml")))); - exchange.getIn().addAttachmentObject("message2.xml", new DefaultAttachment(new FileDataSource(new File("src/test/data/message2.xml")))); + exchange.getIn(AttachmentMessage.class).addAttachment("message1.xml", new DataHandler(new FileDataSource(new File("src/test/data/message1.xml")))); + exchange.getIn(AttachmentMessage.class).addAttachmentObject("message2.xml", new DefaultAttachment(new FileDataSource(new File("src/test/data/message2.xml")))); } }); assertMockEndpointsSatisfied(); - Message msg = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn(); - assertNotNull(msg); + AttachmentMessage msg = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn(AttachmentMessage.class); + Assert.assertNotNull(msg); assertEquals("Hello World", msg.getBody()); - assertTrue(msg.hasAttachments()); + Assert.assertTrue(msg.hasAttachments()); } @Override diff --git a/components/camel-attachments/src/test/resources/log4j2.properties b/components/camel-attachments/src/test/resources/log4j2.properties new file mode 100644 index 0000000..4262769 --- /dev/null +++ b/components/camel-attachments/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-attachments-test.log +appender.file.layout.type = PatternLayout +appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n +appender.out.type = Console +appender.out.name = out +appender.out.layout.type = PatternLayout +appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n +rootLogger.level = INFO +rootLogger.appenderRef.file.ref = file diff --git a/core/camel-api/src/main/java/org/apache/camel/Message.java b/core/camel-api/src/main/java/org/apache/camel/Message.java index 8f99a97..e3022c0a 100644 --- a/core/camel-api/src/main/java/org/apache/camel/Message.java +++ b/core/camel-api/src/main/java/org/apache/camel/Message.java @@ -17,11 +17,8 @@ package org.apache.camel; import java.util.Map; -import java.util.Set; import java.util.function.Supplier; -import javax.activation.DataHandler; - import org.apache.camel.spi.HeadersMapFactory; /** @@ -29,8 +26,7 @@ import org.apache.camel.spi.HeadersMapFactory; * href="http://camel.apache.org/message.html">Message</a> pattern and * represents an inbound or outbound message as part of an {@link Exchange}. * <p/> - * See {@link org.apache.camel.support.DefaultMessage DefaultMessage} for how headers - * is represented in Camel using a {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. + * Headers is represented in Camel using a {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. * The implementation of the map can be configured by the {@link HeadersMapFactory} which can be set * on the {@link CamelContext}. The default implementation uses the {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. */ @@ -173,8 +169,7 @@ public interface Message { /** * Returns all of the headers associated with the message. * <p/> - * See {@link org.apache.camel.support.DefaultMessage DefaultMessage} for how headers - * is represented in Camel using a {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. + * Headers is represented in Camel using a {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. * The implementation of the map can be configured by the {@link HeadersMapFactory} which can be set * on the {@link CamelContext}. The default implementation uses the {@link org.apache.camel.util.CaseInsensitiveMap CaseInsensitiveMap}. * <p/> @@ -303,92 +298,4 @@ public interface Message { */ void copyFromWithNewBody(Message message, Object newBody); - /** - * Copies the attachments of the other message into this message - * - * @param message the other message - */ - void copyAttachments(Message message); - - /** - * Returns the attachment specified by the id - * - * @param id the id under which the attachment is stored - * @return the data handler for this attachment or <tt>null</tt> - */ - DataHandler getAttachment(String id); - - /** - * Returns the attachment specified by the id - * - * @param id the id under which the attachment is stored - * @return the attachment or <tt>null</tt> - */ - Attachment getAttachmentObject(String id); - - /** - * Returns a set of attachment names of the message - * - * @return a set of attachment names - */ - Set<String> getAttachmentNames(); - - /** - * Removes the attachment specified by the id - * - * @param id the id of the attachment to remove - */ - void removeAttachment(String id); - - /** - * Adds an attachment to the message using the id - * - * @param id the id to store the attachment under - * @param content the data handler for the attachment - */ - void addAttachment(String id, DataHandler content); - - /** - * Adds an attachment to the message using the id - * - * @param id the id to store the attachment under - * @param content the attachment - */ - void addAttachmentObject(String id, Attachment content); - - /** - * Returns all attachments of the message - * - * @return the attachments in a map or <tt>null</tt> - */ - Map<String, DataHandler> getAttachments(); - - /** - * Returns all attachments of the message - * - * @return the attachments in a map or <tt>null</tt> - */ - Map<String, Attachment> getAttachmentObjects(); - - /** - * Set all the attachments associated with this message - * - * @param attachments the attachments - */ - void setAttachments(Map<String, DataHandler> attachments); - - /** - * Set all the attachments associated with this message - * - * @param attachments the attachments - */ - void setAttachmentObjects(Map<String, Attachment> attachments); - - /** - * Returns whether this message has attachments. - * - * @return <tt>true</tt> if this message has any attachments. - */ - boolean hasAttachments(); - } diff --git a/core/camel-base/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java b/core/camel-base/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java index ba5d749..27d725a 100644 --- a/core/camel-base/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java +++ b/core/camel-base/src/main/java/org/apache/camel/processor/ClaimCheckAggregationStrategy.java @@ -34,7 +34,6 @@ import org.slf4j.LoggerFactory; * This strategy supports the following include rules syntax: * <ul> * <li>body</li> - to aggregate the message body - * <li>attachments</li> - to aggregate all the message attachments * <li>headers</li> - to aggregate all the message headers * <li>header:pattern</li> - to aggregate all the message headers that matches the pattern. * The pattern syntax is documented by: {@link PatternHelper#matchPattern(String, String)}. @@ -74,10 +73,6 @@ public class ClaimCheckAggregationStrategy implements AggregationStrategy { oldExchange.getMessage().getHeaders().putAll(newExchange.getMessage().getHeaders()); LOG.trace("Including: headers"); } - if (newExchange.getMessage().hasAttachments()) { - oldExchange.getMessage().getAttachments().putAll(newExchange.getMessage().getAttachments()); - LOG.trace("Including: attachments"); - } return oldExchange; } @@ -87,14 +82,6 @@ public class ClaimCheckAggregationStrategy implements AggregationStrategy { LOG.trace("Including: body"); } - // attachments is by default often included - if (isAttachmentsEnabled()) { - if (newExchange.getMessage().hasAttachments()) { - oldExchange.getMessage().getAttachments().putAll(newExchange.getMessage().getAttachments()); - LOG.trace("Including: attachments"); - } - } - // headers is by default often included if (isHeadersEnabled()) { if (newExchange.getMessage().hasHeaders()) { @@ -271,26 +258,6 @@ public class ClaimCheckAggregationStrategy implements AggregationStrategy { return onlyExclude; } - private boolean isAttachmentsEnabled() { - // attachments is always enabled unless excluded - String[] parts = filter.split(","); - - boolean onlyExclude = true; - for (String pattern : parts) { - if (pattern.startsWith("--")) { - continue; - } - if ("attachments".equals(pattern) || "+attachments".equals(pattern)) { - return true; - } else if ("-attachments".equals(pattern)) { - return false; - } - onlyExclude &= pattern.startsWith("-"); - } - // attachments is enabled if we only have exclude patterns - return onlyExclude; - } - private boolean isHeadersEnabled() { // headers may be enabled unless excluded String[] parts = filter.split(","); diff --git a/core/camel-base/src/main/java/org/apache/camel/processor/Splitter.java b/core/camel-base/src/main/java/org/apache/camel/processor/Splitter.java index 630d6a5..44cbf12 100644 --- a/core/camel-base/src/main/java/org/apache/camel/processor/Splitter.java +++ b/core/camel-base/src/main/java/org/apache/camel/processor/Splitter.java @@ -149,7 +149,7 @@ public class Splitter extends MulticastProcessor implements AsyncProcessor, Trac this.original = exchange; this.value = value; this.iterator = ObjectHelper.createIterator(value); - this.copy = copyExchangeNoAttachments(exchange, true); + this.copy = copyAndPrepareSubExchange(exchange, true); this.routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null; } @@ -269,10 +269,8 @@ public class Splitter extends MulticastProcessor implements AsyncProcessor, Trac return expression; } - private static Exchange copyExchangeNoAttachments(Exchange exchange, boolean preserveExchangeId) { + private static Exchange copyAndPrepareSubExchange(Exchange exchange, boolean preserveExchangeId) { Exchange answer = ExchangeHelper.createCopy(exchange, preserveExchangeId); - // we do not want attachments for the splitted sub-messages - answer.getIn().setAttachmentObjects(null); // we do not want to copy the message history for splitted sub-messages answer.getProperties().remove(Exchange.MESSAGE_HISTORY); return answer; diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java index 538a32d..1f7a354 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java @@ -293,13 +293,6 @@ public class ExpressionClause<T> implements Expression, Predicate { } /** - * An expression of the inbound message attachments - */ - public T attachments() { - return delegate.attachments(); - } - - /** * An expression of an exchange property of the given name */ public T exchangeProperty(String name) { diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java index 58a9bd0..1693722 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java @@ -171,13 +171,6 @@ public class ExpressionClauseSupport<T> { } /** - * An expression of the inbound message attachments - */ - public T attachments() { - return expression(ExpressionBuilder.attachmentObjectValuesExpression()); - } - - /** * An expression of the exchange pattern */ public T exchangePattern() { diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseTest.java deleted file mode 100644 index c624389..0000000 --- a/core/camel-core/src/test/java/org/apache/camel/builder/ExpressionClauseTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.builder; - -import java.util.Map; - -import javax.activation.DataHandler; -import javax.activation.DataSource; -import javax.activation.FileDataSource; - -import org.apache.camel.Attachment; -import org.apache.camel.ContextTestSupport; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.support.DefaultAttachment; -import org.junit.Test; - -public class ExpressionClauseTest extends ContextTestSupport { - - @Test - public void testConstant() throws Exception { - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedMessageCount(1); - mock.expectedBodyReceived().constant("Hello World"); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - @Test - public void testAttachments() throws Exception { - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedMessageCount(2); - mock.expectedBodiesReceivedInAnyOrder("log4j2.properties", "jndi-example.properties"); - - template.send("direct:begin", new Processor() { - @Override - public void process(Exchange exchange) throws Exception { - Message m = exchange.getIn(); - m.setBody("Hello World"); - m.addAttachmentObject("log4j", new DefaultAttachment(new FileDataSource("src/test/resources/log4j2.properties"))); - m.addAttachment("jndi-example", new DataHandler(new FileDataSource("src/test/resources/jndi-example.properties"))); - } - }); - - assertMockEndpointsSatisfied(); - Map<String, Attachment> attachments = mock.getExchanges().get(0).getIn().getAttachmentObjects(); - assertTrue(attachments == null || attachments.size() == 0); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start").to("mock:result"); - from("direct:begin") - .split().attachments() - // extract just the name from the DataHandler/DataSource to simplify assertions - .bean(new Extractor()) - .to("mock:result"); - } - }; - } - - public final class Extractor { - public String extractName(DataHandler body) { - DataSource ds = (body != null) ? body.getDataSource() : null; - if (ds instanceof FileDataSource) { - return ((FileDataSource)ds).getName(); - } - return null; - } - } -} diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithExchangeTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithExchangeTest.java deleted file mode 100644 index daecba1..0000000 --- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodWithExchangeTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.bean; - -import javax.activation.DataHandler; -import javax.activation.FileDataSource; -import javax.naming.Context; - -import org.apache.camel.Attachment; -import org.apache.camel.ContextTestSupport; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.apache.camel.Processor; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.support.DefaultAttachment; -import org.apache.camel.support.jndi.JndiContext; -import org.junit.Test; - -public class BeanMethodWithExchangeTest extends ContextTestSupport { - - @Test - public void testBeanWithAnnotationAndExchangeTest() throws Exception { - Exchange result = template.request("direct:start1", new Processor() { - - public void process(Exchange exchange) throws Exception { - Message m = exchange.getIn(); - m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java"))); - } - - }); - - assertTrue(result.getOut().getAttachmentObjects().containsKey("attachment2")); - assertTrue(result.getOut().getAttachments().containsKey("attachment1")); - assertEquals("attachmentValue1", result.getOut().getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1")); - assertFalse(result.getOut().getAttachments().containsKey("attachment")); - - } - - protected Context createJndiContext() throws Exception { - JndiContext answer = new JndiContext(); - answer.bind("processor", new AttachmentProcessor()); - return answer; - } - - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - public void configure() throws Exception { - from("direct:start1").to("bean:processor"); - } - }; - } - - public static class AttachmentProcessor { - public void doSomething(Exchange exchange) { - Attachment att = new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/component/bean/BeanMethodWithExchangeTest.java")); - att.addHeader("attachmentHeader1", "attachmentValue1"); - exchange.getOut().addAttachmentObject("attachment1", att); - exchange.getOut().addAttachment("attachment2", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanMethodWithExchangeTest.java"))); - } - - } - -} diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java index 5e8ecab..9a51ab4 100644 --- a/core/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java @@ -15,12 +15,9 @@ * limitations under the License. */ package org.apache.camel.processor; -import javax.activation.DataHandler; -import javax.activation.FileDataSource; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; -import org.apache.camel.ExchangePattern; import org.apache.camel.Message; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; @@ -125,24 +122,6 @@ public class PipelineTest extends ContextTestSupport { assertEquals(3, exchange.getOut().getHeader("copy-counter")); } - @Test - public void testCopyInOutExchange() { - Exchange exchange = template.request("direct:start", new Processor() { - public void process(Exchange exchange) { - exchange.setPattern(ExchangePattern.InOut); - exchange.getIn().setBody("test"); - } - }); - assertEquals("There should have no message header", 0, exchange.getOut().getHeaders().size()); - assertEquals("There should have no attachments", 0, exchange.getOut().getAttachmentObjects().size()); - assertEquals("There should have no attachments", 0, exchange.getOut().getAttachments().size()); - assertEquals("Get a wrong message body", "test", exchange.getOut().getBody()); - assertNull(exchange.getOut().getHeader("test")); - assertNull(exchange.getOut().getAttachmentObject("test1.xml")); - assertNull(exchange.getOut().getAttachment("test1.xml")); - - } - @Override @Before public void setUp() throws Exception { @@ -176,26 +155,6 @@ public class PipelineTest extends ContextTestSupport { from("direct:b").process(new InToOut()).process(new InToOut()).process(new InToOut()); // Create a route that uses the InToFault processor.. the last InToOut will not be called since the Fault occurs before. from("direct:c").process(new InToOut()).process(new InToFault()).process(new InToOut()); - - from("direct:start") - .process(new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.getOut().copyFrom(exchange.getIn()); - //Added the header and attachment - exchange.getOut().setHeader("test", "testValue"); - exchange.getOut().addAttachment("test1.xml", new DataHandler(new FileDataSource("pom.xml"))); - } - }) - .process(new Processor() { - public void process(Exchange exchange) throws Exception { - exchange.getOut().copyFrom(exchange.getIn()); - assertNotNull("The test attachment should not be null", exchange.getOut().getAttachmentObject("test1.xml")); - assertNotNull("The test attachment should not be null", exchange.getOut().getAttachment("test1.xml")); - assertNotNull("The test header should not be null", exchange.getOut().getHeader("test")); - exchange.getOut().removeAttachment("test1.xml"); - exchange.getOut().removeHeader("test"); - } - }); } }; } diff --git a/core/camel-core/src/test/java/org/apache/camel/support/DefaultMessageTest.java b/core/camel-core/src/test/java/org/apache/camel/support/DefaultMessageTest.java deleted file mode 100644 index 606e60e..0000000 --- a/core/camel-core/src/test/java/org/apache/camel/support/DefaultMessageTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.support; - -import java.util.LinkedHashMap; -import java.util.Map; - -import org.apache.camel.Attachment; -import org.apache.camel.impl.DefaultCamelContext; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; - -public class DefaultMessageTest { - - @Test - public void testAttachmentsAreSorted() { - DefaultMessage message = new DefaultMessage(new DefaultCamelContext()); - - Map<String, Attachment> attachments = message.createAttachments(); - - assertThat(attachments, instanceOf(LinkedHashMap.class)); - } - -} diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java index c028477..5d4a8d3 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultExchange.java @@ -101,8 +101,6 @@ public final class DefaultExchange implements Exchange { exchange.getIn().setFault(getIn().isFault()); if (getIn().hasHeaders()) { exchange.getIn().setHeaders(safeCopyHeaders(getIn().getHeaders())); - // just copy the attachments here - exchange.getIn().copyAttachments(getIn()); } if (hasOut()) { exchange.setOut(getOut().copy()); @@ -111,8 +109,6 @@ public final class DefaultExchange implements Exchange { if (getOut().hasHeaders()) { exchange.getOut().setHeaders(safeCopyHeaders(getOut().getHeaders())); } - // Just copy the attachments here - exchange.getOut().copyAttachments(getOut()); } exchange.setException(getException()); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java index 5a3f085..a2a3cd7 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessage.java @@ -17,14 +17,10 @@ package org.apache.camel.support; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.function.Supplier; -import javax.activation.DataHandler; - -import org.apache.camel.Attachment; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.spi.HeadersMapFactory; @@ -43,8 +39,6 @@ import org.apache.camel.util.ObjectHelper; public class DefaultMessage extends MessageSupport { private boolean fault; private Map<String, Object> headers; - private Map<String, DataHandler> attachments; - private Map<String, Attachment> attachmentObjects; public DefaultMessage(Exchange exchange) { setExchange(exchange); @@ -254,19 +248,6 @@ public class DefaultMessage extends MessageSupport { } /** - * A factory method to lazily create the attachmentObjects to make it easy to - * create efficient Message implementations which only construct and - * populate the Map on demand - * - * @return return a newly constructed Map - */ - protected Map<String, Attachment> createAttachments() { - Map<String, Attachment> map = new LinkedHashMap<>(); - populateInitialAttachments(map); - return map; - } - - /** * A strategy method populate the initial set of headers on an inbound * message from an underlying binding * @@ -277,16 +258,6 @@ public class DefaultMessage extends MessageSupport { } /** - * A strategy method populate the initial set of attachmentObjects on an inbound - * message from an underlying binding - * - * @param map is the empty attachment map to populate - */ - protected void populateInitialAttachments(Map<String, Attachment> map) { - // do nothing by default - } - - /** * A strategy for component specific messages to determine whether the * message is redelivered or not. * <p/> @@ -301,85 +272,6 @@ public class DefaultMessage extends MessageSupport { return null; } - public void addAttachment(String id, DataHandler content) { - addAttachmentObject(id, new DefaultAttachment(content)); - } - - public void addAttachmentObject(String id, Attachment content) { - if (attachmentObjects == null) { - attachmentObjects = createAttachments(); - } - attachmentObjects.put(id, content); - } - - public DataHandler getAttachment(String id) { - Attachment att = getAttachmentObject(id); - if (att == null) { - return null; - } else { - return att.getDataHandler(); - } - } - - @Override - public Attachment getAttachmentObject(String id) { - return getAttachmentObjects().get(id); - } - - public Set<String> getAttachmentNames() { - if (attachmentObjects == null) { - attachmentObjects = createAttachments(); - } - return attachmentObjects.keySet(); - } - - public void removeAttachment(String id) { - if (attachmentObjects != null && attachmentObjects.containsKey(id)) { - attachmentObjects.remove(id); - } - } - - public Map<String, DataHandler> getAttachments() { - if (attachments == null) { - attachments = new AttachmentMap(getAttachmentObjects()); - } - return attachments; - } - - public Map<String, Attachment> getAttachmentObjects() { - if (attachmentObjects == null) { - attachmentObjects = createAttachments(); - } - return attachmentObjects; - } - - public void setAttachments(Map<String, DataHandler> attachments) { - if (attachments == null) { - this.attachmentObjects = null; - } else if (attachments instanceof AttachmentMap) { - // this way setAttachments(getAttachments()) will tunnel attachment headers - this.attachmentObjects = ((AttachmentMap)attachments).getOriginalMap(); - } else { - this.attachmentObjects = new LinkedHashMap<>(); - for (Map.Entry<String, DataHandler> entry : attachments.entrySet()) { - this.attachmentObjects.put(entry.getKey(), new DefaultAttachment(entry.getValue())); - } - } - this.attachments = null; - } - - public void setAttachmentObjects(Map<String, Attachment> attachments) { - this.attachmentObjects = attachments; - this.attachments = null; - } - - public boolean hasAttachments() { - // optimized to avoid calling createAttachments as that creates a new empty map - // that we 99% do not need (only camel-mail supports attachments), and we have - // then ensure camel-mail always creates attachments to remedy for this - return this.attachmentObjects != null && this.attachmentObjects.size() > 0; - } - /** * Returns true if the headers have been mutated in some way */ diff --git a/core/camel-support/src/main/java/org/apache/camel/support/MessageSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/MessageSupport.java index 5a9874f..d20a96d 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/MessageSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/MessageSupport.java @@ -231,8 +231,6 @@ public abstract class MessageSupport implements Message, CamelContextAware, Data getHeaders().putAll(that.getHeaders()); } } - - copyAttachments(that); } @Override @@ -254,26 +252,6 @@ public abstract class MessageSupport implements Message, CamelContextAware, Data this.camelContext = camelContext; } - @Override - public void copyAttachments(Message that) { - // the attachments may be the same instance if the end user has made some mistake - // and set the OUT message with the same attachment instance of the IN message etc - boolean sameAttachments = false; - if (hasAttachments() && that.hasAttachments() && getAttachmentObjects() == that.getAttachmentObjects()) { - sameAttachments = true; - } - - if (!sameAttachments) { - if (hasAttachments()) { - // okay its safe to clear the attachments - getAttachmentObjects().clear(); - } - if (that.hasAttachments()) { - getAttachmentObjects().putAll(that.getAttachmentObjects()); - } - } - } - /** * Returns a new instance */ 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 4965e31..07ab27d 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 @@ -58,78 +58,6 @@ import org.apache.camel.util.StringHelper; public class ExpressionBuilder { /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentObjectsExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachmentObjects(); - } - - @Override - public String toString() { - return "attachmentObjects"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentObjectValuesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachmentObjects().values(); - } - - @Override - public String toString() { - return "attachmentObjects"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentsExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachments(); - } - - @Override - public String toString() { - return "attachments"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentValuesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachments().values(); - } - - @Override - public String toString() { - return "attachments"; - } - }; - } - - /** * Returns an expression for the header value with the given name * <p/> * Will fallback and look in properties if not found in headers. diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootStarterMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootStarterMojo.java index d4eac410..a7bbf36 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootStarterMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/SpringBootStarterMojo.java @@ -75,6 +75,7 @@ public class SpringBootStarterMojo extends AbstractMojo { private static final String[] IGNORE_MODULES = { /* OSGi -> */ "camel-blueprint", "camel-core-osgi", "camel-eventadmin", "camel-paxlogging", + /* extended core */ "camel-attachments", /* Java EE -> */ "camel-cdi", "camel-ejb", /* Microprofile -> */ "camel-microprofile-config", /* deprecated (and not working perfectly) -> */ "camel-swagger", "camel-mina", "camel-ibatis",