diff --git a/README.md b/README.md index d77b780a..68f84b83 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# th2 common library (Java) (3.31.1) +# th2 common library (Java) (3.32.0) ## Usage @@ -288,6 +288,10 @@ dependencies { ## Release notes +### 3.32.0 + ++ Improve `messageRecursionLimit` applying mechanism + ### 3.31.1 + Feature as test assertion methods for messages from fixtures diff --git a/build.gradle b/build.gradle index cc34b5ac..cc71079d 100644 --- a/build.gradle +++ b/build.gradle @@ -157,7 +157,7 @@ test { } dependencies { - api platform('com.exactpro.th2:bom:3.0.0') + api platform('com.exactpro.th2:bom:3.1.0') api "com.exactpro.th2:cradle-core:${cradleVersion}" api 'com.exactpro.th2:grpc-common:3.8.0' diff --git a/gradle.properties b/gradle.properties index 4e56a3fa..ade3f573 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ # limitations under the License. # -release_version=3.31.1 +release_version=3.32.0 description = 'th2 common library (Java)' diff --git a/src/main/java/com/exactpro/th2/common/schema/message/impl/rabbitmq/connection/ConnectionManager.java b/src/main/java/com/exactpro/th2/common/schema/message/impl/rabbitmq/connection/ConnectionManager.java index c073a711..b03e8ed2 100644 --- a/src/main/java/com/exactpro/th2/common/schema/message/impl/rabbitmq/connection/ConnectionManager.java +++ b/src/main/java/com/exactpro/th2/common/schema/message/impl/rabbitmq/connection/ConnectionManager.java @@ -15,9 +15,11 @@ package com.exactpro.th2.common.schema.message.impl.rabbitmq.connection; import com.exactpro.th2.common.metrics.HealthMetrics; +import com.exactpro.th2.common.schema.exception.CommonFactoryException; import com.exactpro.th2.common.schema.message.SubscriberMonitor; import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.ConnectionManagerConfiguration; import com.exactpro.th2.common.schema.message.impl.rabbitmq.configuration.RabbitMQConfiguration; +import com.exactpro.th2.common.schema.util.ProtobufUtils; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.BlockedListener; @@ -96,6 +98,12 @@ public ConnectionManager(@NotNull RabbitMQConfiguration rabbitMQConfiguration, @ Objects.requireNonNull(rabbitMQConfiguration, "RabbitMQ configuration cannot be null"); this.configuration = Objects.requireNonNull(connectionManagerConfiguration, "Connection manager configuration can not be null"); + try { + ProtobufUtils.changeRecursionLimit(configuration.getMessageRecursionLimit()); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new CommonFactoryException("Failed to change GRPC messageRecursionLimit", e); + } + String subscriberNameTmp = ObjectUtils.defaultIfNull(connectionManagerConfiguration.getSubscriberName(), rabbitMQConfiguration.getSubscriberName()); if (StringUtils.isBlank(subscriberNameTmp)) { subscriberName = "rabbit_mq_subscriber." + System.currentTimeMillis(); diff --git a/src/main/java/com/exactpro/th2/common/schema/util/ProtobufUtils.java b/src/main/java/com/exactpro/th2/common/schema/util/ProtobufUtils.java new file mode 100644 index 00000000..a3645e6e --- /dev/null +++ b/src/main/java/com/exactpro/th2/common/schema/util/ProtobufUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exactpro.th2.common.schema.util; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.util.JsonFormat; + +public class ProtobufUtils { + + public static void changeRecursionLimit(int value) throws NoSuchFieldException, IllegalAccessException { + changeCodedInputStreamLimit(value); + changeJsonFormatLimit(value); + } + + public static void changeCodedInputStreamLimit(int value) throws NoSuchFieldException, IllegalAccessException { + modifyPrivateInt(CodedInputStream.class, "defaultRecursionLimit", value); + } + + public static void changeJsonFormatLimit(int value) throws NoSuchFieldException, IllegalAccessException { + modifyPrivateInt(JsonFormat.Parser.class, "DEFAULT_RECURSION_LIMIT", value); + } + + private static void modifyPrivateInt(Class clazz, String fieldName, int value) throws NoSuchFieldException, IllegalAccessException { + Field recursionLimitField = clazz.getDeclaredField(fieldName); + recursionLimitField.setAccessible(true); + + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(recursionLimitField, recursionLimitField.getModifiers() & ~Modifier.FINAL); + recursionLimitField.set(null, value); + recursionLimitField.setAccessible(false); + } + +} diff --git a/src/test/java/com/exactpro/th2/common/schema/util/ProtobufUtilsTest.java b/src/test/java/com/exactpro/th2/common/schema/util/ProtobufUtilsTest.java new file mode 100644 index 00000000..de726a75 --- /dev/null +++ b/src/test/java/com/exactpro/th2/common/schema/util/ProtobufUtilsTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exactpro.th2.common.schema.util; + +import java.lang.reflect.Field; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.util.JsonFormat; + +class ProtobufUtilsTest { + + @Test + void testChangeCodedInputStreamLimit() throws NoSuchFieldException, IllegalAccessException { + int limitToTest = 500; + ProtobufUtils.changeCodedInputStreamLimit(limitToTest); + Integer changedLimit = getField(CodedInputStream.class, "defaultRecursionLimit"); + Assertions.assertEquals(limitToTest, changedLimit); + } + + @Test + void testChangeJsonFormatRecursionLimit() throws NoSuchFieldException, IllegalAccessException { + int limitToTest = 600; + ProtobufUtils.changeJsonFormatLimit(limitToTest); + Integer changedLimit = getField(JsonFormat.Parser.class, "DEFAULT_RECURSION_LIMIT"); + Assertions.assertEquals(limitToTest, changedLimit); + } + + static T getField(Class clazz, String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + return (T)field.get(null); + } + +} \ No newline at end of file