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.mime;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import javax.activation.DataHandler;
024: import javax.activation.DataSource;
025: import javax.activation.FileDataSource;
026:
027: import org.springframework.core.io.InputStreamSource;
028: import org.springframework.core.io.Resource;
029: import org.springframework.util.Assert;
030:
031: /**
032: * Abstract implementation of the {@link MimeMessage} interface. Contains convenient default implementations.
033: *
034: * @author Arjen Poutsma
035: * @since 1.0.0
036: */
037: public abstract class AbstractMimeMessage implements MimeMessage {
038:
039: public final Attachment addAttachment(String contentId, File file)
040: throws AttachmentException {
041: Assert.hasLength(contentId, "contentId must not be empty");
042: Assert.notNull(file, "File must not be null");
043: DataHandler dataHandler = new DataHandler(new FileDataSource(
044: file));
045: return addAttachment(contentId, dataHandler);
046: }
047:
048: public final Attachment addAttachment(String contentId,
049: InputStreamSource inputStreamSource, String contentType) {
050: Assert.hasLength(contentId, "contentId must not be empty");
051: Assert.notNull(inputStreamSource,
052: "InputStreamSource must not be null");
053: if (inputStreamSource instanceof Resource
054: && ((Resource) inputStreamSource).isOpen()) {
055: throw new IllegalArgumentException(
056: "Passed-in Resource contains an open stream: invalid argument. "
057: + "MIME requires an InputStreamSource that creates a fresh stream for every call.");
058: }
059: DataHandler dataHandler = new DataHandler(
060: new InputStreamSourceDataSource(inputStreamSource,
061: contentType));
062: return addAttachment(contentId, dataHandler);
063: }
064:
065: /**
066: * Activation framework <code>DataSource</code> that wraps a Spring <code>InputStreamSource</code>.
067: *
068: * @author Arjen Poutsma
069: * @since 1.0.0
070: */
071: private static class InputStreamSourceDataSource implements
072: DataSource {
073:
074: private final InputStreamSource inputStreamSource;
075:
076: private final String contentType;
077:
078: public InputStreamSourceDataSource(
079: InputStreamSource inputStreamSource, String contentType) {
080: this .inputStreamSource = inputStreamSource;
081: this .contentType = contentType;
082: }
083:
084: public InputStream getInputStream() throws IOException {
085: return inputStreamSource.getInputStream();
086: }
087:
088: public OutputStream getOutputStream() {
089: throw new UnsupportedOperationException(
090: "Read-only javax.activation.DataSource");
091: }
092:
093: public String getContentType() {
094: return contentType;
095: }
096:
097: public String getName() {
098: if (inputStreamSource instanceof Resource) {
099: Resource resource = (Resource) inputStreamSource;
100: return resource.getFilename();
101: } else {
102: throw new UnsupportedOperationException(
103: "DataSource name not available");
104: }
105: }
106:
107: }
108:
109: }
|