KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > util > BinaryFileMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.util;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import javax.activation.DataHandler JavaDoc;
25 import javax.activation.DataSource JavaDoc;
26 import javax.jbi.JBIException;
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.MessagingException;
29 import javax.jbi.messaging.NormalizedMessage;
30
31 import org.apache.servicemix.jbi.util.FileUtil;
32 import org.apache.servicemix.jbi.util.StreamDataSource;
33
34 /**
35  * A FileMarshaler that converts the given input stream into a binary attachment.
36  *
37  * @author Guillaume Nodet
38  * @since 3.0
39  */

40 public class BinaryFileMarshaler extends DefaultFileMarshaler {
41
42     private String JavaDoc attachment = "content";
43     private String JavaDoc contentType = null;
44
45     public String JavaDoc getAttachment() {
46         return attachment;
47     }
48
49     public void setAttachment(String JavaDoc attachment) {
50         this.attachment = attachment;
51     }
52     
53     public String JavaDoc getContentType() {
54         return contentType;
55     }
56
57     public void setContentType(String JavaDoc contentType) {
58         this.contentType = contentType;
59     }
60
61     public void readMessage(MessageExchange exchange, NormalizedMessage message, InputStream JavaDoc in, String JavaDoc path) throws IOException JavaDoc, JBIException {
62         DataSource JavaDoc ds = new StreamDataSource(in, contentType);
63         DataHandler JavaDoc handler = new DataHandler JavaDoc(ds);
64         message.addAttachment(attachment, handler);
65         message.setProperty(FILE_NAME_PROPERTY, new File JavaDoc(path).getName());
66         message.setProperty(FILE_PATH_PROPERTY, path);
67     }
68
69     public void writeMessage(MessageExchange exchange, NormalizedMessage message, OutputStream JavaDoc out, String JavaDoc path) throws IOException JavaDoc, JBIException {
70         DataHandler JavaDoc handler = message.getAttachment(attachment);
71         if (handler == null) {
72             throw new MessagingException("Could not find attachment: " + attachment);
73         }
74         InputStream JavaDoc is = handler.getInputStream();
75         FileUtil.copyInputStream(is, out);
76     }
77
78 }
79
Popular Tags