View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.support;
18  
19  import java.io.IOException;
20  import javax.activation.DataHandler;
21  import javax.xml.transform.Source;
22  
23  import org.springframework.oxm.Marshaller;
24  import org.springframework.oxm.Unmarshaller;
25  import org.springframework.oxm.mime.MimeContainer;
26  import org.springframework.oxm.mime.MimeMarshaller;
27  import org.springframework.oxm.mime.MimeUnmarshaller;
28  import org.springframework.ws.WebServiceMessage;
29  import org.springframework.ws.mime.Attachment;
30  import org.springframework.ws.mime.MimeMessage;
31  
32  /**
33   * Helper class for endpoints and endpoint mappings that use marshalling.
34   *
35   * @author Arjen Poutsma
36   * @since 1.0.0
37   */
38  public abstract class MarshallingUtils {
39  
40      private MarshallingUtils() {
41      }
42  
43      /**
44       * Unmarshals the payload of the given message using the provided {@link Unmarshaller}.
45       * <p/>
46       * If the request message has no payload (i.e. {@link WebServiceMessage#getPayloadSource()} returns
47       * <code>null</code>), this method will return <code>null</code>.
48       *
49       * @param unmarshaller the unmarshaller
50       * @param message      the message of which the payload is to be unmarshalled
51       * @return the unmarshalled object
52       * @throws IOException in case of I/O errors
53       */
54      public static Object unmarshal(Unmarshaller unmarshaller, WebServiceMessage message) throws IOException {
55          Source payload = message.getPayloadSource();
56          if (payload == null) {
57              return null;
58          }
59          else if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) {
60              MimeUnmarshaller mimeUnmarshaller = (MimeUnmarshaller) unmarshaller;
61              MimeMessageContainer container = new MimeMessageContainer((MimeMessage) message);
62              return mimeUnmarshaller.unmarshal(payload, container);
63          }
64          else {
65              return unmarshaller.unmarshal(payload);
66          }
67      }
68  
69      /**
70       * Marshals the given object to the payload of the given message using the provided {@link Marshaller}.
71       *
72       * @param marshaller the marshaller
73       * @param graph      the root of the object graph to marshal
74       * @param message    the message of which the payload is to be unmarshalled
75       * @throws IOException in case of I/O errors
76       */
77      public static void marshal(Marshaller marshaller, Object graph, WebServiceMessage message) throws IOException {
78          if (marshaller instanceof MimeMarshaller && message instanceof MimeMessage) {
79              MimeMarshaller mimeMarshaller = (MimeMarshaller) marshaller;
80              MimeMessageContainer container = new MimeMessageContainer((MimeMessage) message);
81              mimeMarshaller.marshal(graph, message.getPayloadResult(), container);
82          }
83          else {
84              marshaller.marshal(graph, message.getPayloadResult());
85          }
86      }
87  
88      private static class MimeMessageContainer implements MimeContainer {
89  
90          private final MimeMessage mimeMessage;
91  
92          public MimeMessageContainer(MimeMessage mimeMessage) {
93              this.mimeMessage = mimeMessage;
94          }
95  
96          public boolean isXopPackage() {
97              return mimeMessage.isXopPackage();
98          }
99  
100         public boolean convertToXopPackage() {
101             return mimeMessage.convertToXopPackage();
102         }
103 
104         public void addAttachment(String contentId, DataHandler dataHandler) {
105             mimeMessage.addAttachment(contentId, dataHandler);
106         }
107 
108         public DataHandler getAttachment(String contentId) {
109             Attachment attachment = mimeMessage.getAttachment(contentId);
110             return attachment != null ? attachment.getDataHandler() : null;
111         }
112     }
113 
114 }