1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.rest.cxf.batch;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.servlet.ServletConfig;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import org.apache.cxf.transport.http.AbstractHTTPDestination;
28 import org.apache.cxf.transport.http.DestinationRegistry;
29 import org.apache.syncope.common.rest.api.batch.BatchPayloadGenerator;
30 import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
31 import org.apache.syncope.common.rest.api.batch.BatchResponseItem;
32 import org.apache.syncope.common.rest.api.service.JAXRSService;
33 import org.apache.syncope.core.persistence.api.dao.BatchDAO;
34 import org.apache.syncope.core.persistence.api.entity.Batch;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.security.core.Authentication;
39 import org.springframework.security.core.context.SecurityContextHolder;
40
41 public class BatchProcess implements Runnable {
42
43 private static final Logger LOG = LoggerFactory.getLogger(BatchProcess.class);
44
45 @Autowired
46 private BatchDAO batchDAO;
47
48 private String boundary;
49
50 private String basePath;
51
52 private List<BatchRequestItem> batchRequestItems;
53
54 private DestinationRegistry destinationRegistry;
55
56 private ServletConfig servletConfig;
57
58 private HttpServletRequest servletRequest;
59
60 private Authentication authentication;
61
62 public void setBoundary(final String boundary) {
63 this.boundary = boundary;
64 }
65
66 public void setBasePath(final String basePath) {
67 this.basePath = basePath;
68 }
69
70 public void setBatchRequestItems(final List<BatchRequestItem> batchRequestItems) {
71 this.batchRequestItems = batchRequestItems;
72 }
73
74 public void setDestinationRegistry(final DestinationRegistry destinationRegistry) {
75 this.destinationRegistry = destinationRegistry;
76 }
77
78 public void setServletConfig(final ServletConfig servletConfig) {
79 this.servletConfig = servletConfig;
80 }
81
82 public void setServletRequest(final HttpServletRequest servletRequest) {
83 this.servletRequest = servletRequest;
84 }
85
86 public void setAuthentication(final Authentication authentication) {
87 this.authentication = authentication;
88 }
89
90 @Override
91 public void run() {
92 SecurityContextHolder.getContext().setAuthentication(authentication);
93
94 List<BatchResponseItem> batchResponseItems = new ArrayList<>(batchRequestItems.size());
95
96 batchRequestItems.forEach(reqItem -> {
97 LOG.debug("Batch Request item:\n{}", reqItem);
98
99 AbstractHTTPDestination dest = destinationRegistry.getDestinationForPath(reqItem.getRequestURI(), true);
100 if (dest == null) {
101 dest = destinationRegistry.checkRestfulRequest(reqItem.getRequestURI());
102 }
103 LOG.debug("Destination found for {}: {}", reqItem.getRequestURI(), dest);
104
105 BatchResponseItem resItem = new BatchResponseItem();
106 batchResponseItems.add(resItem);
107 if (dest == null) {
108 resItem.setStatus(HttpServletResponse.SC_NOT_FOUND);
109 } else {
110 BatchItemRequest request = new BatchItemRequest(basePath, servletRequest, reqItem);
111 BatchItemResponse response = new BatchItemResponse();
112 try {
113 dest.invoke(servletConfig, servletConfig.getServletContext(), request, response);
114
115 resItem.setStatus(response.getStatus());
116 resItem.setHeaders(response.getHeaders());
117 String output = new String(response.getUnderlyingOutputStream().toByteArray());
118 if (output.length() > 0) {
119 resItem.setContent(output);
120 }
121
122 LOG.debug("Returned:\nstatus: {}\nheaders: {}\nbody:\n{}",
123 response.getStatus(), response.getHeaders(), output);
124 } catch (IOException e) {
125 LOG.error("Invocation of {} failed", dest.getPath(), e);
126
127 resItem.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
128 }
129 }
130
131 LOG.debug("Batch Response item:\n{}", resItem);
132 });
133
134 String results = BatchPayloadGenerator.generate(batchResponseItems, JAXRSService.DOUBLE_DASH + boundary);
135
136 Batch batch = batchDAO.find(boundary);
137 if (batch == null) {
138 LOG.error("Could not find batch {}, cannot save results hence reporting here:\n{}", boundary, results);
139 } else {
140 batch.setResults(results);
141 batchDAO.save(batch);
142 }
143 }
144 }