You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2022/01/07 18:17:58 UTC

[syncope] 03/03: Working around Spring Cloud Gateway tie to Hibernate Validator and not Jakarta Validation API

This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git

commit a54bade156ddf0f4da9dd1f188d438aa6c1c8b2a
Author: Francesco Chicchiriccò <il...@apache.org>
AuthorDate: Fri Jan 7 19:17:03 2022 +0100

    Working around Spring Cloud Gateway tie to Hibernate Validator and not Jakarta Validation API
---
 .../cloud/gateway/config/DataSizeMax.java          | 48 ++++++++++++++++++++++
 .../cloud/gateway/config/DataSizeMaxValidator.java | 41 ++++++++++++++++++
 .../cloud/gateway/config/HttpClientProperties.java | 20 +++++----
 3 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMax.java b/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMax.java
new file mode 100644
index 0000000..9d559a9
--- /dev/null
+++ b/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMax.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.springframework.cloud.gateway.config;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import org.springframework.util.unit.DataUnit;
+
+// Keep until
+// https://lists.apache.org/thread/km7c4ojrlw5q5j42cbw7nht6b0f4z5r2
+// is clear.
+@Documented
+@Constraint(validatedBy = DataSizeMaxValidator.class)
+@Target({ ElementType.FIELD, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface DataSizeMax {
+
+    String message() default "must be less than or equal to {value} {unit}";
+
+    Class<?>[] groups() default {};
+
+    Class<? extends Payload>[] payload() default {};
+
+    int value();
+
+    DataUnit unit();
+}
diff --git a/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMaxValidator.java b/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMaxValidator.java
new file mode 100644
index 0000000..5e3d5aa
--- /dev/null
+++ b/sra/src/main/java/org/springframework/cloud/gateway/config/DataSizeMaxValidator.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.springframework.cloud.gateway.config;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import org.springframework.util.unit.DataSize;
+
+// Keep until
+// https://lists.apache.org/thread/km7c4ojrlw5q5j42cbw7nht6b0f4z5r2
+// is clear.
+public class DataSizeMaxValidator implements ConstraintValidator<DataSizeMax, DataSize> {
+
+    private DataSize dataSizeMax;
+
+    @Override
+    public void initialize(final DataSizeMax dataSizeMax) {
+        this.dataSizeMax = DataSize.of(dataSizeMax.value(), dataSizeMax.unit());
+    }
+
+    @Override
+    public boolean isValid(final DataSize value, final ConstraintValidatorContext context) {
+        return value == null || dataSizeMax.compareTo(value) >= 0;
+    }
+}
diff --git a/sra/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java b/sra/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java
index a3b095b..1a03102 100644
--- a/sra/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java
+++ b/sra/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java
@@ -31,7 +31,6 @@ import java.util.Collection;
 import java.util.List;
 
 import javax.net.ssl.KeyManagerFactory;
-import javax.validation.constraints.Max;
 
 import reactor.netty.resources.ConnectionProvider;
 import reactor.netty.tcp.SslProvider;
@@ -42,17 +41,19 @@ import org.springframework.boot.web.server.WebServerException;
 import org.springframework.core.style.ToStringCreator;
 import org.springframework.util.ResourceUtils;
 import org.springframework.util.unit.DataSize;
-//import org.springframework.validation.annotation.Validated;
+import org.springframework.util.unit.DataUnit;
+import org.springframework.validation.annotation.Validated;
 
 // CHECKSTYLE:OFF
-// Semi-blind copy from Spring Cloud Gateway sources, to keep until
-// https://github.com/spring-cloud/spring-cloud-gateway/issues/2303
-// is fixed.
+// Semi-blind copy from Spring Cloud Gateway sources, to keep until they are tied
+// to Hibernate Validator and not Jakarta Validation API - see
+// https://lists.apache.org/thread/km7c4ojrlw5q5j42cbw7nht6b0f4z5r2
+// for details.
 /**
  * Configuration properties for the Netty {@link reactor.netty.http.client.HttpClient}.
  */
 @ConfigurationProperties("spring.cloud.gateway.httpclient")
-//@Validated
+@Validated
 @SuppressWarnings("deprecation")
 public class HttpClientProperties {
 
@@ -102,7 +103,7 @@ public class HttpClientProperties {
 		this.responseTimeout = responseTimeout;
 	}
 
-	@Max(Integer.MAX_VALUE)
+	@DataSizeMax(value = Integer.MAX_VALUE, unit = DataUnit.BYTES)
 	public DataSize getMaxHeaderSize() {
 		return maxHeaderSize;
 	}
@@ -111,7 +112,7 @@ public class HttpClientProperties {
 		this.maxHeaderSize = maxHeaderSize;
 	}
 
-	@Max(Integer.MAX_VALUE)
+	@DataSizeMax(value = Integer.MAX_VALUE, unit = DataUnit.BYTES)
 	public DataSize getMaxInitialLineLength() {
 		return maxInitialLineLength;
 	}
@@ -422,6 +423,7 @@ public class HttpClientProperties {
 		private Duration closeNotifyReadTimeout = Duration.ZERO;
 
 		/** The default ssl configuration type. Defaults to TCP. */
+		@Deprecated
 		private SslProvider.DefaultConfigurationType defaultConfigurationType = SslProvider.DefaultConfigurationType.TCP;
 
 		/** Keystore path for Netty HttpClient. */
@@ -587,10 +589,12 @@ public class HttpClientProperties {
 			this.closeNotifyReadTimeout = closeNotifyReadTimeout;
 		}
 
+		@Deprecated
 		public SslProvider.DefaultConfigurationType getDefaultConfigurationType() {
 			return defaultConfigurationType;
 		}
 
+		@Deprecated
 		public void setDefaultConfigurationType(SslProvider.DefaultConfigurationType defaultConfigurationType) {
 			this.defaultConfigurationType = defaultConfigurationType;
 		}