5
5
package akka .camel
6
6
7
7
import java .util .{ Map ⇒ JMap , Set ⇒ JSet }
8
+ import javax .activation .DataHandler
8
9
import org .apache .camel .{ CamelContext , Message ⇒ JCamelMessage , StreamCache }
9
10
import akka .AkkaException
10
11
import scala .reflect .ClassTag
12
+ import scala .runtime .ScalaRunTime
11
13
import scala .util .Try
12
14
import scala .collection .JavaConversions ._
13
15
import akka .dispatch .Mapper
14
16
15
17
/**
16
18
* An immutable representation of a Camel message.
17
19
*/
18
- case class CamelMessage (body : Any , headers : Map [String , Any ]) {
19
- def this (body : Any , headers : JMap [String , Any ]) = this (body, headers.toMap) // for Java
20
+ class CamelMessage (val body : Any , val headers : Map [String , Any ], val attachments : Map [String , DataHandler ]) extends Serializable with Product {
21
+ def this (body : Any , headers : JMap [String , Any ]) = this (body, headers.toMap, Map .empty[String , DataHandler ]) // Java
22
+ def this (body : Any , headers : JMap [String , Any ], attachments : JMap [String , DataHandler ]) = this (body, headers.toMap, attachments.toMap) // Java
23
+ def this (body : Any , headers : Map [String , Any ]) = this (body, headers.toMap, Map .empty[String , DataHandler ])
20
24
21
- override def toString : String = " CamelMessage(%s, %s)" format (body, headers)
25
+ def copy (body : Any = this .body, headers : Map [String , Any ] = this .headers): CamelMessage = CamelMessage (body, headers, this .attachments)
26
+
27
+ override def toString : String = " CamelMessage(%s, %s, %s)" format (body, headers, attachments)
22
28
23
29
/**
24
30
* Returns those headers from this message whose name is contained in <code>names</code>.
@@ -112,7 +118,7 @@ case class CamelMessage(body: Any, headers: Map[String, Any]) {
112
118
/**
113
119
* Java API: Returns a new CamelMessage with a new body, while keeping the same headers.
114
120
*/
115
- def withBody [T ](body : T ) = this . copy(body = body)
121
+ def withBody [T ](body : T ): CamelMessage = copy(body = body)
116
122
/**
117
123
* Creates a CamelMessage with current <code>body</code> converted to type <code>T</code>.
118
124
* The CamelContext is accessible in a [[akka.camel.javaapi.UntypedConsumerActor ]] and [[akka.camel.javaapi.UntypedProducerActor ]]
@@ -128,12 +134,95 @@ case class CamelMessage(body: Any, headers: Map[String, Any]) {
128
134
*/
129
135
def withBodyAs [T ](clazz : Class [T ])(implicit camelContext : CamelContext ): CamelMessage = copy(body = getBodyAs(clazz, camelContext))
130
136
137
+ /**
138
+ * Returns those attachments from this message whose name is contained in <code>names</code>.
139
+ */
140
+ def attachments (names : Set [String ]): Map [String , DataHandler ] = attachments filterKeys names
141
+
142
+ /**
143
+ * Java API: Returns those attachments from this message whose name is contained in <code>names</code>.
144
+ * The returned headers map is backed up by an immutable headers map. Any attempt to modify
145
+ * the returned map will throw an exception.
146
+ */
147
+ def getAttachments (names : JSet [String ]): JMap [String , DataHandler ] = attachments(names.toSet)
148
+
149
+ /**
150
+ * Java API: Returns all attachments from this message. The returned attachments map is backed up by this
151
+ * message's immutable headers map. Any attempt to modify the returned map will throw an
152
+ * exception.
153
+ */
154
+ def getAttachments : JMap [String , DataHandler ] = attachments
155
+
156
+ /**
157
+ * Java API: Creates a new CamelMessage with given <code>attachments</code>. A copy of the attachments map is made.
158
+ */
159
+ def withAttachments (attachments : JMap [String , DataHandler ]): CamelMessage = CamelMessage (this .body, this .headers, attachments.toMap)
160
+
161
+ /**
162
+ * SCALA API: Creates a new CamelMessage with given <code>attachments</code>.
163
+ */
164
+ def withAttachments (attachments : Map [String , DataHandler ]): CamelMessage = CamelMessage (this .body, this .headers, attachments)
165
+
166
+ /**
167
+ * Indicates whether some other object is "equal to" this one.
168
+ */
169
+ override def equals (that : Any ): Boolean =
170
+ that match {
171
+ case that : CamelMessage if canEqual(that) ⇒
172
+ this .body == that.body &&
173
+ this .headers == that.headers &&
174
+ this .attachments == that.attachments
175
+ case _ ⇒ false
176
+ }
177
+
178
+ /**
179
+ * Returns a hash code value for the object.
180
+ */
181
+ override def hashCode (): Int = ScalaRunTime ._hashCode(this )
182
+
183
+ /**
184
+ * Returns the n-th element of this product, 0-based.
185
+ */
186
+ override def productElement (n : Int ): Any = n match {
187
+ case 0 ⇒ body
188
+ case 1 ⇒ headers
189
+ case 2 ⇒ attachments
190
+ }
191
+
192
+ /**
193
+ * Returns the size of this product.
194
+ */
195
+ override def productArity : Int = 3
196
+
197
+ /**
198
+ * Indicates if some other object can be compared (based on type).
199
+ * This method should be called from every well-designed equals method that is open to be overridden in a subclass.
200
+ */
201
+ override def canEqual (that : Any ): Boolean = that match {
202
+ case _ : CamelMessage ⇒ true
203
+ case _ ⇒ false
204
+ }
131
205
}
132
206
133
207
/**
134
208
* Companion object of CamelMessage class.
135
209
*/
136
- object CamelMessage {
210
+ object CamelMessage extends ((Any , Map [String , Any ]) ⇒ CamelMessage ) {
211
+
212
+ /**
213
+ * Returns a new CamelMessage based on the <code>body</code> and <code>headers</code>.
214
+ */
215
+ def apply (body : Any , headers : Map [String , Any ]): CamelMessage = new CamelMessage (body, headers, Map .empty[String , DataHandler ])
216
+
217
+ /**
218
+ * Returns a new CamelMessage based on the <code>body</code>, <code>headers</code> and <code>attachments</code>.
219
+ */
220
+ def apply (body : Any , headers : Map [String , Any ], attachments : Map [String , DataHandler ]): CamelMessage = new CamelMessage (body, headers, attachments)
221
+
222
+ /**
223
+ * Returns <code>Some(body, headers)</code>.
224
+ */
225
+ def unapply (camelMessage : CamelMessage ): Option [(Any , Map [String , Any ])] = Some ((camelMessage.body, camelMessage.headers))
137
226
138
227
/**
139
228
* CamelMessage header to correlate request with response messages. Applications that send
@@ -150,7 +239,7 @@ object CamelMessage {
150
239
*/
151
240
private [camel] def canonicalize (msg : Any ) = msg match {
152
241
case mobj : CamelMessage ⇒ mobj
153
- case body ⇒ CamelMessage (body, Map .empty)
242
+ case body ⇒ CamelMessage (body, Map .empty[ String , Any ] )
154
243
}
155
244
156
245
/**
@@ -159,15 +248,28 @@ object CamelMessage {
159
248
* @param headers additional headers to set on the created CamelMessage in addition to those
160
249
* in the Camel message.
161
250
*/
162
- private [camel] def from (camelMessage : JCamelMessage , headers : Map [String , Any ]): CamelMessage = CamelMessage (camelMessage.getBody, headers ++ camelMessage.getHeaders)
251
+ private [camel] def from (camelMessage : JCamelMessage , headers : Map [String , Any ]): CamelMessage =
252
+ CamelMessage (camelMessage.getBody, headers ++ camelMessage.getHeaders, camelMessage.getAttachments.toMap)
253
+
254
+ /**
255
+ * Creates a new CamelMessageWithAttachments object from the Camel message.
256
+ *
257
+ * @param headers additional headers to set on the created CamelMessageWithAttachments in addition to those
258
+ * in the Camel message.
259
+ * @param attachments additional attachments to set on the created CamelMessageWithAttachments in addition to those
260
+ * in the Camel message.
261
+ */
262
+ private [camel] def from (camelMessage : JCamelMessage , headers : Map [String , Any ], attachments : Map [String , DataHandler ]): CamelMessage =
263
+ CamelMessage (camelMessage.getBody, headers ++ camelMessage.getHeaders, attachments ++ camelMessage.getAttachments)
163
264
164
265
/**
165
266
* INTERNAL API
166
- * copies the content of this CamelMessage to an Apache Camel Message.
267
+ * copies the content of this CamelMessageWithAttachments to an Apache Camel Message.
167
268
*/
168
269
private [camel] def copyContent (from : CamelMessage , to : JCamelMessage ): Unit = {
169
270
to.setBody(from.body)
170
271
for ((name, value) ← from.headers) to.getHeaders.put(name, value.asInstanceOf [AnyRef ])
272
+ to.getAttachments.putAll(from.getAttachments)
171
273
}
172
274
}
173
275
0 commit comments