01: package com.bostechcorp.cbesb.map;
02:
03: import java.io.File;
04: import java.io.FileOutputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.util.HashMap;
08: import java.util.Map;
09: import java.util.Set;
10:
11: import javax.activation.DataHandler;
12: import javax.activation.FileDataSource;
13:
14: import org.apache.commons.logging.Log;
15: import org.apache.commons.logging.LogFactory;
16:
17: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;
18: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringDataSource;
19:
20: /**
21: * User Map Operation used to store a string value as an attachment in the
22: * target message.
23: * Source Arg 1: String content to use as an attachment
24: * Source Arg 2: The content ID to use for the attachment
25: * Source Arg 3: (optional) the MIME content type of the attachment. Defaults
26: * to "text/plain"
27: * Target Arg 1: The field in the target message to store the target content ID.
28: *
29: */
30: public class StoreAttachmentFromString implements
31: ITransformationOperation {
32:
33: protected final transient Log logger = LogFactory
34: .getLog(getClass());
35:
36: private HashMap<String, DataHandler> sourceAttachmentMap;
37: private HashMap<String, DataHandler> targetAttachmentMap;
38:
39: public void initialize(Map<String, Object> transformContext)
40: throws Exception {
41: sourceAttachmentMap = (HashMap<String, DataHandler>) transformContext
42: .get("Source.AttachmentMap");
43: targetAttachmentMap = (HashMap<String, DataHandler>) transformContext
44: .get("Target.AttachmentMap");
45: }
46:
47: public void addProperty(String name, String value) {
48:
49: }
50:
51: public void cleanup(Map<String, Object> transformContext)
52: throws Exception {
53:
54: }
55:
56: /**
57: * source[] must contain two entries.
58: * source[0] - String content to use as an attachment
59: * source[1] - The content ID to use for the attachment
60: */
61: public boolean process(String[] source, String[] target)
62: throws Exception {
63: if (source.length >= 2) {
64: String destCID;
65: destCID = source[1];
66: String contentType = "text/plain";
67: if (source.length > 2) {
68: contentType = source[2];
69: }
70: StringDataSource stringDataSource = new StringDataSource(
71: source[0], contentType);
72: DataHandler dataHandler = new DataHandler(stringDataSource);
73: targetAttachmentMap.put(destCID, dataHandler);
74: target[0] = destCID;
75: } else {
76: logger
77: .error("Incorrect number of arguments. Two source arguments are required.");
78: return false;
79: }
80:
81: return true;
82: }
83:
84: }
|