All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.se.ase.jms.JMSMessageExchangeSerializer Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2010-2012 EBM WebSourcing, 2012-2023 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.ase.jms;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.SocketOptions;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.activation.DataHandler;
import javax.mail.util.ByteArrayDataSource;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.ebmwebsourcing.easycommons.stream.EasyByteArrayOutputStream;
import com.ebmwebsourcing.easycommons.xml.Transformers;

/**
 * 

* {@link JMSMessageExchange} serialization/deserialisation utilities. *

*

* Extracted from the library {@code org.ow2.petals:petals-java-orchestration:1.1.0}, class * '{@code org.ow2.petals.orchestration.core.ICSerializer}'. * * @author Christophe DENEUX - Linagora * */ public class JMSMessageExchangeSerializer { private JMSMessageExchangeSerializer() { // Utility class --> No constructor } public static final void serializeContent(final Source content, final ObjectOutputStream outStream) throws IOException, TransformerException { if (content == null) { // set a flag which indicate that the source is null outStream.writeBoolean(false); } else { outStream.writeBoolean(true); final Transformer transformer = Transformers.takeTransformer(); try { final StreamResult sresult = new StreamResult(outStream); transformer.transform(content, sresult); } finally { Transformers.releaseTransformer(transformer); } } } public static final Source deserializeContent(final ObjectInputStream inStream) throws IOException { if (inStream.readBoolean()) { // the source is not null final StreamSource source = new StreamSource(); byte[] buffer = new byte[SocketOptions.SO_RCVBUF]; try (final EasyByteArrayOutputStream outputStream = new EasyByteArrayOutputStream()) { int n = 0; while (-1 != (n = inStream.read(buffer))) { outputStream.write(buffer, 0, n); } source.setInputStream(outputStream.toByteArrayInputStream()); } return source; } else { return null; } } /** *

* Serialize all datahandler in the Map. *

*

* Serialized information is: *

*
    *
  • attachmentName,
  • *
  • attachmentContentType,
  • *
  • name,
  • *
  • inputStream available bytes count,
  • *
  • inputStream as a byteArray.
  • *
* * @param attachments * @param s * @throws IOException */ public static final void serializeAttachments(final Map attachments, final ObjectOutputStream s) throws IOException { s.writeInt(attachments.size()); for (final Entry attachment : attachments.entrySet()) { final InputStream binary = attachment.getValue().getInputStream(); try { final int available = binary.available(); // write information about the attachment s.writeUTF(attachment.getKey()); s.writeUTF(attachment.getValue().getContentType()); s.writeUTF(attachment.getValue().getName()); s.writeInt(available); // write attachment content with the default socket output // buffer size byte[] buff = new byte[SocketOptions.SO_SNDBUF]; int n = 0; while (-1 != (n = binary.read(buff))) { s.write(buff, 0, n); s.flush(); } } finally { binary.close(); } } } /** *

* Deserialize all datahandlers in the stream and return a Map. *

*

* Deserialized information is: *

*
    *
  • attachmentName,
  • *
  • attachmentContentType,
  • *
  • name,
  • *
  • Stream available bytes count,
  • *
  • Stream as a byteArray.
  • *
* * @param s * @return * @throws IOException */ public static final Map deserializeAttachments(final ObjectInputStream s) throws IOException { final Map attachments = new HashMap<>(); final int attachmentsCount = s.readInt(); for (int i = 0; i < attachmentsCount; i++) { // read information final String attachmentName = s.readUTF(); final String contentType = s.readUTF(); final String name = s.readUTF(); final int available = s.readInt(); // read content final byte[] content = new byte[available]; s.readFully(content); final ByteArrayDataSource dataSource = new ByteArrayDataSource(content, contentType); dataSource.setName(name); // create the dataHandler final DataHandler dataHandler = new DataHandler(dataSource); attachments.put(attachmentName, dataHandler); } return attachments; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy