001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jbi;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.impl.DefaultExchange;
023    
024    import javax.jbi.messaging.MessageExchange;
025    import javax.jbi.messaging.NormalizedMessage;
026    
027    /**
028     * An {@link Exchange} working with JBI which exposes the underlying JBI features such as the
029     * JBI {@link #getMessageExchange()}, {@link #getInMessage()} and {@link #getOutMessage()} 
030     *
031     * @version $Revision: 550760 $
032     */
033    public class JbiExchange extends DefaultExchange {
034        private final JbiBinding binding;
035        private MessageExchange messageExchange;
036    
037        public JbiExchange(CamelContext context, JbiBinding binding) {
038            super(context);
039            this.binding = binding;
040        }
041    
042        public JbiExchange(CamelContext context, JbiBinding binding, MessageExchange messageExchange) {
043            super(context);
044            this.binding = binding;
045            this.messageExchange = messageExchange;
046    
047            // TODO we could maybe use the typesafe APIs of different derived APIs from JBI 
048            setIn(new JbiMessage(messageExchange.getMessage("in")));
049            setOut(new JbiMessage(messageExchange.getMessage("out")));
050            setFault(new JbiMessage(messageExchange.getMessage("fault")));
051        }
052    
053        @Override
054        public JbiMessage getIn() {
055            return (JbiMessage) super.getIn();
056        }
057    
058        @Override
059        public JbiMessage getOut() {
060            return (JbiMessage) super.getOut();
061        }
062    
063        @Override
064        public JbiMessage getOut(boolean lazyCreate) {
065            return (JbiMessage) super.getOut(lazyCreate);
066        }
067    
068        @Override
069        public JbiMessage getFault() {
070            return (JbiMessage) super.getFault();
071        }
072    
073        /**
074         * @return the Camel <-> JBI binding
075         */
076        public JbiBinding getBinding() {
077            return binding;
078        }
079    
080        // Expose JBI features
081        //-------------------------------------------------------------------------
082    
083        /**
084         * Returns the underlying JBI message exchange for an inbound exchange
085         * or null for outbound messages
086         *
087         * @return the inbound message exchange
088         */
089        public MessageExchange getMessageExchange() {
090            return messageExchange;
091        }
092    
093        /**
094         * Returns the underlying In {@link NormalizedMessage}
095         *
096         * @return the In message
097         */
098        public NormalizedMessage getInMessage() {
099            return getIn().getNormalizedMessage();
100        }
101    
102        /**
103         * Returns the underlying Out {@link NormalizedMessage}
104         *
105         * @return the Out message
106         */
107        public NormalizedMessage getOutMessage() {
108            return getOut().getNormalizedMessage();
109        }
110    
111        /**
112         * Returns the underlying Fault {@link NormalizedMessage}
113         *
114         * @return the Fault message
115         */
116        public NormalizedMessage getFaultMessage() {
117            return getFault().getNormalizedMessage();
118        }
119    
120        // Implementation methods
121        //-------------------------------------------------------------------------
122    
123        @Override
124        protected JbiMessage createInMessage() {
125            return new JbiMessage();
126        }
127    
128        @Override
129        protected JbiMessage createOutMessage() {
130            return new JbiMessage();
131        }
132    }