KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > NormalizedMessageUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id$
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.component.common.util;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.activation.DataHandler JavaDoc;
33 import javax.jbi.messaging.MessagingException;
34 import javax.jbi.messaging.NormalizedMessage;
35
36 /**
37  * Utilities for normalized message manipulation.
38  *
39  * @author Christophe HAMERLING - eBMWebSourcing
40  *
41  */

42 public class NormalizedMessageUtil {
43
44     /**
45      * Date format used to get unique file name
46      */

47     private static final String JavaDoc DATE_FORMAT = "ddMMyyHHmmssSSS";
48
49     /**
50      * Copy the attachments from source to destination
51      *
52      * @param from
53      * @param to
54      * @throws MessagingException
55      */

56     public static void copyAttachments(final NormalizedMessage from,
57         NormalizedMessage to) throws MessagingException {
58         Set JavaDoc attachmentIds = from.getAttachmentNames();
59         for (Object JavaDoc id : attachmentIds) {
60             String JavaDoc attachName = (String JavaDoc) id;
61             DataHandler JavaDoc dh = from.getAttachment(attachName);
62             to.addAttachment(attachName, dh);
63         }
64     }
65
66     /**
67      * Copy the properties from source to destination.
68      *
69      * @param from
70      * @param to
71      */

72     public static void copyProperties(final NormalizedMessage from,
73         NormalizedMessage to) {
74         Set JavaDoc propertiesNames = from.getPropertyNames();
75         for (Object JavaDoc propertyName : propertiesNames) {
76             String JavaDoc name = (String JavaDoc) propertyName;
77             Object JavaDoc value = from.getProperty(name);
78             to.setProperty(name, value);
79         }
80     }
81
82     /**
83      * Copy the content from source to destination.
84      *
85      * @param from
86      * @param to
87      */

88     public static void copyContent(final NormalizedMessage from,
89         NormalizedMessage to) throws MessagingException {
90         to.setContent(from.getContent());
91     }
92
93     /**
94      * Write the given attchment to a file. The file will have the same name as
95      * the attachment one.
96      *
97      * @param dh
98      * the attachement
99      * @param dest
100      * the destination file
101      */

102     public static void writeAttachmentToFile(final DataHandler JavaDoc handler,
103         final File JavaDoc destDir) {
104         FileOutputStream JavaDoc fos = null;
105
106         try {
107             if (!destDir.exists())
108                 destDir.mkdirs();
109
110             // output file
111
File JavaDoc outFile = new File JavaDoc(destDir.getAbsolutePath(), handler
112                 .getName());
113
114             // if file already exists, add a timestamp suffix
115
String JavaDoc ext = "";
116             if (outFile.exists()) {
117                 SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_FORMAT);
118                 ext = sdf.format(new Date JavaDoc(System.currentTimeMillis()));
119             }
120
121             fos = new FileOutputStream JavaDoc(
122                 new File JavaDoc(outFile.getAbsoluteFile() + ext));
123
124             handler.writeTo(fos);
125             fos.flush();
126
127         } catch (FileNotFoundException JavaDoc e) {
128         } catch (IOException JavaDoc e) {
129         } finally {
130             try {
131                 fos.close();
132             } catch (IOException JavaDoc e) {
133             }
134         }
135     }
136
137     /**
138      * Write all the attachments of the normalized message to files. The file
139      * name will be the same as the attachment name.
140      *
141      * @param nm
142      * @param outputDir
143      */

144     public static void writeAttachmentsToFiles(final NormalizedMessage nm,
145         final File JavaDoc destDir) {
146         
147         Set JavaDoc attachments = nm.getAttachmentNames();
148         for (Object JavaDoc attachmentId : attachments) {
149             writeAttachmentToFile(nm.getAttachment((String JavaDoc) attachmentId),
150                 destDir);
151         }
152     }
153
154 }
155
Popular Tags