diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index f33bc9341a..ab2b07d7af 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -73,15 +73,19 @@ public class FileWritingMessageHandler implements MessageHandler { Assert.notNull(payload, "message payload must not be null"); File file = new File(parentDirectory, this.fileNameGenerator.generateFileName(message)); try { - if (payload instanceof byte[]) { - FileCopyUtils.copy((byte[]) payload, file); - } - else if (payload instanceof File) { + if (payload instanceof File) { FileCopyUtils.copy((File) payload, file); } - else { + else if (payload instanceof byte[]) { + FileCopyUtils.copy((byte[]) payload, file); + } + else if (payload instanceof String) { OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), this.charset); - FileCopyUtils.copy(payload.toString(), writer); + FileCopyUtils.copy((String) payload, writer); + } + else { + throw new IllegalArgumentException( + "unsupported Message payload type [" + payload.getClass().getName() + "]"); } } catch (Exception e) { diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java new file mode 100644 index 0000000000..5c0ca009e6 --- /dev/null +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.file; + +import java.io.File; + +import org.junit.Test; + +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessageHandlingException; + +/** + * @author Mark Fisher + */ +public class FileWritingMessageHandlerTests { + + @Test(expected = MessageHandlingException.class) + public void unsupportedType() { + FileWritingMessageHandler handler = new FileWritingMessageHandler( + new File(System.getProperty("java.io.tmpdir"))); + handler.handleMessage(new GenericMessage(99)); + } + +}