2008-05-15 22.29 http://servicemix.apache.org/dist/servicemix-3.2.1/site/parent/co…/org/apache/servicemix/components/util/DefaultFileMarshaler.html
Sida 1 av 3 001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0 010 *
011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and 015 * limitations under the License.
016 */
017 package org.apache.servicemix.components.util;
018
019 import java.io.File;
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.ObjectOutputStream;
023 import java.io.OutputStream;
024 import java.io.OutputStreamWriter;
025
026 import javax.jbi.JBIException;
027 import javax.jbi.messaging.MessageExchange;
028 import javax.jbi.messaging.MessagingException;
029 import javax.jbi.messaging.NormalizedMessage;
030 import javax.xml.transform.Source;
031 import javax.xml.transform.TransformerException;
032 import javax.xml.transform.stream.StreamResult;
033 import javax.xml.transform.stream.StreamSource;
034
035 import org.apache.servicemix.expression.Expression;
036 import org.apache.servicemix.expression.PropertyExpression;
037 import org.apache.servicemix.jbi.NoMessageContentAvailableException;
038 039 /**
040 * A default file transformer which assumes the file is already in XML format and 041 * requires no transformation other than to be wrapped in a normalized message..
042 *
043 * @org.apache.xbean.XBean 044 * @version $Revision: 564374 $ 045 */
046 public class DefaultFileMarshaler extends MarshalerSupport implements FileMarshaler { 047
048 public static final String FILE_NAME_PROPERTY = "org.apache.servicemix.file.name";
049 public static final String FILE_PATH_PROPERTY = "org.apache.servicemix.file.path";
050 public static final String FILE_CONTENT = "org.apache.servicemix.file.content";
051
052 protected static final PropertyExpression FILE_NAME_EXPRESSION = new PropertyExpression(FILE_NAME_PROPERTY);
053 protected static final PropertyExpression FILE_CONTENT_EXPRESSION = new PropertyExpression(FILE_CONTENT);
054
055 private Expression fileName = FILE_NAME_EXPRESSION;
056 private Expression content = FILE_CONTENT_EXPRESSION;
057
058 public void readMessage(MessageExchange exchange, NormalizedMessage message,
059 InputStream in, String path) throws IOException, JBIException { 060 message.setContent(new StreamSource(in, path));
061 message.setProperty(FILE_NAME_PROPERTY, new File(path).getName());
062 message.setProperty(FILE_PATH_PROPERTY, path);
063 } 064
065 public String getOutputName(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 066 return asString(fileName.evaluate(exchange, message));
067 } 068
069 public void writeMessage(MessageExchange exchange, NormalizedMessage message,
070 OutputStream out, String path) throws IOException, JBIException { 071 try {
072 Object value = content.evaluate(exchange, message);
073 if (value != null) { 074 writeValue(value, out);
075 } else {
076 writeMessageContent(exchange, message, out, path);
077 }
078 } catch (IOException e) {
079 throw new MessagingException(e);
080 } 081 } 082
083 // Properties
2008-05-15 22.29 http://servicemix.apache.org/dist/servicemix-3.2.1/site/parent/co…/org/apache/servicemix/components/util/DefaultFileMarshaler.html
Sida 2 av 3 084 //---
085 public Expression getContent() { 086 return content;
087 } 088
089 public void setContent(Expression content) { 090 this.content = content;
091 } 092
093 public Expression getFileName() { 094 return fileName;
095 } 096
097 public void setFileName(Expression fileName) { 098 this.fileName = fileName;
099 } 100
101 // Implementation methods
102 //--- 103
104 /**
105 * Writes the given value to the output stream 106 *
107 * @param value the value to be written to the stream 108 * @param out the output stream
109 */
110 protected void writeValue(Object value, OutputStream out) throws IOException, MessagingException { 111 if (value instanceof String) {
112 OutputStreamWriter writer = new OutputStreamWriter(out);
113 writer.write((String) value);
114 writer.flush();
115 } else if (value instanceof byte[]) { 116 out.write((byte[]) value);
117 } else {
118 ObjectOutputStream objectOut = new ObjectOutputStream(out);
119 objectOut.writeObject(value);
120 } 121 } 122 123 /**
124 * Writes the message content to the given output stream 125 *
126 * @param message the message who's content we are about to write 127 * @param out the destination of the output
128 * @param path the name of the output resource (file, uri, url) 129 */
130 protected void writeMessageContent(MessageExchange exchange, NormalizedMessage message, 131 OutputStream out, String path) throws MessagingException { 132 Source src = message.getContent();
133 if (src == null) {
134 throw new NoMessageContentAvailableException(exchange);
135 } 136 try {
137 getTransformer().toResult(src, new StreamResult(out));
138 } catch (TransformerException e) { 139 throw new MessagingException(e);
140 } 141 } 142 }
2008-05-15 22.29 http://servicemix.apache.org/dist/servicemix-3.2.1/site/parent/co…/org/apache/servicemix/components/util/DefaultFileMarshaler.html
Sida 3 av 3