View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }