/*
 * Decompiled with CFR 0.152.
 *
 * Could not load the following classes:
 *  org.springframework.amqp.core.Message
 *  org.springframework.amqp.core.MessageProperties
 *  org.springframework.amqp.support.converter.MessageConversionException
 *  org.springframework.amqp.support.converter.MessageConverter
 */
package org.jeecg.boot.starter.rabbitmq.core;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;

public class MapMessageConverter implements MessageConverter {
    public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
        return new Message(object.toString().getBytes(), messageProperties);
    }

    public Object fromMessage(Message message) throws MessageConversionException {
        String contentType = message.getMessageProperties().getContentType();
        if (null != contentType && contentType.contains("text")) {
            return new String(message.getBody());
        }
        ObjectInputStream objInt = null;
        try {
            ByteArrayInputStream byteInt = new ByteArrayInputStream(message.getBody());
            objInt = new ObjectInputStream(byteInt);
            HashMap map = (HashMap) objInt.readObject();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}