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.mime;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import javax.activation.DataHandler;
24  import javax.activation.DataSource;
25  import javax.activation.FileDataSource;
26  
27  import org.springframework.core.io.InputStreamSource;
28  import org.springframework.core.io.Resource;
29  import org.springframework.util.Assert;
30  
31  /**
32   * Abstract implementation of the {@link MimeMessage} interface. Contains convenient default implementations.
33   *
34   * @author Arjen Poutsma
35   * @since 1.0.0
36   */
37  public abstract class AbstractMimeMessage implements MimeMessage {
38  
39      public final Attachment addAttachment(String contentId, File file) throws AttachmentException {
40          Assert.hasLength(contentId, "contentId must not be empty");
41          Assert.notNull(file, "File must not be null");
42          DataHandler dataHandler = new DataHandler(new FileDataSource(file));
43          return addAttachment(contentId, dataHandler);
44      }
45  
46      public final Attachment addAttachment(String contentId, InputStreamSource inputStreamSource, String contentType) {
47          Assert.hasLength(contentId, "contentId must not be empty");
48          Assert.notNull(inputStreamSource, "InputStreamSource must not be null");
49          if (inputStreamSource instanceof Resource && ((Resource) inputStreamSource).isOpen()) {
50              throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. " +
51                      "MIME requires an InputStreamSource that creates a fresh stream for every call.");
52          }
53          DataHandler dataHandler = new DataHandler(new InputStreamSourceDataSource(inputStreamSource, contentType));
54          return addAttachment(contentId, dataHandler);
55      }
56  
57      /**
58       * Activation framework <code>DataSource</code> that wraps a Spring <code>InputStreamSource</code>.
59       *
60       * @author Arjen Poutsma
61       * @since 1.0.0
62       */
63      private static class InputStreamSourceDataSource implements DataSource {
64  
65          private final InputStreamSource inputStreamSource;
66  
67          private final String contentType;
68  
69          public InputStreamSourceDataSource(InputStreamSource inputStreamSource, String contentType) {
70              this.inputStreamSource = inputStreamSource;
71              this.contentType = contentType;
72          }
73  
74          public InputStream getInputStream() throws IOException {
75              return inputStreamSource.getInputStream();
76          }
77  
78          public OutputStream getOutputStream() {
79              throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
80          }
81  
82          public String getContentType() {
83              return contentType;
84          }
85  
86          public String getName() {
87              if (inputStreamSource instanceof Resource) {
88                  Resource resource = (Resource) inputStreamSource;
89                  return resource.getFilename();
90              }
91              else {
92                  throw new UnsupportedOperationException("DataSource name not available");
93              }
94          }
95  
96      }
97  
98  }