Skip to content

Commit

Permalink
CAMEL-9880: Header Support for Attachments in Camel 2.18
Browse files Browse the repository at this point in the history
  • Loading branch information
stsiano committed Jul 19, 2016
1 parent 00b5913 commit 50d6d4f
Show file tree
Hide file tree
Showing 44 changed files with 829 additions and 94 deletions.
83 changes: 83 additions & 0 deletions camel-core/src/main/java/org/apache/camel/Attachment.java
@@ -0,0 +1,83 @@
/**
* 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;

import java.util.Collection;
import java.util.List;

import javax.activation.DataHandler;

/**
* Represents an attachment as part of a {@link Message}.
*/
public interface Attachment {
/**
* Return a DataHandler for the content within this attachment.
*
* @return DataHandler for the content
*/
DataHandler getDataHandler();

/**
* Get all the headers for this header name. Returns null if no headers for
* this header name are available.
*
* @param headerName he name of this header
* @return a comma separated list of all header values
*/
String getHeader(String headerName);

/**
* Get all the headers for this header name. Returns null if no headers for
* this header name are available.
*
* @param headerName he name of this header
* @return a list of all header values
*/
List<String> getHeaderAsList(String name);

/**
* Get all header names for this attachment.
*
* @return a collection of all header names
*/
Collection<String> getHeaderNames();

/**
* Set the value for this headerName. Replaces all existing header values
* with this new value.
*
* @param headerName the name of this header
* @param headerValue the value for this header
*/
void setHeader(String headerName, String headerValue);

/**
* Add this value to the existing values for this headerName.
*
* @param headerName the name of this header
* @param headerValue the value for this header
*/
void addHeader(String headerName, String headerValue);

/**
* Remove all headers with this name.
*
* @param headerName the name of this header
*/
void removeHeader(String headerName);
}
35 changes: 35 additions & 0 deletions camel-core/src/main/java/org/apache/camel/AttachmentObjects.java
@@ -0,0 +1,35 @@
/**
* 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;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Marks a parameter as being Map of attachments as {@link Attachment} objects
* of an inbound {@link Message}
*
* @version
*/
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ElementType.PARAMETER })
public @interface AttachmentObjects {
}
5 changes: 3 additions & 2 deletions camel-core/src/main/java/org/apache/camel/Attachments.java
Expand Up @@ -23,9 +23,10 @@
import java.lang.annotation.Target; import java.lang.annotation.Target;


/** /**
* Marks a parameter as being Map of attachments of an inbound {@link Message} * Marks a parameter as being Map of attachments as
* {@link javax.activation.DataHandler} objects of an inbound {@link Message}
* *
* @version * @version
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
Expand Down
30 changes: 30 additions & 0 deletions camel-core/src/main/java/org/apache/camel/Message.java
Expand Up @@ -255,6 +255,14 @@ public interface Message {
*/ */
DataHandler getAttachment(String id); 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 * Returns a set of attachment names of the message
* *
Expand All @@ -277,20 +285,42 @@ public interface Message {
*/ */
void addAttachment(String id, DataHandler content); 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 * Returns all attachments of the message
* *
* @return the attachments in a map or <tt>null</tt> * @return the attachments in a map or <tt>null</tt>
*/ */
Map<String, DataHandler> getAttachments(); 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 * Set all the attachments associated with this message
* *
* @param attachments the attachments * @param attachments the attachments
*/ */
void setAttachments(Map<String, DataHandler> 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. * Returns whether this message has attachments.
* *
Expand Down
Expand Up @@ -80,6 +80,42 @@ public final class ExpressionBuilder {
private ExpressionBuilder() { private 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 * Returns an expression for the inbound message attachments
* *
Expand Down
Expand Up @@ -171,7 +171,7 @@ public T outHeaders() {
* An expression of the inbound message attachments * An expression of the inbound message attachments
*/ */
public T attachments() { public T attachments() {
return expression(ExpressionBuilder.attachmentValuesExpression()); return expression(ExpressionBuilder.attachmentObjectValuesExpression());
} }


/** /**
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


import org.apache.camel.AttachmentObjects;
import org.apache.camel.Attachments; import org.apache.camel.Attachments;
import org.apache.camel.Body; import org.apache.camel.Body;
import org.apache.camel.CamelContext; import org.apache.camel.CamelContext;
Expand Down Expand Up @@ -946,7 +947,9 @@ private Expression createParameterUnmarshalExpression(Class<?> clazz, Method met


private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method,
Class<?> parameterType, Annotation annotation) { Class<?> parameterType, Annotation annotation) {
if (annotation instanceof Attachments) { if (annotation instanceof AttachmentObjects) {
return ExpressionBuilder.attachmentObjectsExpression();
} else if (annotation instanceof Attachments) {
return ExpressionBuilder.attachmentsExpression(); return ExpressionBuilder.attachmentsExpression();
} else if (annotation instanceof Property) { } else if (annotation instanceof Property) {
Property propertyAnnotation = (Property)annotation; Property propertyAnnotation = (Property)annotation;
Expand Down
@@ -0,0 +1,43 @@
/**
* 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.converter;

import javax.activation.DataHandler;

import org.apache.camel.Attachment;
import org.apache.camel.Converter;

/**
* Some useful converters for {@link Attachment}
* to a {@link DataHandler}
*
* @version
*/
@Converter
public final class AttachmentConverter {

/**
* Utility classes should not have a public constructor.
*/
private AttachmentConverter() {
}

@Converter
public static DataHandler toDataHandler(final Attachment attachment) {
return attachment.getDataHandler();
}
}

0 comments on commit 50d6d4f

Please sign in to comment.