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.wsn.client;
018    
019    import java.io.StringWriter;
020    
021    import javax.jbi.messaging.MessageExchange;
022    import javax.jbi.messaging.MessagingException;
023    import javax.jbi.messaging.NormalizedMessage;
024    import javax.xml.bind.JAXBContext;
025    import javax.xml.transform.Source;
026    
027    import org.apache.servicemix.jbi.jaxp.StringSource;
028    import org.apache.servicemix.jbi.messaging.DefaultMarshaler;
029    
030    public class JAXBMarshaler extends DefaultMarshaler {
031    
032        private JAXBContext context;
033    
034        public JAXBMarshaler(JAXBContext context) {
035            this.context = context;
036        }
037    
038        public JAXBContext getContext() {
039            return context;
040        }
041    
042        public void setContext(JAXBContext context) {
043            this.context = context;
044        }
045    
046        protected Object defaultUnmarshal(MessageExchange exchange, NormalizedMessage message) {
047            try {
048                Source content = message.getContent();
049                return context.createUnmarshaller().unmarshal(content);
050            } catch (Exception e) {
051                throw new RuntimeException(e);
052            }
053        }
054    
055        protected Source asContent(NormalizedMessage message, Object body) {
056            try {
057                StringWriter writer = new StringWriter();
058                context.createMarshaller().marshal(body, writer);
059                return new StringSource(writer.toString());
060            } catch (Exception e) {
061                throw new RuntimeException(e);
062            }
063        }
064    
065        @Override
066        public void marshal(MessageExchange exchange, NormalizedMessage message, Object body) throws MessagingException {
067            if (body instanceof Source) {
068                message.setContent((Source) body);
069            } else {
070                Source content = asContent(message, body);
071                message.setContent(content);
072            }
073        }
074    }