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 68d14f6333 A few Batch processing improvements (#337) 68d14f6333 is described below commit 68d14f63337d0595c4e238a5c9741ee58b5435be Author: Francesco Chicchiriccò <ilgro...@users.noreply.github.com> AuthorDate: Sun Apr 10 19:07:23 2022 +0200 A few Batch processing improvements (#337) --- .../syncope/common/rest/api/batch/BatchItem.java | 4 ++-- .../common/rest/api/batch/BatchRequestItem.java | 11 +++++++++++ .../common/rest/api/batch/BatchResponseItem.java | 9 +++++++++ .../core/rest/cxf/RestServiceExceptionMapper.java | 16 +++++++++------- .../syncope/core/rest/cxf/batch/BatchProcess.java | 22 +++++++++++----------- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchItem.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchItem.java index 3a0bc6833e..2c3c2a6f49 100644 --- a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchItem.java +++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchItem.java @@ -29,9 +29,9 @@ public abstract class BatchItem implements Serializable { private static final long serialVersionUID = -1393976266651766259L; - private final Map<String, List<Object>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + protected final Map<String, List<Object>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - private String content; + protected String content; public Map<String, List<Object>> getHeaders() { return headers; diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchRequestItem.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchRequestItem.java index 834b0fe8d9..5a9739a62f 100644 --- a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchRequestItem.java +++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchRequestItem.java @@ -51,4 +51,15 @@ public class BatchRequestItem extends BatchItem { public void setQueryString(final String queryString) { this.queryString = queryString; } + + @Override + public String toString() { + return "BatchRequestItem{" + + "method=" + method + " ," + + "requestURI=" + requestURI + " ," + + "queryString=" + queryString + " ," + + "headers=" + headers + ", " + + "content=" + content + " ," + + '}'; + } } diff --git a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchResponseItem.java b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchResponseItem.java index e989b65712..8c6f902d41 100644 --- a/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchResponseItem.java +++ b/common/idrepo/rest-api/src/main/java/org/apache/syncope/common/rest/api/batch/BatchResponseItem.java @@ -31,4 +31,13 @@ public class BatchResponseItem extends BatchItem { public void setStatus(final int status) { this.status = status; } + + @Override + public String toString() { + return "BatchResponseItem{" + + "status=" + status + ", " + + "headers=" + headers + ", " + + "content=" + content + + '}'; + } } diff --git a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java index 71243cc955..189c98d538 100644 --- a/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java +++ b/core/idrepo/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/RestServiceExceptionMapper.java @@ -29,6 +29,7 @@ import javax.persistence.EntityExistsException; import javax.persistence.PersistenceException; import javax.persistence.RollbackException; import javax.validation.ValidationException; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @@ -129,17 +130,18 @@ public class RestServiceExceptionMapper implements ExceptionMapper<Exception> { header(RESTHeaders.ERROR_CODE, ClientExceptionType.RESTValidation.name()). header(RESTHeaders.ERROR_INFO, ClientExceptionType.RESTValidation.getInfoHeaderValue( ExceptionUtils.getRootCauseMessage(ex))); - - ErrorTO error = new ErrorTO(); - error.setStatus(ClientExceptionType.RESTValidation.getResponseStatus().getStatusCode()); - error.setType(ClientExceptionType.RESTValidation); - error.getElements().add(ExceptionUtils.getRootCauseMessage(ex)); - - builder.entity(error); + } + // process web application exceptions + if (builder == null && ex instanceof WebApplicationException) { + builder = builder(((WebApplicationException) ex).getResponse()). + header(RESTHeaders.ERROR_CODE, ClientExceptionType.Unknown.name()). + header(RESTHeaders.ERROR_INFO, ClientExceptionType.Unknown.getInfoHeaderValue( + ExceptionUtils.getRootCauseMessage(ex))); } // ...or just report as InternalServerError if (builder == null) { builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR). + header(RESTHeaders.ERROR_CODE, ClientExceptionType.Unknown.name()). header(RESTHeaders.ERROR_INFO, ClientExceptionType.Unknown.getInfoHeaderValue( ExceptionUtils.getRootCauseMessage(ex))); 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 2c130956b6..5b9e3fa61e 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 @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.syncope.common.rest.api.batch.BatchPayloadGenerator; @@ -93,7 +94,7 @@ public class BatchProcess implements Runnable { List<BatchResponseItem> batchResponseItems = new ArrayList<>(batchRequestItems.size()); batchRequestItems.forEach(reqItem -> { - LOG.debug("Batch item:\n{}", reqItem); + LOG.debug("Batch Request item:\n{}", reqItem); AbstractHTTPDestination dest = destinationRegistry.getDestinationForPath(reqItem.getRequestURI(), true); if (dest == null) { @@ -101,34 +102,33 @@ public class BatchProcess implements Runnable { } LOG.debug("Destination found for {}: {}", reqItem.getRequestURI(), dest); + BatchResponseItem resItem = new BatchResponseItem(); + batchResponseItems.add(resItem); if (dest == null) { - BatchResponseItem resItem = new BatchResponseItem(); - resItem.setStatus(404); - batchResponseItems.add(resItem); + resItem.setStatus(HttpServletResponse.SC_NOT_FOUND); } else { BatchItemRequest request = new BatchItemRequest(basePath, servletRequest, reqItem); BatchItemResponse response = new BatchItemResponse(); try { dest.invoke(servletConfig, servletConfig.getServletContext(), request, response); - LOG.debug("Returned:\nstatus: {}\nheaders: {}\nbody:\n{}", response.getStatus(), - response.getHeaders(), new String(response.getUnderlyingOutputStream().toByteArray())); - BatchResponseItem resItem = new BatchResponseItem(); resItem.setStatus(response.getStatus()); resItem.setHeaders(response.getHeaders()); String output = new String(response.getUnderlyingOutputStream().toByteArray()); if (output.length() > 0) { resItem.setContent(output); } - batchResponseItems.add(resItem); + + LOG.debug("Returned:\nstatus: {}\nheaders: {}\nbody:\n{}", + response.getStatus(), response.getHeaders(), output); } catch (IOException e) { LOG.error("Invocation of {} failed", dest.getPath(), e); - BatchResponseItem resItem = new BatchResponseItem(); - resItem.setStatus(404); - batchResponseItems.add(resItem); + resItem.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } + + LOG.debug("Batch Response item:\n{}", resItem); }); String results = BatchPayloadGenerator.generate(batchResponseItems, JAXRSService.DOUBLE_DASH + boundary);