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.components.util;
018
019 import java.io.IOException;
020 import java.util.Iterator;
021
022 import javax.activation.DataHandler;
023 import javax.jbi.messaging.MessageExchange;
024 import javax.jbi.messaging.MessagingException;
025 import javax.jbi.messaging.NormalizedMessage;
026 import javax.xml.parsers.ParserConfigurationException;
027 import javax.xml.transform.Source;
028 import javax.xml.transform.TransformerException;
029 import javax.xml.transform.sax.SAXSource;
030 import javax.xml.transform.stream.StreamSource;
031
032 import org.xml.sax.SAXException;
033
034 import org.apache.servicemix.jbi.helper.MessageUtil;
035 import org.apache.servicemix.jbi.jaxp.BytesSource;
036 import org.apache.servicemix.jbi.jaxp.ResourceSource;
037 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
038 import org.apache.servicemix.jbi.jaxp.StringSource;
039 import org.apache.servicemix.jbi.messaging.PojoMarshaler;
040
041 /**
042 * A simple transformer which copies the properties and content from the source message to the destination message.
043 *
044 * @version $Revision: 690205 $
045 * @deprecated
046 */
047 public class CopyTransformer implements MessageTransformer {
048
049 private static final CopyTransformer INSTANCE = new CopyTransformer();
050
051 private SourceTransformer sourceTransformer = new SourceTransformer();
052
053 private boolean copyProperties = true;
054
055 private boolean copyAttachments = true;
056
057 private boolean copySecuritySubject = true;
058
059 /**
060 * @return the copyAttachments
061 */
062 public boolean isCopyAttachments() {
063 return copyAttachments;
064 }
065
066 /**
067 * @param copyAttachments the copyAttachments to set
068 */
069 public void setCopyAttachments(boolean copyAttachments) {
070 this.copyAttachments = copyAttachments;
071 }
072
073 /**
074 * @return the copyProperties
075 */
076 public boolean isCopyProperties() {
077 return copyProperties;
078 }
079
080 /**
081 * @param copyProperties the copyProperties to set
082 */
083 public void setCopyProperties(boolean copyProperties) {
084 this.copyProperties = copyProperties;
085 }
086
087 /**
088 * @return the copySecuritySubject
089 */
090 public boolean isCopySecuritySubject() {
091 return copySecuritySubject;
092 }
093
094 /**
095 * @param copySecuritySubject the copySecuritySubject to set
096 */
097 public void setCopySecuritySubject(boolean copySecuritySubject) {
098 this.copySecuritySubject = copySecuritySubject;
099 }
100
101 /**
102 * Returns the singleton instance
103 *
104 * @return the singleton instance
105 */
106 public static CopyTransformer getInstance() {
107 return INSTANCE;
108 }
109
110 public boolean transform(MessageExchange exchange, NormalizedMessage from,
111 NormalizedMessage to) throws MessagingException {
112 if (copyProperties) {
113 copyProperties(from, to);
114 }
115
116 Source content = from.getContent();
117 if ((content instanceof StreamSource || content instanceof SAXSource)
118 && !(content instanceof StringSource)
119 && !(content instanceof BytesSource)
120 && !(content instanceof ResourceSource)) {
121 // lets avoid stream open exceptions by using a temporary format
122 try {
123 content = sourceTransformer.toDOMSource(from);
124 } catch (TransformerException e) {
125 throw new MessagingException(e);
126 } catch (ParserConfigurationException e) {
127 throw new MessagingException(e);
128 } catch (IOException e) {
129 throw new MessagingException(e);
130 } catch (SAXException e) {
131 throw new MessagingException(e);
132 }
133 }
134 to.setContent(content);
135
136 if (copyAttachments) {
137 copyAttachments(from, to);
138 }
139
140 if (copySecuritySubject) {
141 copySecuritySubject(from, to);
142 }
143
144 return true;
145 }
146
147 public NormalizedMessage transform(MessageExchange exchange, NormalizedMessage in)
148 throws MessagingException {
149 NormalizedMessage out = new MessageUtil.NormalizedMessageImpl();
150 transform(exchange, in, out);
151 return out;
152 }
153
154 /**
155 * Copies all of the properties from one message to another
156 *
157 * @param from the message containing the properties
158 * @param to the destination messages where the properties are set
159 */
160 public static void copyProperties(NormalizedMessage from,
161 NormalizedMessage to) {
162 for (Iterator iter = from.getPropertyNames().iterator(); iter.hasNext();) {
163 String name = (String) iter.next();
164 // Do not copy transient properties
165 if (!PojoMarshaler.BODY.equals(name)) {
166 Object value = from.getProperty(name);
167 to.setProperty(name, value);
168 }
169 }
170 }
171
172 /**
173 * Copies the attachments from a message to another message
174 *
175 * @param from the message with the attachments
176 * @param to the message to which attachments are added
177 * @throws MessagingException if an attachment could not be added
178 */
179 public static void copyAttachments(NormalizedMessage from,
180 NormalizedMessage to) throws MessagingException {
181 for (Iterator iter = from.getAttachmentNames().iterator(); iter
182 .hasNext();) {
183 String name = (String) iter.next();
184 DataHandler value = from.getAttachment(name);
185 to.addAttachment(name, value);
186 }
187 }
188
189 /**
190 * Copies the subject from a message to another message
191 *
192 * @param from the message with the subject
193 * @param to the message to which the subject is added
194 * @throws MessagingException if an attachment could not be added
195 */
196 public static void copySecuritySubject(NormalizedMessage from,
197 NormalizedMessage to) throws MessagingException {
198 to.setSecuritySubject(from.getSecuritySubject());
199 }
200
201 }