001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.util;
018    
019    import java.io.ByteArrayOutputStream;
020    import java.io.IOException;
021    
022    import javax.activation.DataHandler;
023    import javax.activation.DataSource;
024    import javax.jbi.messaging.MessagingException;
025    import javax.jbi.messaging.NormalizedMessage;
026    
027    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
028    import org.apache.servicemix.jbi.jaxp.StringSource;
029    
030    /**
031     * A thread-safe copier for NormalizedMessage onjects.
032     * 
033     * @author Martin Krasser
034     * @deprecated use {@link org.apache.servicemix.jbi.transformer.CopyTransformer} instead
035     */
036    public class MessageCopier {
037    
038        private boolean copySubject;
039        private boolean copyContent;
040        private boolean copyProperties;
041        private boolean copyAttachments;
042    
043        /**
044         * Creates a new message copier instance that creates full (deep) message
045         * copies.
046         */
047        public MessageCopier() {
048            this(true, true, true, true);
049        }
050        
051        /**
052         * Create a new message copier instance.
053         * 
054         * @param copySubject <code>true</code> if subject shall be copied.
055         * @param copyContent <code>true</code> if content shall be copied
056         * @param copyProperties <code>true</code> if properties shall be copied 
057         * @param copyAttachments <code>true</code> if attachments shall be copied
058         */
059        public MessageCopier(boolean copySubject, boolean copyContent, boolean copyProperties, boolean copyAttachments) {
060            super();
061            this.copySubject = copySubject;
062            this.copyContent = copyContent;
063            this.copyProperties = copyProperties;
064            this.copyAttachments = copyAttachments;
065        }
066    
067        /**
068         * Copies messages under consideration of the <code>copySubject</code>,
069         * <code>copyContent</code>, <code>copyProperties</code>,
070         * <code>copyAttachments</code> properties.
071         * 
072         * @param message original message.
073         * @return a copy of the original message.
074         * @throws MessagingException if a system-level exception occurs.
075         */
076        public NormalizedMessage copy(NormalizedMessage message) throws MessagingException {
077            NormalizedMessage copy = new MessageUtil.NormalizedMessageImpl();
078            if (copySubject) {
079                copySubject(message, copy);
080            }
081            if (copyContent) {
082                copyContent(message, copy);
083            }
084            if (copyProperties) {
085                copyProperties(message, copy);
086            }
087            if (copyAttachments) {
088                copyAttachments(message, copy);
089            }
090            return copy;
091        }
092        
093        public boolean isCopyAttachments() {
094            return copyAttachments;
095        }
096    
097        public boolean isCopyContent() {
098            return copyContent;
099        }
100    
101        public boolean isCopyProperties() {
102            return copyProperties;
103        }
104    
105        public boolean isCopySubject() {
106            return copySubject;
107        }
108    
109        private static void copySubject(NormalizedMessage from, NormalizedMessage to) {
110            to.setSecuritySubject(from.getSecuritySubject());
111        }
112        
113        private static void copyContent(NormalizedMessage from, NormalizedMessage to) throws MessagingException {
114            String str = null; 
115            try {
116                str = new SourceTransformer().toString(from.getContent());
117            } catch (Exception e) {
118                throw new MessagingException(e);
119            }
120            if (str != null) {
121                to.setContent(new StringSource(str));
122            }
123        }
124        
125        private static void copyProperties(NormalizedMessage from, NormalizedMessage to) {
126            for (Object name : from.getPropertyNames()) {
127                to.setProperty((String)name, from.getProperty((String)name));
128            }
129        }
130        
131        private static void copyAttachments(NormalizedMessage from, NormalizedMessage to) throws MessagingException {
132            for (Object name : from.getAttachmentNames()) {
133                DataHandler handler = from.getAttachment((String)name);
134                DataSource source = handler.getDataSource();
135                if (!(source instanceof ByteArrayDataSource)) {
136                    DataSource copy = copyDataSource(source);
137                    handler = new DataHandler(copy);
138                }
139                to.addAttachment((String)name, handler);
140            }
141        }
142        
143        private static DataSource copyDataSource(DataSource source) throws MessagingException {
144            try {
145                ByteArrayOutputStream baos = new ByteArrayOutputStream();
146                FileUtil.copyInputStream(source.getInputStream(), baos);
147                ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), source.getContentType());
148                bads.setName(source.getName());
149                return bads;
150            } catch (IOException e) {
151                throw new MessagingException(e);
152            }
153        }
154        
155    }