Search in sources :

Example 1 with SerializationException

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

the class AbstractSerializationProtocol method serialize.

/**
 * {@inheritDoc}
 *
 * @throws IllegalArgumentException if object is not a {@link java.io.Serializable}
 */
@Override
public void serialize(Object object, OutputStream out) throws SerializationException {
    try {
        byte[] bytes = serialize(object);
        out.write(bytes);
        out.flush();
    } catch (IOException e) {
        throw new SerializationException("Could not write to output stream", e);
    } finally {
        closeQuietly(out);
    }
}
Also used : SerializationException(org.mule.runtime.api.serialization.SerializationException) IOException(java.io.IOException)

Example 2 with SerializationException

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

the class AbstractSerializationProtocol method deserialize.

/**
 * {@inheritDoc}
 */
@Override
public <T> T deserialize(InputStream inputStream, ClassLoader classLoader) throws SerializationException {
    checkArgument(inputStream != null, "Cannot deserialize a null stream");
    checkArgument(classLoader != null, "Cannot deserialize with a null classloader");
    try {
        return (T) postInitialize(doDeserialize(inputStream, classLoader));
    } catch (Exception e) {
        throw new SerializationException("Could not deserialize object", e);
    } finally {
        closeQuietly(inputStream);
    }
}
Also used : SerializationException(org.mule.runtime.api.serialization.SerializationException) IOException(java.io.IOException) SerializationException(org.mule.runtime.api.serialization.SerializationException)

Example 3 with SerializationException

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

the class CustomJavaSerializationProtocol method doSerialize.

/**
 * {@inheritDoc}
 */
@Override
protected byte[] doSerialize(Object object) throws Exception {
    // TODO: MULE-11939
    if (object instanceof CursorStreamProvider) {
        try (CursorStream cursor = ((CursorStreamProvider) object).openCursor()) {
            object = toByteArray(cursor);
        }
    }
    validateForSerialization(object);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(512);
    try (ObjectOutputStream out = new ArtifactClassLoaderObjectOutputStream(classLoaderRepository, outputStream)) {
        out.writeObject(object);
    } catch (IOException ex) {
        throw new SerializationException("Cannot serialize object", ex);
    }
    return outputStream.toByteArray();
}
Also used : CursorStreamProvider(org.mule.runtime.api.streaming.bytes.CursorStreamProvider) SerializationException(org.mule.runtime.api.serialization.SerializationException) ArtifactClassLoaderObjectOutputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ArtifactClassLoaderObjectOutputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) CursorStream(org.mule.runtime.api.streaming.bytes.CursorStream)

Example 4 with SerializationException

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

the class CustomJavaSerializationProtocol method doDeserialize.

/**
 * {@inheritDoc}
 */
@Override
protected <T> T doDeserialize(InputStream inputStream, ClassLoader classLoader) throws Exception {
    checkArgument(inputStream != null, "Cannot deserialize a null stream");
    checkArgument(classLoader != null, "Cannot deserialize with a null classloader");
    try (ObjectInputStream in = new ArtifactClassLoaderObjectInputStream(classLoaderRepository, inputStream)) {
        Object obj = in.readObject();
        return (T) obj;
    } catch (Exception ex) {
        throw new SerializationException("Cannot deserialize object", ex);
    }
}
Also used : SerializationException(org.mule.runtime.api.serialization.SerializationException) ArtifactClassLoaderObjectInputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectInputStream) IOException(java.io.IOException) SerializationException(org.mule.runtime.api.serialization.SerializationException) ObjectInputStream(java.io.ObjectInputStream) ArtifactClassLoaderObjectInputStream(org.mule.runtime.module.artifact.api.serializer.ArtifactClassLoaderObjectInputStream)

Example 5 with SerializationException

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

the class MessageChunkAggregator method getCorrelatorCallback.

@Override
protected EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext) {
    return new CollectionCorrelatorCallback(muleContext, storePrefix) {

        /**
         * This method is invoked if the shouldAggregate method is called and returns true. Once this method returns an aggregated
         * message the event group is removed from the router
         *
         * @param events the event group for this request
         * @return an aggregated message
         * @throws AggregationException if the aggregation fails. in this scenario the whole event
         *         group is removed and passed to the exception handler for this componenet
         */
        @Override
        public CoreEvent aggregateEvents(EventGroup events) throws AggregationException {
            PrivilegedEvent[] collectedEvents;
            try {
                collectedEvents = events.toArray(false);
            } catch (ObjectStoreException e) {
                throw new AggregationException(events, MessageChunkAggregator.this, e);
            }
            CoreEvent firstEvent = collectedEvents[0];
            Arrays.sort(collectedEvents, eventComparator);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
            try {
                for (PrivilegedEvent event : collectedEvents) {
                    baos.write(event.getMessageAsBytes(muleContext));
                }
                final Message.Builder builder = Message.builder(firstEvent.getMessage());
                // try to deserialize message, since ChunkingRouter might have serialized the object...
                try {
                    builder.value(muleContext.getObjectSerializer().getInternalProtocol().deserialize(baos.toByteArray()));
                } catch (SerializationException e) {
                    builder.value(baos.toByteArray());
                }
                // Use last event, that hasn't been completed yet, for continued processing.
                return PrivilegedEvent.builder(collectedEvents[collectedEvents.length - 1]).message(builder.build()).session(getMergedSession(events.toArray())).build();
            } catch (Exception e) {
                throw new AggregationException(events, MessageChunkAggregator.this, e);
            } finally {
                closeQuietly(baos);
            }
        }
    };
}
Also used : CollectionCorrelatorCallback(org.mule.runtime.core.internal.routing.correlation.CollectionCorrelatorCallback) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) SerializationException(org.mule.runtime.api.serialization.SerializationException) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) SerializationException(org.mule.runtime.api.serialization.SerializationException)

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