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);
}
}
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;
}
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);
}
}
}
}
Aggregations