Search in sources :

Example 6 with SerializationException

use of org.mule.runtime.api.serialization.SerializationException in project mule by mulesoft.

the class GZipUncompressTransformer method doTransform.

@Override
public Object doTransform(Object src, Charset outputEncoding) throws TransformerException {
    try {
        if (src instanceof CursorStreamProvider) {
            return getStrategy().uncompressInputStream(((CursorStreamProvider) src).openCursor());
        }
        if (src instanceof InputStream) {
            return getStrategy().uncompressInputStream((InputStream) src);
        } else {
            byte[] buffer = getStrategy().uncompressByteArray((byte[]) src);
            DataType returnDataType = getReturnDataType();
            // If a return type has been specified, then deserialize the uncompressed byte array.
            if (DataType.STRING.isCompatibleWith(returnDataType)) {
                return new String(buffer, outputEncoding);
            } else if (!DataType.OBJECT.isCompatibleWith(returnDataType) && !DataType.BYTE_ARRAY.isCompatibleWith(returnDataType)) {
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    throw new TransformerException(this, e);
                }
            } else {
                // First try to deserialize the byte array. If it can be deserialized, then it was originally serialized.
                try {
                    return muleContext.getObjectSerializer().getExternalProtocol().deserialize(buffer);
                } catch (SerializationException e) {
                    // If it fails, ignore it. We assume it was not serialized in the first place and return the buffer as it is.
                    return buffer;
                }
            }
        }
    } catch (IOException e) {
        throw new TransformerException(I18nMessageFactory.createStaticMessage("Failed to uncompress message."), this, e);
    }
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SerializationException(org.mule.runtime.api.serialization.SerializationException) InputStream(java.io.InputStream) DataType(org.mule.runtime.api.metadata.DataType) IOException(java.io.IOException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Aggregations

SerializationException (org.mule.runtime.api.serialization.SerializationException)6 IOException (java.io.IOException)5 CursorStreamProvider (org.mule.runtime.api.streaming.bytes.CursorStreamProvider)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 Message (org.mule.runtime.api.message.Message)1 DataType (org.mule.runtime.api.metadata.DataType)1 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)1 CursorStream (org.mule.runtime.api.streaming.bytes.CursorStream)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)1 CollectionCorrelatorCallback (org.mule.runtime.core.internal.routing.correlation.CollectionCorrelatorCallback)1 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)1 ArtifactClassLoaderObjectInputStream (org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectInputStream)1 ArtifactClassLoaderObjectOutputStream (org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectOutputStream)1