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.common.rest.api.batch;
20  
21  import java.util.List;
22  import javax.ws.rs.core.HttpHeaders;
23  import javax.ws.rs.core.Response;
24  import org.apache.syncope.common.rest.api.service.JAXRSService;
25  
26  public final class BatchPayloadGenerator {
27  
28      private static final String HTTP_1_1 = "HTTP/1.1";
29  
30      public static <T extends BatchItem> String generate(final List<T> items, final String boundary) {
31          StringBuilder payload = new StringBuilder();
32  
33          items.forEach(item -> {
34              payload.append(boundary).append(JAXRSService.CRLF);
35              payload.append(HttpHeaders.CONTENT_TYPE).append(": ").append("application/http").append('\n');
36              payload.append("Content-Transfer-Encoding: binary").append('\n');
37              payload.append(JAXRSService.CRLF);
38  
39              if (item instanceof BatchRequestItem) {
40                  BatchRequestItem bri = BatchRequestItem.class.cast(item);
41                  payload.append(bri.getMethod()).append(' ').append(bri.getRequestURI());
42                  if (bri.getQueryString() != null) {
43                      payload.append('?').append(bri.getQueryString());
44                  }
45                  payload.append(' ').append(HTTP_1_1).append('\n');
46              }
47  
48              if (item instanceof BatchResponseItem) {
49                  BatchResponseItem bri = BatchResponseItem.class.cast(item);
50                  payload.append(HTTP_1_1).append(' ').
51                          append(bri.getStatus()).append(' ').
52                          append(Response.Status.fromStatusCode(bri.getStatus()).getReasonPhrase()).
53                          append('\n');
54              }
55  
56              if (item.getHeaders() != null && !item.getHeaders().isEmpty()) {
57                  item.getHeaders().forEach((key, values) -> values.forEach(
58                          value -> payload.append(key).append(": ").append(value).append('\n')));
59                  payload.append(JAXRSService.CRLF);
60              }
61  
62              if (item.getContent() != null) {
63                  payload.append(item.getContent()).append('\n');
64              }
65          });
66  
67          payload.append(boundary).append(JAXRSService.DOUBLE_DASH).append('\n');
68  
69          return payload.toString();
70      }
71  
72      private BatchPayloadGenerator() {
73          // private constructor for static utility class
74      }
75  }