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 javax.jbi.messaging.MessagingException;
020 import javax.jbi.messaging.NormalizedMessage;
021 import javax.xml.transform.Source;
022
023 import org.apache.servicemix.JavaSource;
024 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl;
025 import org.apache.servicemix.jbi.messaging.PojoMarshaler;
026
027 /**
028 * Some helper methods for working with messages
029 *
030 * @version $Revision: $
031 * @deprecated
032 */
033 public final class MessageHelper {
034
035 private MessageHelper() {
036 }
037
038 /**
039 * A helper method to return the body of the message as a POJO which could be a
040 * bean or some DOMish model of the body.
041 *
042 * @param message the message on which to extract the body
043 * @return the body of the message as a POJO or DOM object
044 * @throws javax.jbi.messaging.MessagingException
045 *
046 */
047 public static Object getBody(NormalizedMessage message) throws MessagingException {
048 Source content = message.getContent();
049 if (content instanceof JavaSource) {
050 JavaSource source = (JavaSource) content;
051 return source.getObject();
052 }
053 if (message instanceof NormalizedMessageImpl) {
054 return ((NormalizedMessageImpl) message).getBody();
055 }
056 return message.getProperty(PojoMarshaler.BODY);
057 }
058
059 /**
060 * A helper method to return the body of the message as a POJO which could be a
061 * bean or some DOMish model of the body.
062 *
063 * @param message the message on which to extract the body
064 * @param marshaller the marshaller used to map from the XML representation to the POJO
065 * @return the body of the message as a POJO or DOM object
066 * @throws javax.jbi.messaging.MessagingException
067 *
068 */
069 public static Object getBody(NormalizedMessage message, PojoMarshaler marshaller) throws MessagingException {
070 Source content = message.getContent();
071 if (content instanceof JavaSource) {
072 JavaSource source = (JavaSource) content;
073 return source.getObject();
074 }
075 if (message instanceof NormalizedMessageImpl) {
076 return ((NormalizedMessageImpl) message).getBody(marshaller);
077 }
078 return message.getProperty(PojoMarshaler.BODY);
079 }
080
081 /**
082 * Sets the body of the message as a POJO
083 *
084 * @param message the message on which to set the body
085 * @param body the POJO or DOMish model to set
086 * @throws MessagingException
087 */
088 public static void setBody(NormalizedMessage message, Object body) throws MessagingException {
089 Source content = message.getContent();
090 if (content instanceof JavaSource) {
091 JavaSource source = (JavaSource) content;
092 source.setObject(body);
093 } else if (message instanceof NormalizedMessageImpl) {
094 ((NormalizedMessageImpl) message).setBody(body);
095 } else {
096 message.setProperty(PojoMarshaler.BODY, body);
097 }
098 }
099
100 }