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:
10: import javax.activation.DataHandler;
11:
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14:
15: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;
16:
17: /**
18: * User Map Operation used to save an attachment from the source message to
19: * a file.
20: * Source Arg 1: source attachment content ID. This is usually supplied in a field
21: * from the source message.
22: * Source Arg 2: The absolute path/filename where to write the attachment.
23: * Target Arg 1: No target argument is used, but the Map Editor requires one,
24: * so just specify a bogus variable to make it happy.
25: *
26: */
27: public class SaveAttachmentToFile implements ITransformationOperation {
28:
29: protected final transient Log logger = LogFactory
30: .getLog(getClass());
31:
32: private HashMap<String, DataHandler> sourceAttachmentMap;
33: private HashMap<String, DataHandler> targetAttachmentMap;
34:
35: public void initialize(Map<String, Object> transformContext)
36: throws Exception {
37: sourceAttachmentMap = (HashMap<String, DataHandler>) transformContext
38: .get("Source.AttachmentMap");
39: targetAttachmentMap = (HashMap<String, DataHandler>) transformContext
40: .get("Target.AttachmentMap");
41: }
42:
43: public void addProperty(String name, String value) {
44:
45: }
46:
47: public void cleanup(Map<String, Object> transformContext)
48: throws Exception {
49:
50: }
51:
52: public boolean process(String[] source, String[] target)
53: throws Exception {
54: if (source.length >= 2) {
55: //Get Content ID of attachment from first arg
56: if (source[0] != null) {
57: DataHandler dh = sourceAttachmentMap.get(source[0]);
58: if (dh == null) {
59: logger
60: .error("No attachment was found for Content ID: "
61: + source[0]);
62: return false;
63: }
64: try {
65: File destFile = new File(source[1]);
66: FileOutputStream fos = new FileOutputStream(
67: destFile);
68: dh.writeTo(fos);
69: fos.close();
70: } catch (IOException e) {
71: logger
72: .error(
73: "Exception writing attachment to file: ",
74: e);
75: return false;
76: }
77: } else {
78: logger
79: .error("Source value is not an attachment content ID: "
80: + source[0]);
81: return false;
82: }
83: } else {
84: logger
85: .error("Incorrect arguments. Src1 = Attachment Content ID; Src2 = Destination filename.");
86: return false;
87: }
88: return true;
89: }
90:
91: }
|