001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.support;
018:
019: import java.io.IOException;
020: import javax.activation.DataHandler;
021:
022: import org.springframework.oxm.Marshaller;
023: import org.springframework.oxm.Unmarshaller;
024: import org.springframework.oxm.mime.MimeContainer;
025: import org.springframework.oxm.mime.MimeMarshaller;
026: import org.springframework.oxm.mime.MimeUnmarshaller;
027: import org.springframework.ws.WebServiceMessage;
028: import org.springframework.ws.mime.Attachment;
029: import org.springframework.ws.mime.MimeMessage;
030:
031: /**
032: * Helper class for endpoints and endpoint mappings that use marshalling.
033: *
034: * @author Arjen Poutsma
035: * @since 1.0.0
036: */
037: public abstract class MarshallingUtils {
038:
039: private MarshallingUtils() {
040: }
041:
042: /**
043: * Unmarshals the payload of the given message using the provided {@link Unmarshaller}.
044: *
045: * @param unmarshaller the unmarshaller
046: * @param message the message of which the payload is to be unmarshalled
047: * @return the unmarshalled object
048: * @throws IOException in case of I/O errors
049: */
050: public static Object unmarshal(Unmarshaller unmarshaller,
051: WebServiceMessage message) throws IOException {
052: if (unmarshaller instanceof MimeUnmarshaller
053: && message instanceof MimeMessage) {
054: MimeUnmarshaller mimeUnmarshaller = (MimeUnmarshaller) unmarshaller;
055: MimeMessageContainer container = new MimeMessageContainer(
056: (MimeMessage) message);
057: return mimeUnmarshaller.unmarshal(message
058: .getPayloadSource(), container);
059: } else {
060: return unmarshaller.unmarshal(message.getPayloadSource());
061: }
062: }
063:
064: /**
065: * Marshals the given object to the payload of the given message using the provided {@link Marshaller}.
066: *
067: * @param marshaller the marshaller
068: * @param graph the root of the object graph to marshal
069: * @param message the message of which the payload is to be unmarshalled
070: * @throws IOException in case of I/O errors
071: */
072: public static void marshal(Marshaller marshaller, Object graph,
073: WebServiceMessage message) throws IOException {
074: if (marshaller instanceof MimeMarshaller
075: && message instanceof MimeMessage) {
076: MimeMarshaller mimeMarshaller = (MimeMarshaller) marshaller;
077: MimeMessageContainer container = new MimeMessageContainer(
078: (MimeMessage) message);
079: mimeMarshaller.marshal(graph, message.getPayloadResult(),
080: container);
081: } else {
082: marshaller.marshal(graph, message.getPayloadResult());
083: }
084: }
085:
086: private static class MimeMessageContainer implements MimeContainer {
087:
088: private final MimeMessage mimeMessage;
089:
090: public MimeMessageContainer(MimeMessage mimeMessage) {
091: this .mimeMessage = mimeMessage;
092: }
093:
094: public boolean isXopPackage() {
095: return mimeMessage.isXopPackage();
096: }
097:
098: public boolean convertToXopPackage() {
099: return mimeMessage.convertToXopPackage();
100: }
101:
102: public void addAttachment(String contentId,
103: DataHandler dataHandler) {
104: mimeMessage.addAttachment(contentId, dataHandler);
105: }
106:
107: public DataHandler getAttachment(String contentId) {
108: Attachment attachment = mimeMessage
109: .getAttachment(contentId);
110: return attachment != null ? attachment.getDataHandler()
111: : null;
112: }
113: }
114:
115: }
|