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:
19: /**
20: * User Map Operation used to store a file as an attachment in the target message.
21: * Source Arg 1: Absolute path of file to create an attachment from.
22: * Source Arg 2: (optional) Content ID to use for attachment. If not provided,
23: * the name of the file is used.
24: * Target Arg 1: The field in the target message to store the target content ID.
25: *
26: */
27: public class StoreAttachmentFromFile implements
28: ITransformationOperation {
29:
30: protected final transient Log logger = LogFactory
31: .getLog(getClass());
32:
33: private HashMap<String, DataHandler> sourceAttachmentMap;
34: private HashMap<String, DataHandler> targetAttachmentMap;
35:
36: public void initialize(Map<String, Object> transformContext)
37: throws Exception {
38: sourceAttachmentMap = (HashMap<String, DataHandler>) transformContext
39: .get("Source.AttachmentMap");
40: targetAttachmentMap = (HashMap<String, DataHandler>) transformContext
41: .get("Target.AttachmentMap");
42: }
43:
44: public void addProperty(String name, String value) {
45:
46: }
47:
48: public void cleanup(Map<String, Object> transformContext)
49: throws Exception {
50:
51: }
52:
53: public boolean process(String[] source, String[] target)
54: throws Exception {
55: if (source.length > 0) {
56: File srcFile = new File(source[0]);
57: FileDataSource fileDataSource = new FileDataSource(srcFile);
58: DataHandler dataHandler = new DataHandler(fileDataSource);
59: String destCID;
60: if (source.length == 2) {
61: destCID = source[1];
62: } else {
63: destCID = srcFile.getName();
64: }
65: targetAttachmentMap.put(destCID, dataHandler);
66: target[0] = destCID;
67: } else {
68: logger.error("No source arguments supplied.");
69: return false;
70: }
71:
72: return true;
73: }
74:
75: }
|