Search in sources :

Example 6 with ObjectInputStreamWithClassLoader

use of org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader in project activemq-artemis by apache.

the class ObjectInputStreamWithClassLoaderTest method testClassLoaderIsolation.

// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testClassLoaderIsolation() throws Exception {
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        AnObject obj = new AnObjectImpl();
        byte[] bytes = ObjectInputStreamWithClassLoaderTest.toBytes(obj);
        // Class.isAnonymousClass() call used in ObjectInputStreamWithClassLoader
        // need to access the enclosing class and its parent class of the obj
        // i.e. ActiveMQTestBase and Assert.
        ClassLoader testClassLoader = ObjectInputStreamWithClassLoaderTest.newClassLoader(obj.getClass(), ActiveMQTestBase.class, Assert.class);
        Thread.currentThread().setContextClassLoader(testClassLoader);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStreamWithClassLoader ois = new ObjectInputStreamWithClassLoader(bais);
        Object deserializedObj = ois.readObject();
        Assert.assertNotSame(obj, deserializedObj);
        Assert.assertNotSame(obj.getClass(), deserializedObj.getClass());
        Assert.assertNotSame(obj.getClass().getClassLoader(), deserializedObj.getClass().getClassLoader());
        Assert.assertSame(testClassLoader, deserializedObj.getClass().getClassLoader());
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : ObjectInputStreamWithClassLoader(org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) URLClassLoader(java.net.URLClassLoader) ObjectInputStreamWithClassLoader(org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader) Test(org.junit.Test)

Example 7 with ObjectInputStreamWithClassLoader

use of org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader in project activemq-artemis by apache.

the class JsonUtil method fromJsonArray.

public static Object[] fromJsonArray(final JsonArray jsonArray) throws Exception {
    Object[] array = new Object[jsonArray.size()];
    for (int i = 0; i < jsonArray.size(); i++) {
        Object val = jsonArray.get(i);
        if (val instanceof JsonArray) {
            Object[] inner = fromJsonArray((JsonArray) val);
            array[i] = inner;
        } else if (val instanceof JsonObject) {
            JsonObject jsonObject = (JsonObject) val;
            Map<String, Object> map = new HashMap<>();
            Set<String> keys = jsonObject.keySet();
            for (String key : keys) {
                Object innerVal = jsonObject.get(key);
                if (innerVal instanceof JsonArray) {
                    innerVal = fromJsonArray(((JsonArray) innerVal));
                } else if (innerVal instanceof JsonString) {
                    innerVal = ((JsonString) innerVal).getString();
                } else if (innerVal == JsonValue.FALSE) {
                    innerVal = Boolean.FALSE;
                } else if (innerVal == JsonValue.TRUE) {
                    innerVal = Boolean.TRUE;
                } else if (innerVal instanceof JsonNumber) {
                    JsonNumber jsonNumber = (JsonNumber) innerVal;
                    if (jsonNumber.isIntegral()) {
                        innerVal = jsonNumber.longValue();
                    } else {
                        innerVal = jsonNumber.doubleValue();
                    }
                } else if (innerVal instanceof JsonObject) {
                    Map<String, Object> innerMap = new HashMap<>();
                    JsonObject o = (JsonObject) innerVal;
                    Set<String> innerKeys = o.keySet();
                    for (String k : innerKeys) {
                        innerMap.put(k, o.get(k));
                    }
                    innerVal = innerMap;
                }
                if (CompositeData.class.getName().equals(key)) {
                    Object[] data = (Object[]) innerVal;
                    CompositeData[] cds = new CompositeData[data.length];
                    for (int i1 = 0; i1 < data.length; i1++) {
                        String dataConverted = convertJsonValue(data[i1], String.class).toString();
                        try (ObjectInputStreamWithClassLoader ois = new ObjectInputStreamWithClassLoader(new ByteArrayInputStream(Base64.decode(dataConverted)))) {
                            ois.setWhiteList("java.util,java.lang,javax.management");
                            cds[i1] = (CompositeDataSupport) ois.readObject();
                        }
                    }
                    innerVal = cds;
                }
                map.put(key, innerVal);
            }
            array[i] = map;
        } else if (val instanceof JsonString) {
            array[i] = ((JsonString) val).getString();
        } else if (val == JsonValue.FALSE) {
            array[i] = Boolean.FALSE;
        } else if (val == JsonValue.TRUE) {
            array[i] = Boolean.TRUE;
        } else if (val instanceof JsonNumber) {
            JsonNumber jsonNumber = (JsonNumber) val;
            if (jsonNumber.isIntegral()) {
                array[i] = jsonNumber.longValue();
            } else {
                array[i] = jsonNumber.doubleValue();
            }
        } else {
            if (val == JsonValue.NULL) {
                array[i] = null;
            } else {
                array[i] = val;
            }
        }
    }
    return array;
}
Also used : ObjectInputStreamWithClassLoader(org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader) Set(java.util.Set) HashMap(java.util.HashMap) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonArray(javax.json.JsonArray) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with ObjectInputStreamWithClassLoader

