All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectweb.petals.binding.filetransferbc.IOUtils Maven / Gradle / Ivy

/**
 * PETALS - PETALS Services Platform.
 * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -------------------------------------------------------------------------
 * $Id$
 * -------------------------------------------------------------------------
 */
package org.objectweb.petals.binding.filetransferbc;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import javax.activation.DataHandler;
import javax.jbi.messaging.NormalizedMessage;

/**
 * Some IO utilities for the file transfer BC
 *
 * @author chamerling - eBM WebSourcing
 *
 */
public class IOUtils {

    private static final String DATE_FORMAT = "ddMMyyHHmmssSSS";

    /**
     * Write all attachments to files. One file per attachement. The file will
     * have the name of the attachment.
     *
     * @param message
     *            the message to get attachment from
     * @param destDir
     *            the direcptry where the files will be written
     * @throws FileTransferBCException
     */
    public static void writeAttachmentsToFiles(NormalizedMessage message,
        File destDir) throws FileTransferBCException {

        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String date = sdf.format(new Date(System.currentTimeMillis()));
        Set attachIds = message.getAttachmentNames();

        for (Object name : attachIds) {
            DataHandler handler = message.getAttachment((String) name);
            FileOutputStream fos = null;

            try {
                if (!destDir.exists())
                    destDir.mkdirs();

                File outFile = new File(destDir.getAbsolutePath(),
                    (String) name);

                fos = new FileOutputStream(new File(outFile.getAbsoluteFile()
                    + "_" + date));

                handler.writeTo(fos);
                fos.flush();

            } catch (Exception e) {
                throw new FileTransferBCException("Error: writing attachment "
                    + handler.getName() + "  " + e.getMessage());
            } finally {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }

    /**
     * Write a string to a file
     *
     * @param content
     * @param destDir
     * @param fileName
     * @throws FileTransferBCException
     */
    public static File writeStringToFile(String content, File destDir,
        String fileName) throws FileTransferBCException {
        BufferedWriter out = null;
        File outFile = null;

        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        fileName = fileName + "_"
            + sdf.format(new Date(System.currentTimeMillis())) + ".xml";

        try {
            // create destination directory if it do not exists
            if (!destDir.exists()) {
                destDir.mkdirs();
            }

            // create and write file
            outFile = new File(destDir.getAbsolutePath(), fileName);
            out = new BufferedWriter(new FileWriter(outFile.getAbsolutePath()));
            out.write(content);

        } catch (IOException e) {
            throw new FileTransferBCException("Error creating new file "
                + fileName, e.getMessage());
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return outFile;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy