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.net.URI;
22  import java.nio.charset.StandardCharsets;
23  import java.util.List;
24  import java.util.Map;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.cxf.jaxrs.client.ClientProxyImpl;
27  import org.apache.cxf.jaxrs.client.ClientState;
28  import org.apache.cxf.jaxrs.model.ClassResourceInfo;
29  import org.apache.cxf.message.Message;
30  import org.apache.syncope.common.rest.api.batch.BatchRequestItem;
31  
32  public class BatchClientProxyImpl extends ClientProxyImpl {
33  
34      private final BatchClientFactoryBean factory;
35  
36      public BatchClientProxyImpl(
37              final BatchClientFactoryBean factory,
38              final URI baseURI,
39              final ClassLoader loader,
40              final ClassResourceInfo cri,
41              final boolean isRoot,
42              final boolean inheritHeaders,
43              final Object... varValues) {
44  
45          super(baseURI, loader, cri, isRoot, inheritHeaders, varValues);
46          this.factory = factory;
47      }
48  
49      public BatchClientProxyImpl(
50              final BatchClientFactoryBean factory,
51              final ClientState initialState,
52              final ClassLoader loader,
53              final ClassResourceInfo cri,
54              final boolean isRoot,
55              final boolean inheritHeaders,
56              final Object... varValues) {
57  
58          super(initialState, loader, cri, isRoot, inheritHeaders, varValues);
59          this.factory = factory;
60      }
61  
62      @Override
63      @SuppressWarnings("unchecked")
64      protected Object[] preProcessResult(final Message message) throws Exception {
65          BatchRequestItem bri = new BatchRequestItem();
66          bri.setMethod((String) message.get(Message.HTTP_REQUEST_METHOD));
67          bri.setRequestURI(StringUtils.substringAfter(
68                  (String) message.getContextualProperty(Message.REQUEST_URI),
69                  getState().getBaseURI().toASCIIString()));
70          bri.setHeaders((Map<String, List<Object>>) message.get(Message.PROTOCOL_HEADERS));
71  
72          BatchOfflineHTTPConduit conduit = (BatchOfflineHTTPConduit) message.getExchange().getConduit(message);
73          bri.setContent(conduit.getOutputStream().toString(StandardCharsets.UTF_8));
74  
75          factory.add(bri);
76          return null;
77      }
78  
79      @Override
80      protected Object handleResponse(final Message outMessage, final Class<?> serviceCls) throws Throwable {
81          return null;
82      }
83  }