use of org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader in project activemq-artemis by apache.

the class HttpMessageHelper method buildMessage.

public static void buildMessage(ClientMessage message, ClientRequest request, String contentType, ConnectionFactoryOptions jmsOptions) {
    for (SimpleString key : message.getPropertyNames()) {
        String k = key.toString();
        String headerName = HttpHeaderProperty.fromPropertyName(k);
        if (headerName == null || headerName.contains("content-length")) {
            continue;
        }
        String value = message.getStringProperty(k);
        request.header(headerName, value);
        ActiveMQRestLogger.LOGGER.debug("Examining " + headerName + ": " + value);
        // override default content type if it is set as a message property
        if (headerName.equalsIgnoreCase("content-type")) {
            contentType = value;
            ActiveMQRestLogger.LOGGER.debug("Using contentType: " + contentType);
        }
    }
    int size = message.getBodySize();
    if (size > 0) {
        Boolean aBoolean = message.getBooleanProperty(POSTED_AS_HTTP_MESSAGE);
        if (aBoolean != null && aBoolean.booleanValue()) {
            byte[] body = new byte[size];
            message.getBodyBuffer().readBytes(body);
            ActiveMQRestLogger.LOGGER.debug("Building Message from HTTP message");
            request.body(contentType, body);
        } else {
            // assume posted as a JMS or ActiveMQ Artemis object message
            size = message.getBodyBuffer().readInt();
            byte[] body = new byte[size];
            message.getBodyBuffer().readBytes(body);
            ByteArrayInputStream bais = new ByteArrayInputStream(body);
            Object obj = null;
            try (ObjectInputStreamWithClassLoader ois = new ObjectInputStreamWithClassLoader(bais)) {
                if (jmsOptions != null) {
                    ois.setBlackList(jmsOptions.getDeserializationBlackList());
                    ois.setWhiteList(jmsOptions.getDeserializationWhiteList());
                }
                obj = ois.readObject();
                ActiveMQRestLogger.LOGGER.debug("**** Building Message from object: " + obj.toString());
                request.body(contentType, obj);
            } catch (Exception e) {
                ActiveMQRestLogger.LOGGER.failedToBuildMessageFromObject(e);
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : ObjectInputStreamWithClassLoader(org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Aggregations

ObjectInputStreamWithClassLoader (org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Test (org.junit.Test)3 FileInputStream (java.io.FileInputStream)2 URLClassLoader (java.net.URLClassLoader)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 JMSException (javax.jms.JMSException)1 MessageFormatException (javax.jms.MessageFormatException)1 JsonArray (javax.json.JsonArray)1 JsonNumber (javax.json.JsonNumber)1 JsonObject (javax.json.JsonObject)1 JsonString (javax.json.JsonString)1 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)1