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.client.lib.batch;
20  
21  import java.io.IOException;
22  import java.net.URI;
23  import java.util.ArrayList;
24  import java.util.List;
25  import org.apache.cxf.endpoint.ConduitSelector;
26  import org.apache.cxf.endpoint.Endpoint;
27  import org.apache.cxf.endpoint.UpfrontConduitSelector;
28  import org.apache.cxf.jaxrs.client.ClientProxyImpl;
29  import org.apache.cxf.jaxrs.client.ClientState;
30  import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
31  import org.apache.cxf.jaxrs.model.ClassResourceInfo;
32  import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
33  
34  public class BatchClientFactoryBean extends JAXRSClientFactoryBean {
35  
36      private final List<BatchRequestItem> batchRequestItems = new ArrayList<>();
37  
38      @Override
39      protected ConduitSelector getConduitSelector(final Endpoint ep) {
40          ConduitSelector cs = getConduitSelector();
41          if (cs == null) {
42              try {
43                  cs = new UpfrontConduitSelector(new BatchOfflineHTTPConduit(bus, ep.getEndpointInfo()));
44              } catch (IOException e) {
45                  throw new IllegalArgumentException("Could not create " + BatchOfflineHTTPConduit.class.getName(), e);
46              }
47          }
48          cs.setEndpoint(ep);
49          return cs;
50      }
51  
52      @Override
53      protected ClientProxyImpl createClientProxy(
54              final ClassResourceInfo cri,
55              final boolean isRoot,
56              final ClientState actualState,
57              final Object[] varValues) {
58  
59          if (actualState == null) {
60              return new BatchClientProxyImpl(
61                      this, URI.create(getAddress()), proxyLoader, cri, isRoot, inheritHeaders, varValues);
62          } else {
63              return new BatchClientProxyImpl(
64                      this, actualState, proxyLoader, cri, isRoot, inheritHeaders, varValues);
65          }
66      }
67  
68      public boolean add(final BatchRequestItem item) {
69          return this.batchRequestItems.add(item);
70      }
71  
72      public List<BatchRequestItem> getBatchRequestItems() {
73          return batchRequestItems;
74      }
75  }