01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.jbi.messaging;
18:
19: import javax.jbi.messaging.MessageExchange;
20: import javax.jbi.messaging.MessagingException;
21: import javax.jbi.messaging.NormalizedMessage;
22: import javax.xml.transform.Source;
23: import javax.xml.transform.dom.DOMSource;
24:
25: import org.w3c.dom.Node;
26:
27: import org.apache.servicemix.jbi.jaxp.StringSource;
28:
29: /**
30: * Default implementation of {@link PojoMarshaler} which will pass through
31: * String objects as XML content, DOM objects or Stream objects, otherwise the
32: * payload is stored in a message property.
33: *
34: * @version $Revision: 564607 $
35: */
36: public class DefaultMarshaler implements PojoMarshaler {
37:
38: private PojoMarshaler parent;
39:
40: public DefaultMarshaler() {
41: }
42:
43: public DefaultMarshaler(PojoMarshaler parent) {
44: this .parent = parent;
45: }
46:
47: public PojoMarshaler getParent() {
48: return parent;
49: }
50:
51: public void marshal(MessageExchange exchange,
52: NormalizedMessage message, Object body)
53: throws MessagingException {
54: if (body instanceof Source) {
55: message.setContent((Source) body);
56: } else {
57: message.setProperty(BODY, body);
58: Source content = asContent(message, body);
59: message.setContent(content);
60: }
61: }
62:
63: public Object unmarshal(MessageExchange exchange,
64: NormalizedMessage message) throws MessagingException {
65: Object answer = message.getProperty(BODY);
66: if (answer == null) {
67: if (parent != null) {
68: answer = parent.unmarshal(exchange, message);
69: }
70: if (answer == null) {
71: answer = defaultUnmarshal(exchange, message);
72: }
73: }
74: return answer;
75: }
76:
77: protected Object defaultUnmarshal(MessageExchange exchange,
78: NormalizedMessage message) {
79: Source content = message.getContent();
80: if (content instanceof DOMSource) {
81: DOMSource source = (DOMSource) content;
82: return source.getNode();
83: }
84: return content;
85: }
86:
87: protected Source asContent(NormalizedMessage message, Object body) {
88: if (body instanceof Source) {
89: return (Source) body;
90: } else if (body instanceof String) {
91: // lets assume String is the XML to send
92: return new StringSource((String) body);
93: } else if (body instanceof Node) {
94: return new DOMSource((Node) body);
95: }
96: return null;
97: }
98: }
|