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

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

commit b6538b1220a266df95902b5c953b6577771e87e2
Author: Francesco Chicchiriccò <ilgro...@apache.org>
AuthorDate: Fri Jun 21 12:22:14 2024 +0200

    Make BatchItemRequest more independent from incoming HttpServletRequest
---
 .../core/rest/cxf/batch/BatchItemRequest.java      | 154 +++++++++++----------
 .../syncope/core/rest/cxf/batch/BatchProcess.java  |  52 ++++++-
 .../core/rest/cxf/service/SyncopeServiceImpl.java  |   9 +-
 pom.xml                                            |   2 +-
 4 files changed, 137 insertions(+), 80 deletions(-)

diff --git 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchItemRequest.java
 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchItemRequest.java
index 71e0c29635..33ee7bac6d 100644
--- 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchItemRequest.java
+++ 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchItemRequest.java
@@ -20,12 +20,12 @@ package org.apache.syncope.core.rest.cxf.batch;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Optional;
+import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 import javax.servlet.ReadListener;
@@ -33,8 +33,6 @@ import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.ws.rs.core.HttpHeaders;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -44,19 +42,49 @@ public class BatchItemRequest extends 
HttpServletRequestWrapper {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(BatchItemRequest.class);
 
-    private final String basePath;
+    private final String scheme;
+
+    private final String serverName;
+
+    private final int serverPort;
+
+    private final String contextPath;
+
+    private final String servletPath;
+
+    private final String pathInfo;
+
+    private final String characterEncoding;
+
+    private final String baseURI;
 
     private final BatchRequestItem batchItem;
 
     private final ServletInputStream inputStream;
 
+    private final Map<String, Object> attributes = new HashMap<>();
+
     public BatchItemRequest(
-            final String basePath,
+            final String scheme,
+            final String serverName,
+            final int serverPort,
+            final String contextPath,
+            final String servletPath,
+            final String pathInfo,
+            final String characterEncoding,
+            final String baseURI,
             final HttpServletRequest request,
             final BatchRequestItem batchItem) {
 
         super(request);
-        this.basePath = basePath;
+        this.scheme = scheme;
+        this.serverName = serverName;
+        this.serverPort = serverPort;
+        this.contextPath = contextPath;
+        this.servletPath = servletPath;
+        this.pathInfo = pathInfo;
+        this.characterEncoding = characterEncoding;
+        this.baseURI = baseURI;
         this.batchItem = batchItem;
         this.inputStream = new ServletInputStream() {
 
@@ -91,35 +119,43 @@ public class BatchItemRequest extends 
HttpServletRequestWrapper {
     }
 
     @Override
-    public String getMethod() {
-        return batchItem.getMethod();
+    public String getScheme() {
+        return scheme;
     }
 
     @Override
     public String getServerName() {
-        try {
-            return super.getServerName();
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return Optional.ofNullable(getHeader(HttpHeaders.HOST)).
-                    map(host -> StringUtils.substringBefore(host, 
":")).orElse(null);
-        }
+        return serverName;
     }
 
     @Override
     public int getServerPort() {
-        try {
-            return super.getServerPort();
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return Optional.ofNullable(getHeader(HttpHeaders.HOST)).
-                    map(host -> 
NumberUtils.toInt(StringUtils.substringAfter(host, ":"))).orElse(0);
-        }
+        return serverPort;
+    }
+
+    @Override
+    public String getContextPath() {
+        return contextPath;
+    }
+
+    @Override
+    public String getServletPath() {
+        return servletPath;
+    }
+
+    @Override
+    public String getPathInfo() {
+        return pathInfo;
+    }
+
+    @Override
+    public String getMethod() {
+        return batchItem.getMethod();
     }
 
     @Override
     public StringBuffer getRequestURL() {
-        return new StringBuffer(basePath).append(getRequestURI());
+        return new StringBuffer(baseURI).append(getRequestURI());
     }
 
     @Override
@@ -144,7 +180,7 @@ public class BatchItemRequest extends 
HttpServletRequestWrapper {
         int contentLength = 0;
         if (batchItem.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
             try {
-                contentLength = Integer.valueOf(
+                contentLength = Integer.parseInt(
                         
batchItem.getHeaders().get(HttpHeaders.CONTENT_LENGTH).get(0).toString());
             } catch (NumberFormatException e) {
                 LOG.error("Invalid value found for {}: {}",
@@ -161,74 +197,44 @@ public class BatchItemRequest extends 
HttpServletRequestWrapper {
 
     @Override
     public String getHeader(final String name) {
-        try {
-            return batchItem.getHeaders().containsKey(name)
-                    ? batchItem.getHeaders().get(name).get(0).toString()
-                    : HttpHeaders.CONTENT_TYPE.equals(name) || 
HttpHeaders.ACCEPT.equals(name)
-                    ? MediaType.ALL_VALUE
-                    : super.getHeader(name);
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return null;
-        }
+        return batchItem.getHeaders().containsKey(name)
+                ? batchItem.getHeaders().get(name).get(0).toString()
+                : HttpHeaders.CONTENT_TYPE.equals(name) || 
HttpHeaders.ACCEPT.equals(name)
+                ? MediaType.ALL_VALUE
+                : null;
     }
 
     @Override
     public Enumeration<String> getHeaders(final String name) {
-        try {
-            return batchItem.getHeaders().containsKey(name)
-                    ? 
Collections.enumeration(batchItem.getHeaders().get(name).stream().
-                            map(Object::toString).collect(Collectors.toList()))
-                    : HttpHeaders.CONTENT_TYPE.equals(name) || 
HttpHeaders.ACCEPT.equals(name)
-                    ? Collections.enumeration(List.of(MediaType.ALL_VALUE))
-                    : super.getHeaders(name);
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return Collections.emptyEnumeration();
-        }
+        return batchItem.getHeaders().containsKey(name)
+                ? 
Collections.enumeration(batchItem.getHeaders().get(name).stream().
+                        map(Object::toString).collect(Collectors.toList()))
+                : HttpHeaders.CONTENT_TYPE.equals(name) || 
HttpHeaders.ACCEPT.equals(name)
+                ? Collections.enumeration(List.of(MediaType.ALL_VALUE))
+                : Collections.emptyEnumeration();
     }
 
     @Override
     public Enumeration<String> getHeaderNames() {
-        try {
-            return super.getHeaderNames();
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-
-            Set<String> names = new HashSet<>(batchItem.getHeaders().keySet());
-            names.add(HttpHeaders.CONTENT_TYPE);
-            names.add(HttpHeaders.ACCEPT);
-            return Collections.enumeration(names);
-        }
+        Set<String> names = new HashSet<>(batchItem.getHeaders().keySet());
+        names.add(HttpHeaders.CONTENT_TYPE);
+        names.add(HttpHeaders.ACCEPT);
+        return Collections.enumeration(names);
     }
 
     @Override
     public Object getAttribute(final String name) {
-        try {
-            return super.getAttribute(name);
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return null;
-        }
+        return attributes.get(name);
     }
 
     @Override
-    public void setAttribute(final String name, final Object o) {
-        try {
-            super.setAttribute(name, o);
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-        }
+    public void setAttribute(final String name, final Object value) {
+        attributes.put(name, value);
     }
 
     @Override
     public String getCharacterEncoding() {
-        try {
-            return super.getCharacterEncoding();
-        } catch (Exception e) {
-            LOG.debug("While delegating to wrapped request", e);
-            return StandardCharsets.UTF_8.name();
-        }
+        return characterEncoding;
     }
 
     @Override
diff --git 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchProcess.java
 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchProcess.java
index 5b9e3fa61e..5e0bb61562 100644
--- 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchProcess.java
+++ 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/BatchProcess.java
@@ -47,7 +47,21 @@ public class BatchProcess implements Runnable {
 
     private String boundary;
 
-    private String basePath;
+    private String scheme;
+
+    private String serverName;
+
+    private int serverPort;
+
+    private String contextPath;
+
+    private String servletPath;
+
+    private String pathInfo;
+
+    private String characterEncoding;
+
+    private String baseURI;
 
     private List<BatchRequestItem> batchRequestItems;
 
@@ -63,8 +77,36 @@ public class BatchProcess implements Runnable {
         this.boundary = boundary;
     }
 
-    public void setBasePath(final String basePath) {
-        this.basePath = basePath;
+    public void setScheme(final String scheme) {
+        this.scheme = scheme;
+    }
+
+    public void setServerName(final String serverName) {
+        this.serverName = serverName;
+    }
+
+    public void setServerPort(final int serverPort) {
+        this.serverPort = serverPort;
+    }
+
+    public void setContextPath(final String contextPath) {
+        this.contextPath = contextPath;
+    }
+
+    public void setServletPath(final String servletPath) {
+        this.servletPath = servletPath;
+    }
+
+    public void setPathInfo(final String pathInfo) {
+        this.pathInfo = pathInfo;
+    }
+
+    public void setCharacterEncoding(final String characterEncoding) {
+        this.characterEncoding = characterEncoding;
+    }
+
+    public void setBaseURI(final String baseURI) {
+        this.baseURI = baseURI;
     }
 
     public void setBatchRequestItems(final List<BatchRequestItem> 
batchRequestItems) {
@@ -107,7 +149,9 @@ public class BatchProcess implements Runnable {
             if (dest == null) {
                 resItem.setStatus(HttpServletResponse.SC_NOT_FOUND);
             } else {
-                BatchItemRequest request = new BatchItemRequest(basePath, 
servletRequest, reqItem);
+                BatchItemRequest request = new BatchItemRequest(
+                        scheme, serverName, serverPort, contextPath, 
servletPath, pathInfo, characterEncoding,
+                        baseURI, servletRequest, reqItem);
                 BatchItemResponse response = new BatchItemResponse();
                 try {
                     dest.invoke(servletConfig, 
servletConfig.getServletContext(), request, response);
diff --git 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/SyncopeServiceImpl.java
 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/SyncopeServiceImpl.java
index 9b4e4e3cdc..444223eb6c 100644
--- 
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/SyncopeServiceImpl.java
+++ 
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/SyncopeServiceImpl.java
@@ -143,7 +143,14 @@ public class SyncopeServiceImpl extends AbstractService 
implements SyncopeServic
 
         BatchProcess batchProcess = 
ApplicationContextProvider.getBeanFactory().createBean(BatchProcess.class);
         batchProcess.setBoundary(boundary);
-        batchProcess.setBasePath(uriInfo.getBaseUri().toASCIIString());
+        
batchProcess.setScheme(messageContext.getHttpServletRequest().getScheme());
+        
batchProcess.setServerName(messageContext.getHttpServletRequest().getServerName());
+        
batchProcess.setServerPort(messageContext.getHttpServletRequest().getServerPort());
+        
batchProcess.setContextPath(messageContext.getHttpServletRequest().getContextPath());
+        
batchProcess.setServletPath(messageContext.getHttpServletRequest().getServletPath());
+        
batchProcess.setPathInfo(messageContext.getHttpServletRequest().getPathInfo());
+        
batchProcess.setCharacterEncoding(messageContext.getHttpServletRequest().getCharacterEncoding());
+        batchProcess.setBaseURI(uriInfo.getBaseUri().toASCIIString());
         batchProcess.setBatchRequestItems(batchRequestItems);
         
batchProcess.setDestinationRegistry(getDestinationRegistryFromBusOrDefault());
         batchProcess.setServletConfig(messageContext.getServletConfig());
diff --git a/pom.xml b/pom.xml
index eaebf2a8e3..ecc72c9702 100644
--- a/pom.xml
+++ b/pom.xml
@@ -498,7 +498,7 @@ under the License.
     <cargo.rmi.port>9805</cargo.rmi.port>
     <cargo.deployable.ping.timeout>60000</cargo.deployable.ping.timeout>
 
-    <tomcat.version>9.0.89</tomcat.version>
+    <tomcat.version>9.0.90</tomcat.version>
     <wildfly.version>26.1.3.Final</wildfly.version>
     <payara.version>5.2022.5</payara.version>
     <javax.faces.version>2.3.14</javax.faces.version>

Reply via email to