KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > CopyTransformer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.util;
18
19 import org.apache.servicemix.jbi.jaxp.BytesSource;
20 import org.apache.servicemix.jbi.jaxp.ResourceSource;
21 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
22 import org.apache.servicemix.jbi.jaxp.StringSource;
23 import org.apache.servicemix.jbi.messaging.PojoMarshaler;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import javax.activation.DataHandler JavaDoc;
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.MessagingException;
29 import javax.jbi.messaging.NormalizedMessage;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.sax.SAXSource JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 /**
40  * A simple transformer which copies the properties and content from the source message to the destination message.
41  *
42  * @version $Revision: 440077 $
43  */

44 public class CopyTransformer implements MessageTransformer {
45
46     private static final CopyTransformer instance = new CopyTransformer();
47
48     private SourceTransformer sourceTransformer = new SourceTransformer();
49     
50     private boolean copyProperties = true;
51     private boolean copyAttachments = true;
52     private boolean copySecuritySubject = true;
53
54     /**
55      * @return the copyAttachments
56      */

57     public boolean isCopyAttachments() {
58         return copyAttachments;
59     }
60
61     /**
62      * @param copyAttachments the copyAttachments to set
63      */

64     public void setCopyAttachments(boolean copyAttachments) {
65         this.copyAttachments = copyAttachments;
66     }
67
68     /**
69      * @return the copyProperties
70      */

71     public boolean isCopyProperties() {
72         return copyProperties;
73     }
74
75     /**
76      * @param copyProperties the copyProperties to set
77      */

78     public void setCopyProperties(boolean copyProperties) {
79         this.copyProperties = copyProperties;
80     }
81
82     /**
83      * @return the copySecuritySubject
84      */

85     public boolean isCopySecuritySubject() {
86         return copySecuritySubject;
87     }
88
89     /**
90      * @param copySecuritySubject the copySecuritySubject to set
91      */

92     public void setCopySecuritySubject(boolean copySecuritySubject) {
93         this.copySecuritySubject = copySecuritySubject;
94     }
95
96     /**
97      * Returns the singleton instance
98      *
99      * @return the singleton instance
100      */

101     public static CopyTransformer getInstance() {
102         return instance;
103     }
104
105     public boolean transform(MessageExchange exchange, NormalizedMessage from, NormalizedMessage to) throws MessagingException {
106         if (copyProperties) {
107             copyProperties(from, to);
108         }
109
110         Source content = from.getContent();
111         if ((content instanceof StreamSource JavaDoc ||
112              content instanceof SAXSource JavaDoc) &&
113             !(content instanceof StringSource) &&
114             !(content instanceof BytesSource) &&
115             !(content instanceof ResourceSource)) {
116             // lets avoid stream open exceptions by using a temporary format
117
try {
118                 content = sourceTransformer.toDOMSource(from);
119             }
120             catch (TransformerException JavaDoc e) {
121                 throw new MessagingException(e);
122             }
123             catch (ParserConfigurationException JavaDoc e) {
124                 throw new MessagingException(e);
125             }
126             catch (IOException JavaDoc e) {
127                 throw new MessagingException(e);
128             }
129             catch (SAXException JavaDoc e) {
130                 throw new MessagingException(e);
131             }
132         }
133         to.setContent(content);
134         
135         if (copyAttachments) {
136             copyAttachments(from, to);
137         }
138         
139         if (copySecuritySubject) {
140             copySecuritySubject(from, to);
141         }
142         
143         return true;
144     }
145
146     /**
147      * Copies all of the properties from one message to another
148      *
149      * @param from the message containing the properties
150      * @param to the destination messages where the properties are set
151      */

152     public static void copyProperties(NormalizedMessage from, NormalizedMessage to) {
153         for (Iterator JavaDoc iter = from.getPropertyNames().iterator(); iter.hasNext();) {
154             String JavaDoc name = (String JavaDoc) iter.next();
155             // Do not copy transient properties
156
if (!SourceTransformer.CONTENT_DOCUMENT_PROPERTY.equals(name) &&
157                 !PojoMarshaler.BODY.equals(name))
158             {
159                 Object JavaDoc value = from.getProperty(name);
160                 to.setProperty(name, value);
161             }
162         }
163     }
164
165     /**
166      * Copies the attachments from a message to another message
167      *
168      * @param from the message with the attachments
169      * @param to the message to which attachments are added
170      * @throws MessagingException if an attachment could not be added
171      */

172     public static void copyAttachments(NormalizedMessage from, NormalizedMessage to) throws MessagingException {
173         for (Iterator JavaDoc iter = from.getAttachmentNames().iterator(); iter.hasNext();) {
174             String JavaDoc name = (String JavaDoc) iter.next();
175             DataHandler JavaDoc value = from.getAttachment(name);
176             to.addAttachment(name, value);
177         }
178     }
179
180     /**
181      * Copies the subject from a message to another message
182      *
183      * @param from the message with the subject
184      * @param to the message to which the subject is added
185      * @throws MessagingException if an attachment could not be added
186      */

187     public static void copySecuritySubject(NormalizedMessage from, NormalizedMessage to) throws MessagingException {
188         to.setSecuritySubject(from.getSecuritySubject());
189     }
190     
191 }
192
Popular Tags