01: package com.bostechcorp.cbesb.map;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.File;
05: import java.io.FileOutputStream;
06: import java.io.IOException;
07: import java.io.InputStream;
08: import java.util.HashMap;
09: import java.util.Map;
10:
11: import javax.activation.DataHandler;
12:
13: import org.apache.commons.logging.Log;
14: import org.apache.commons.logging.LogFactory;
15:
16: import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;
17:
18: /**
19: * User Map Operation used to save an attachment from the source message
20: * as a string. Note that the actual attachment content should only
21: * contain character data for this to work properly.
22: * Source Arg 1: source attachment content ID. This is usually supplied in a field
23: * from the source message.
24: * Target Arg 1: Target field to store the content of the attachment
25: *
26: */
27: public class SaveAttachmentToString 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 > 0 && source[0] != null) {
55: //Get Content ID of attachment from first arg
56: DataHandler dh = sourceAttachmentMap.get(source[0]);
57: if (dh == null) {
58: logger.error("No attachment was found for Content ID: "
59: + source[0]);
60: return false;
61: }
62: ByteArrayOutputStream baos = new ByteArrayOutputStream();
63: InputStream is = dh.getInputStream();
64: int c = is.read();
65: while (c > -1) {
66: baos.write(c);
67: c = is.read();
68: }
69: target[0] = baos.toString();
70:
71: } else {
72: logger.error("No source arguments supplied.");
73: return false;
74: }
75: return true;
76: }
77:
78: }
|