001: /*
002: * $Id: TransformerPairWireFormat.java 10776 2008-02-11 18:15:42Z rossmason $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transformer.wire;
012:
013: import org.mule.api.MuleException;
014: import org.mule.api.DefaultMuleException;
015: import org.mule.api.transformer.Transformer;
016: import org.mule.api.transformer.TransformerException;
017: import org.mule.api.transformer.wire.WireFormat;
018: import org.mule.config.i18n.CoreMessages;
019: import org.mule.util.IOUtils;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: import org.apache.commons.io.output.ByteArrayOutputStream;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * TODO
031: */
032: public class TransformerPairWireFormat implements WireFormat {
033:
034: /**
035: * logger used by this class
036: */
037: protected transient Log logger = LogFactory.getLog(getClass());
038:
039: protected Transformer inboundTransformer;
040: protected Transformer outboundTransformer;
041: protected Class transferObjectClass;
042:
043: public Object read(InputStream in) throws MuleException {
044: if (inboundTransformer == null) {
045: throw new IllegalArgumentException(CoreMessages
046: .objectIsNull("inboundTransformer").getMessage());
047: }
048: if (inboundTransformer.isSourceTypeSupported(InputStream.class)) {
049: return inboundTransformer.transform(in);
050: } else {
051: try {
052: ByteArrayOutputStream baos = new ByteArrayOutputStream();
053: IOUtils.copy(in, baos);
054: return inboundTransformer.transform(baos.toByteArray());
055: } catch (IOException e) {
056: throw new DefaultMuleException(CoreMessages
057: .failedToReadPayload(), e);
058: }
059: }
060: }
061:
062: public void write(OutputStream out, Object o, String encoding)
063: throws MuleException {
064:
065: if (outboundTransformer == null) {
066: throw new IllegalArgumentException(CoreMessages
067: .objectIsNull("outboundTransformer").getMessage());
068: }
069: try {
070: Class returnClass = outboundTransformer.getReturnClass();
071: if (returnClass == null) {
072: logger
073: .warn("No return class was set on transformer: "
074: + outboundTransformer
075: + ". Attempting to work out how to treat the result transformation");
076:
077: Object result = outboundTransformer.transform(o);
078:
079: byte[] bytes;
080: if (result instanceof byte[]) {
081: bytes = (byte[]) result;
082: } else {
083: bytes = result.toString().getBytes(encoding);
084: }
085:
086: out.write(bytes);
087: } else if (returnClass.equals(byte[].class)) {
088: byte[] b = (byte[]) outboundTransformer.transform(o);
089: out.write(b);
090: } else if (returnClass.equals(String.class)) {
091: String s = (String) outboundTransformer.transform(o);
092: out.write(s.getBytes(encoding));
093: } else {
094: throw new TransformerException(CoreMessages
095: .transformFailedFrom(o.getClass()));
096: }
097: } catch (IOException e) {
098: throw new TransformerException(CoreMessages
099: .transformFailedFrom(o.getClass()), e);
100: }
101: }
102:
103: public Transformer getInboundTransformer() {
104: return inboundTransformer;
105: }
106:
107: public void setInboundTransformer(Transformer inboundTransformer) {
108: this .inboundTransformer = inboundTransformer;
109: }
110:
111: public Transformer getOutboundTransformer() {
112: return outboundTransformer;
113: }
114:
115: public void setOutboundTransformer(Transformer outboundTransformer) {
116: this .outboundTransformer = outboundTransformer;
117: }
118:
119: public void setTransferObjectClass(Class clazz) {
120: transferObjectClass = clazz;
121: }
122: }
|