KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.servicemix.expression.Expression;
20 import org.apache.servicemix.expression.PropertyExpression;
21 import org.apache.servicemix.jbi.NoMessageContentAvailableException;
22 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
23 import org.w3c.dom.Node JavaDoc;
24
25 import javax.jbi.JBIException;
26 import javax.jbi.messaging.MessageExchange;
27 import javax.jbi.messaging.MessagingException;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.stream.StreamResult JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.ObjectOutputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.OutputStreamWriter JavaDoc;
41
42 /**
43  * A default file transformer which assumes the file is already in XML format and
44  * requires no transformation other than to be wrapped in a normalized message..
45  *
46  * @version $Revision: 426415 $
47  */

48 public class DefaultFileMarshaler extends MarshalerSupport implements FileMarshaler {
49
50     public static final String JavaDoc FILE_NAME_PROPERTY = "org.apache.servicemix.file.name";
51     public static final String JavaDoc FILE_PATH_PROPERTY = "org.apache.servicemix.file.path";
52     public static final String JavaDoc FILE_CONTENT = "org.apache.servicemix.file.content";
53
54     protected static final PropertyExpression FILE_NAME_EXPRESSION = new PropertyExpression(FILE_NAME_PROPERTY);
55     protected static final PropertyExpression FILE_CONTENT_EXPRESSION = new PropertyExpression(FILE_CONTENT);
56
57     private Expression fileName = FILE_NAME_EXPRESSION;
58     private Expression content = FILE_CONTENT_EXPRESSION;
59
60     public void readMessage(MessageExchange exchange, NormalizedMessage message, InputStream JavaDoc in, String JavaDoc path) throws IOException JavaDoc, JBIException {
61         message.setContent(new StreamSource JavaDoc(in, path));
62         message.setProperty(FILE_NAME_PROPERTY, new File JavaDoc(path).getName());
63         message.setProperty(FILE_PATH_PROPERTY, path);
64     }
65
66     public String JavaDoc getOutputName(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
67         return asString(fileName.evaluate(exchange, message));
68     }
69
70     public void writeMessage(MessageExchange exchange, NormalizedMessage message, OutputStream JavaDoc out, String JavaDoc path) throws IOException JavaDoc, JBIException {
71         try {
72             Object JavaDoc value = content.evaluate(exchange, message);
73             if (value != null) {
74                 writeValue(value, out);
75             }
76             else {
77                 writeMessageContent(exchange, message, out, path);
78             }
79         }
80         catch (IOException JavaDoc e) {
81             throw new MessagingException(e);
82         }
83     }
84
85     // Properties
86
//-------------------------------------------------------------------------
87
public Expression getContent() {
88         return content;
89     }
90
91     public void setContent(Expression content) {
92         this.content = content;
93     }
94
95     public Expression getFileName() {
96         return fileName;
97     }
98
99     public void setFileName(Expression fileName) {
100         this.fileName = fileName;
101     }
102
103     // Implementation methods
104
//-------------------------------------------------------------------------
105

106     /**
107      * Writes the given value to the output stream
108      *
109      * @param value the value to be written to the stream
110      * @param out the output stream
111      */

112     protected void writeValue(Object JavaDoc value, OutputStream JavaDoc out) throws IOException JavaDoc, MessagingException {
113         if (value instanceof String JavaDoc) {
114             OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(out);
115             writer.write((String JavaDoc) value);
116             writer.flush();
117         }
118         else if (value instanceof byte[]) {
119             out.write((byte[]) value);
120         }
121         else {
122             ObjectOutputStream JavaDoc objectOut = new ObjectOutputStream JavaDoc(out);
123             objectOut.writeObject(value);
124         }
125     }
126
127     /**
128      * Writes the message content to the given output stream
129      *
130      * @param message the message who's content we are about to write
131      * @param out the destination of the output
132      * @param name the name of the output resource (file, uri, url)
133      */

134     protected void writeMessageContent(MessageExchange exchange, NormalizedMessage message, OutputStream JavaDoc out, String JavaDoc path) throws MessagingException {
135         Source JavaDoc content = null;
136         Node JavaDoc document = (Node JavaDoc) message.getProperty(SourceTransformer.CONTENT_DOCUMENT_PROPERTY);
137         if (document != null) {
138             content = new DOMSource JavaDoc(document);
139         }
140         else {
141             content = message.getContent();
142         }
143         if (content == null) {
144             throw new NoMessageContentAvailableException(exchange);
145         }
146         try {
147             getTransformer().toResult(content, new StreamResult JavaDoc(out));
148         } catch (TransformerException JavaDoc e) {
149             throw new MessagingException(e);
150         }
151     }
152 }
153
Popular Tags