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
The following commit(s) were added to refs/heads/master by this push:
new e58cbd37cc Make BatchItemRequest more independent from incoming
HttpServletRequest
e58cbd37cc is described below
commit e58cbd37cc7ec2f3ab25f9f3860418b381b51320
Author: Francesco Chicchiriccò <[email protected]>
AuthorDate: Fri Jun 21 12:22:14 2024 +0200
Make BatchItemRequest more independent from incoming HttpServletRequest
---
.../core/rest/cxf/batch/BatchItemRequest.java | 107 ++++++++------
.../syncope/core/rest/cxf/batch/BatchProcess.java | 64 +++++++--
.../cxf/batch/CommonBatchHttpServletRequest.java | 154 ---------------------
.../core/rest/cxf/service/SyncopeServiceImpl.java | 9 +-
4 files changed, 128 insertions(+), 206 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 599ee21747..7f88c42b4f 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,14 +20,19 @@ package org.apache.syncope.core.rest.cxf.batch;
import jakarta.servlet.ReadListener;
import jakarta.servlet.ServletInputStream;
+import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.ws.rs.core.HttpHeaders;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.security.Principal;
import java.util.Collections;
import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,22 +42,49 @@ public class BatchItemRequest extends
HttpServletRequestWrapper {
private static final Logger LOG =
LoggerFactory.getLogger(BatchItemRequest.class);
- private final CommonBatchHttpServletRequest commonRequest;
+ private final String scheme;
- private final String basePath;
+ 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 CommonBatchHttpServletRequest commonRequest,
- 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(commonRequest.getServletRequest());
- this.commonRequest = commonRequest;
- this.basePath = basePath;
+ super(request);
+ 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() {
@@ -87,48 +119,43 @@ public class BatchItemRequest extends
HttpServletRequestWrapper {
}
@Override
- public String getContextPath() {
- return commonRequest.getContextPath();
+ public String getScheme() {
+ return scheme;
}
@Override
- public String getServletPath() {
- return commonRequest.getServletPath();
- }
-
- @Override
- public String getPathInfo() {
- return commonRequest.getPathInfo();
+ public String getServerName() {
+ return serverName;
}
@Override
- public Principal getUserPrincipal() {
- return commonRequest.getUserPrincipal();
+ public int getServerPort() {
+ return serverPort;
}
@Override
- public boolean isUserInRole(final String role) {
- return false;
+ public String getContextPath() {
+ return contextPath;
}
@Override
- public String getMethod() {
- return batchItem.getMethod();
+ public String getServletPath() {
+ return servletPath;
}
@Override
- public String getServerName() {
- return commonRequest.getServerName();
+ public String getPathInfo() {
+ return pathInfo;
}
@Override
- public int getServerPort() {
- return commonRequest.getServerPort();
+ public String getMethod() {
+ return batchItem.getMethod();
}
@Override
public StringBuffer getRequestURL() {
- return new StringBuffer(basePath).append(getRequestURI());
+ return new StringBuffer(baseURI).append(getRequestURI());
}
@Override
@@ -174,42 +201,40 @@ public class BatchItemRequest extends
HttpServletRequestWrapper {
? batchItem.getHeaders().get(name).get(0).toString()
: HttpHeaders.CONTENT_TYPE.equals(name) ||
HttpHeaders.ACCEPT.equals(name)
? MediaType.ALL_VALUE
- : commonRequest.getHeader(name);
+ : null;
}
@Override
public Enumeration<String> getHeaders(final String name) {
return batchItem.getHeaders().containsKey(name)
?
Collections.enumeration(batchItem.getHeaders().get(name).stream().
- map(Object::toString).toList())
+ map(Object::toString).collect(Collectors.toList()))
: HttpHeaders.CONTENT_TYPE.equals(name) ||
HttpHeaders.ACCEPT.equals(name)
? Collections.enumeration(List.of(MediaType.ALL_VALUE))
- : commonRequest.getHeaders(name);
+ : Collections.emptyEnumeration();
}
@Override
public Enumeration<String> getHeaderNames() {
- return commonRequest.getHeaderNames();
+ 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) {
- return commonRequest.getAttribute(name);
- }
-
- @Override
- public void setAttribute(final String name, final Object o) {
- commonRequest.setAttribute(name, o);
+ return attributes.get(name);
}
@Override
- public Enumeration<String> getAttributeNames() {
- return commonRequest.getAttributeNames();
+ public void setAttribute(final String name, final Object value) {
+ attributes.put(name, value);
}
@Override
public String getCharacterEncoding() {
- return commonRequest.getCharacterEncoding();
+ 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 58b76ae54f..8a21ba32ac 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
@@ -24,7 +24,6 @@ import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Optional;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.cxf.transport.http.DestinationRegistry;
import org.apache.syncope.common.rest.api.batch.BatchPayloadGenerator;
@@ -47,7 +46,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;
@@ -55,7 +68,7 @@ public class BatchProcess implements Runnable {
private ServletConfig servletConfig;
- private CommonBatchHttpServletRequest commonRequest;
+ private HttpServletRequest servletRequest;
private Authentication authentication;
@@ -63,8 +76,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) {
@@ -80,7 +121,7 @@ public class BatchProcess implements Runnable {
}
public void setServletRequest(final HttpServletRequest servletRequest) {
- this.commonRequest = new CommonBatchHttpServletRequest(servletRequest);
+ this.servletRequest = servletRequest;
}
public void setAuthentication(final Authentication authentication) {
@@ -96,9 +137,10 @@ public class BatchProcess implements Runnable {
batchRequestItems.forEach(reqItem -> {
LOG.debug("Batch Request item:\n{}", reqItem);
- AbstractHTTPDestination dest =
-
Optional.ofNullable(destinationRegistry.getDestinationForPath(reqItem.getRequestURI(),
true)).
- orElseGet(() ->
destinationRegistry.checkRestfulRequest(reqItem.getRequestURI()));
+ AbstractHTTPDestination dest =
destinationRegistry.getDestinationForPath(reqItem.getRequestURI(), true);
+ if (dest == null) {
+ dest =
destinationRegistry.checkRestfulRequest(reqItem.getRequestURI());
+ }
LOG.debug("Destination found for {}: {}", reqItem.getRequestURI(),
dest);
BatchResponseItem resItem = new BatchResponseItem();
@@ -106,7 +148,9 @@ public class BatchProcess implements Runnable {
if (dest == null) {
resItem.setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
- BatchItemRequest request = new BatchItemRequest(commonRequest,
basePath, 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/batch/CommonBatchHttpServletRequest.java
b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/CommonBatchHttpServletRequest.java
deleted file mode 100644
index 8a94de581a..0000000000
---
a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/batch/CommonBatchHttpServletRequest.java
+++ /dev/null
@@ -1,154 +0,0 @@
-/*
- * 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.apache.syncope.core.rest.cxf.batch;
-
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.ws.rs.core.HttpHeaders;
-import java.security.Principal;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.TreeMap;
-import org.apache.cxf.helpers.HttpHeaderHelper;
-
-public class CommonBatchHttpServletRequest {
-
- private final HttpServletRequest servletRequest;
-
- private final String serverName;
-
- private final int serverPort;
-
- private final String contextPath;
-
- private final String servletPath;
-
- private final String pathInfo;
-
- private final Principal userPrincipal;
-
- private final String characterEncoding;
-
- private final Map<String, List<String>> headers = new
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-
- private final Map<String, Object> attributes = new
TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-
- public CommonBatchHttpServletRequest(final HttpServletRequest
servletRequest) {
- this.servletRequest = servletRequest;
-
- serverName = servletRequest.getServerName();
- serverPort = servletRequest.getServerPort();
- contextPath = servletRequest.getContextPath();
- servletPath = servletRequest.getServletPath();
- pathInfo = servletRequest.getPathInfo();
- userPrincipal = servletRequest.getUserPrincipal();
- characterEncoding = servletRequest.getCharacterEncoding();
-
- for (Enumeration<String> ehn = servletRequest.getHeaderNames();
ehn.hasMoreElements();) {
- String fname = ehn.nextElement();
- String mappedName = HttpHeaderHelper.getHeaderKey(fname);
-
- List<String> values =
Optional.ofNullable(headers.get(mappedName)).orElseGet(() -> {
- List<String> v = new ArrayList<>();
- headers.put(mappedName, v);
- return v;
- });
-
- for (Enumeration<String> eh = servletRequest.getHeaders(fname);
eh.hasMoreElements();) {
- String value = eh.nextElement();
- if (HttpHeaders.ACCEPT.equals(mappedName) &&
!values.isEmpty()) {
- //ensure we collapse Accept into first line
- String firstAccept = values.get(0);
- firstAccept = firstAccept + ", " + value;
- values.set(0, firstAccept);
- }
- values.add(value);
- }
- }
-
- for (Enumeration<String> ea = servletRequest.getAttributeNames();
ea.hasMoreElements();) {
- String aname = ea.nextElement();
- attributes.put(aname, servletRequest.getAttribute(aname));
- }
- }
-
- public HttpServletRequest getServletRequest() {
- return servletRequest;
- }
-
- public String getServerName() {
- return serverName;
- }
-
- public int getServerPort() {
- return serverPort;
- }
-
- public String getContextPath() {
- return contextPath;
- }
-
- public String getServletPath() {
- return servletPath;
- }
-
- public String getPathInfo() {
- return pathInfo;
- }
-
- public Principal getUserPrincipal() {
- return userPrincipal;
- }
-
- public String getCharacterEncoding() {
- return characterEncoding;
- }
-
- public Enumeration<String> getHeaderNames() {
- return Collections.enumeration(headers.keySet());
- }
-
- public String getHeader(final String name) {
- return Optional.ofNullable(headers.get(name)).
- filter(v -> !v.isEmpty()).
- map(v -> v.get(0)).
- orElse(null);
- }
-
- public Enumeration<String> getHeaders(final String name) {
- return headers.containsKey(name)
- ? Collections.enumeration(headers.get(name))
- : Collections.emptyEnumeration();
- }
-
- public Enumeration<String> getAttributeNames() {
- return Collections.enumeration(attributes.keySet());
- }
-
- public Object getAttribute(final String name) {
- return attributes.get(name);
- }
-
- public void setAttribute(final String name, final Object o) {
- attributes.put(name, o);
- }
-}
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 3a3af2b3ac..3bd1938fb6 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
@@ -144,7 +144,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());