StoreAttachmentFromFile.java :  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » map » Java Open Source

Java Open Source » ESB » cbesb 1.2 
cbesb 1.2 » com » bostechcorp » cbesb » map » StoreAttachmentFromFile.java
package com.bostechcorp.cbesb.map;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bostechcorp.cbesb.runtime.ccsl.lib.ITransformationOperation;

/**
 * User Map Operation used to store a file as an attachment in the target message.
 * Source Arg 1: Absolute path of file to create an attachment from.
 * Source Arg 2: (optional) Content ID to use for attachment.  If not provided,
 * the name of the file is used.
 * Target Arg 1: The field in the target message to store the target content ID. 
 *
 */
public class StoreAttachmentFromFile implements ITransformationOperation {

  protected final transient Log logger = LogFactory.getLog(getClass());
  
  private HashMap<String, DataHandler> sourceAttachmentMap;
  private HashMap<String, DataHandler> targetAttachmentMap;
  
  public void initialize(Map<String, Object> transformContext) throws Exception {
    sourceAttachmentMap = (HashMap<String, DataHandler>)transformContext.get("Source.AttachmentMap");
    targetAttachmentMap = (HashMap<String, DataHandler>)transformContext.get("Target.AttachmentMap");
  }

  public void addProperty(String name, String value) {

  }

  public void cleanup(Map<String, Object> transformContext) throws Exception {

  }

  public boolean process(String[] source, String[] target) throws Exception {
    if (source.length > 0) {
      File srcFile = new File(source[0]);
      FileDataSource fileDataSource = new FileDataSource(srcFile);
          DataHandler dataHandler = new DataHandler(fileDataSource);
          String destCID;
          if (source.length == 2) {
            destCID = source[1];
          } else {
            destCID = srcFile.getName();
          }
      targetAttachmentMap.put(destCID, dataHandler);
      target[0] = destCID;
    } else {
      logger.error("No source arguments supplied.");
      return false;
    }

    return true;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.