private void initBatchClientFactoryBean() { this.bcfb = new BatchClientFactoryBean(); this.bcfb.setAddress(address); this.bcfb.setProviders(providers); }
/** * Sends the current request, with items accumulated by invoking methods on proxies obtained via * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for synchronous response. * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests. * * @return batch response */ public BatchResponse commit() { return commit(false); }
public BatchRequest( final MediaType mediaType, final String address, final List<?> providers, final String jwt) { this.mediaType = mediaType; this.jwt = jwt; this.address = address; this.providers = providers; initBatchClientFactoryBean(); }
/** * Sends the current request, with items accumulated by invoking methods on proxies obtained via * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for a synchronous or asynchronous * response, depending on the {@code async} parameter. * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests. * * @param async whether asynchronous Batch process is requested, or not * @return batch response */ public BatchResponse commit(final boolean async) { String boundary = "--batch_" + UUID.randomUUID().toString(); WebClient webClient = WebClient.create(bcfb.getAddress()).path("batch"). header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt). type(RESTHeaders.multipartMixedWith(boundary.substring(2))); if (async) { webClient.header(RESTHeaders.PREFER, Preference.RESPOND_ASYNC); } String body = BatchPayloadGenerator.generate(bcfb.getBatchRequestItems(), boundary); LOG.debug("Batch request body:\n{}", body); initBatchClientFactoryBean(); return new BatchResponse(boundary, jwt, webClient.post(body)); } }
@Override @SuppressWarnings("unchecked") protected Object[] preProcessResult(final Message message) throws Exception { BatchRequestItem bri = new BatchRequestItem(); bri.setMethod((String) message.get(Message.HTTP_REQUEST_METHOD)); bri.setRequestURI(StringUtils.substringAfter( (String) message.getContextualProperty(Message.REQUEST_URI), getState().getBaseURI().toASCIIString())); bri.setHeaders((Map<String, List<Object>>) message.get(Message.PROTOCOL_HEADERS)); BatchOfflineHTTPConduit conduit = (BatchOfflineHTTPConduit) message.getExchange().getConduit(message); bri.setContent(conduit.getOutputStream().toString(StandardCharsets.UTF_8.name())); factory.add(bri); return null; }
@Override protected ClientProxyImpl createClientProxy( final ClassResourceInfo cri, final boolean isRoot, final ClientState actualState, final Object[] varValues) { if (actualState == null) { return new BatchClientProxyImpl( this, URI.create(getAddress()), proxyLoader, cri, isRoot, inheritHeaders, varValues); } else { return new BatchClientProxyImpl( this, actualState, proxyLoader, cri, isRoot, inheritHeaders, varValues); } }
public <T> T getService(final Class<T> serviceClass) { bcfb.setServiceClass(serviceClass); T serviceInstance = bcfb.create(serviceClass); Client client = WebClient.client(serviceInstance); client.type(mediaType).accept(mediaType); return serviceInstance; }
@Override protected ConduitSelector getConduitSelector(final Endpoint ep) { ConduitSelector cs = getConduitSelector(); if (cs == null) { try { cs = new UpfrontConduitSelector(new BatchOfflineHTTPConduit(bus, ep.getEndpointInfo())); } catch (IOException e) { throw new IllegalArgumentException("Could not create " + BatchOfflineHTTPConduit.class.getName(), e); } } cs.setEndpoint(ep); return cs; }
public List<BatchRequestItem> getItems() { return bcfb.getBatchRequestItems(); }
/** * Initiates a new Batch request. * * The typical operation flow is: * <pre> * BatchRequest batchRequest = syncopeClient.batch(); * batchRequest.getService(UserService.class).create(...); * batchRequest.getService(UserService.class).update(...); * batchRequest.getService(GroupService.class).update(...); * batchRequest.getService(GroupService.class).delete(...); * ... * BatchResponse batchResponse = batchRequest().commit(); * List<BatchResponseItem> items = batchResponse.getItems() * </pre> * * @return empty Batch request */ public BatchRequest batch() { return new BatchRequest( mediaType, restClientFactory.getAddress(), restClientFactory.getProviders(), getJWT()); } }
/** * Sends the current request, with items accumulated by invoking methods on proxies obtained via * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for a synchronous or asynchronous * response, depending on the {@code async} parameter. * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests. * * @param async whether asynchronous Batch process is requested, or not * @return batch response */ public BatchResponse commit(final boolean async) { String boundary = "--batch_" + UUID.randomUUID().toString(); WebClient webClient = WebClient.create(bcfb.getAddress()).path("batch"). header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt). type(RESTHeaders.multipartMixedWith(boundary.substring(2))); if (async) { webClient.header(RESTHeaders.PREFER, Preference.RESPOND_ASYNC); } String body = BatchPayloadGenerator.generate(bcfb.getBatchRequestItems(), boundary); LOG.debug("Batch request body:\n{}", body); initBatchClientFactoryBean(); return new BatchResponse(boundary, jwt, webClient.post(body)); } }
private void initBatchClientFactoryBean() { this.bcfb = new BatchClientFactoryBean(); this.bcfb.setAddress(address); this.bcfb.setProviders(providers); }
@Override @SuppressWarnings("unchecked") protected Object[] preProcessResult(final Message message) throws Exception { BatchRequestItem bri = new BatchRequestItem(); bri.setMethod((String) message.get(Message.HTTP_REQUEST_METHOD)); bri.setRequestURI(StringUtils.substringAfter( (String) message.getContextualProperty(Message.REQUEST_URI), getState().getBaseURI().toASCIIString())); bri.setHeaders((Map<String, List<Object>>) message.get(Message.PROTOCOL_HEADERS)); BatchOfflineHTTPConduit conduit = (BatchOfflineHTTPConduit) message.getExchange().getConduit(message); bri.setContent(conduit.getOutputStream().toString(StandardCharsets.UTF_8)); factory.add(bri); return null; }
@Override protected ClientProxyImpl createClientProxy( final ClassResourceInfo cri, final boolean isRoot, final ClientState actualState, final Object[] varValues) { if (actualState == null) { return new BatchClientProxyImpl( this, URI.create(getAddress()), proxyLoader, cri, isRoot, inheritHeaders, varValues); } else { return new BatchClientProxyImpl( this, actualState, proxyLoader, cri, isRoot, inheritHeaders, varValues); } }
public <T> T getService(final Class<T> serviceClass) { bcfb.setServiceClass(serviceClass); T serviceInstance = bcfb.create(serviceClass); Client client = WebClient.client(serviceInstance); client.type(mediaType).accept(mediaType); return serviceInstance; }
@Override protected ConduitSelector getConduitSelector(final Endpoint ep) { ConduitSelector cs = getConduitSelector(); if (cs == null) { try { cs = new UpfrontConduitSelector(new BatchOfflineHTTPConduit(bus, ep.getEndpointInfo())); } catch (IOException e) { throw new IllegalArgumentException("Could not create " + BatchOfflineHTTPConduit.class.getName(), e); } } cs.setEndpoint(ep); return cs; }
/** * Sends the current request, with items accumulated by invoking methods on proxies obtained via * {@link #getService(java.lang.Class)}, to the Batch service, and awaits for synchronous response. * It also clears out the accumulated items, in case of reuse of this instance for subsequent requests. * * @return batch response */ public BatchResponse commit() { return commit(false); }
public BatchRequest( final MediaType mediaType, final String address, final List<?> providers, final String jwt) { this.mediaType = mediaType; this.jwt = jwt; this.address = address; this.providers = providers; initBatchClientFactoryBean(); }
public List<BatchRequestItem> getItems() { return bcfb.getBatchRequestItems(); }
/** * Initiates a new Batch request. * * The typical operation flow is: * <pre> * BatchRequest batchRequest = syncopeClient.batch(); * batchRequest.getService(UserService.class).create(...); * batchRequest.getService(UserService.class).update(...); * batchRequest.getService(GroupService.class).update(...); * batchRequest.getService(GroupService.class).delete(...); * ... * BatchResponse batchResponse = batchRequest().commit(); * List<BatchResponseItem> items = batchResponse.getItems() * </pre> * * @return empty Batch request */ public BatchRequest batch() { return new BatchRequest( mediaType, restClientFactory.getAddress(), restClientFactory.getProviders(), getJWT()); } }