Java Code Examples for org.apache.cxf.jaxrs.client.Client#accept()

The following examples show how to use org.apache.cxf.jaxrs.client.Client#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReconciliationRestClient.java    From syncope with Apache License 2.0 5 votes vote down vote up
public static Response push(final AnyQuery anyQuery, final CSVPushSpec spec) {
    ReconciliationService service = getService(ReconciliationService.class);
    Client client = WebClient.client(service);
    client.accept(RESTHeaders.TEXT_CSV);

    Response response = service.push(anyQuery, spec);

    SyncopeConsoleSession.get().resetClient(ReconciliationService.class);

    return response;
}
 
Example 2
Source File: AbstractAnyRestClient.java    From syncope with Apache License 2.0 5 votes vote down vote up
public Map<String, String> associate(
        final ResourceAssociationAction action,
        final String etag,
        final String key,
        final List<StatusBean> statuses) {

    Map<String, String> result = new LinkedHashMap<>();
    synchronized (this) {
        AnyService<?> service = getService(etag, getAnyServiceClass());
        Client client = WebClient.client(service);
        List<String> accept = client.getHeaders().get(HttpHeaders.ACCEPT);
        if (!accept.contains(RESTHeaders.MULTIPART_MIXED)) {
            client.accept(RESTHeaders.MULTIPART_MIXED);
        }

        StatusR statusR = StatusUtils.statusR(statuses).build();

        ResourceAR resourceAR = new ResourceAR.Builder().key(key).
                action(action).
                onSyncope(statusR.isOnSyncope()).
                resources(statusR.getResources()).build();
        try {
            List<BatchResponseItem> items = parseBatchResponse(service.associate(resourceAR));
            for (int i = 0; i < items.size(); i++) {
                result.put(
                        resourceAR.getResources().get(i),
                        getStatus(items.get(i).getStatus()));
            }
        } catch (IOException e) {
            LOG.error("While processing Batch response", e);
        }

        resetClient(getAnyServiceClass());
    }
    return result;
}
 
Example 3
Source File: AbstractAnyRestClient.java    From syncope with Apache License 2.0 5 votes vote down vote up
public Map<String, String> deassociate(
        final ResourceDeassociationAction action,
        final String etag,
        final String key,
        final List<StatusBean> statuses) {

    Map<String, String> result = new LinkedHashMap<>();
    synchronized (this) {
        AnyService<?> service = getService(etag, getAnyServiceClass());
        Client client = WebClient.client(service);
        List<String> accept = client.getHeaders().get(HttpHeaders.ACCEPT);
        if (!accept.contains(RESTHeaders.MULTIPART_MIXED)) {
            client.accept(RESTHeaders.MULTIPART_MIXED);
        }

        ResourceDR resourceDR = new ResourceDR.Builder().key(key).
                action(action).
                resources(StatusUtils.statusR(statuses).build().getResources()).build();
        try {
            List<BatchResponseItem> items = parseBatchResponse(service.deassociate(resourceDR));
            for (int i = 0; i < items.size(); i++) {
                result.put(
                        resourceDR.getResources().get(i),
                        getStatus(items.get(i).getStatus()));
            }
        } catch (IOException e) {
            LOG.error("While processing Batch response", e);
        }

        resetClient(getAnyServiceClass());
    }
    return result;
}
 
Example 4
Source File: SyncopeClient.java    From syncope with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an instance of the given service class, with configured content type and authentication.
 *
 * @param <T> any service class
 * @param serviceClass service class reference
 * @return service instance of the given reference class
 */
public <T> T getService(final Class<T> serviceClass) {
    synchronized (restClientFactory) {
        restClientFactory.setServiceClass(serviceClass);
        T serviceInstance = restClientFactory.create(serviceClass);

        Client client = WebClient.client(serviceInstance);
        client.type(mediaType).accept(mediaType);
        if (serviceInstance instanceof AnyService || serviceInstance instanceof ExecutableService) {
            client.accept(RESTHeaders.MULTIPART_MIXED);
        }

        ClientConfiguration config = WebClient.getConfig(client);
        config.getRequestContext().put(HEADER_SPLIT_PROPERTY, true);
        config.getRequestContext().put(URLConnectionHTTPConduit.HTTPURL_CONNECTION_METHOD_REFLECTION, true);
        if (useCompression) {
            config.getInInterceptors().add(new GZIPInInterceptor());
            config.getOutInterceptors().add(new GZIPOutInterceptor());
        }
        if (tlsClientParameters != null) {
            HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
            httpConduit.setTlsClientParameters(tlsClientParameters);
        }

        return serviceInstance;
    }
}
 
Example 5
Source File: ReconciliationITCase.java    From syncope with Apache License 2.0 4 votes vote down vote up
@Test
public void exportCSV() throws IOException {
    ReconciliationService service = adminClient.getService(ReconciliationService.class);
    Client client = WebClient.client(service);
    client.accept(RESTHeaders.TEXT_CSV);

    AnyQuery anyQuery = new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
            fiql(SyncopeClient.getUserSearchConditionBuilder().is("username").equalTo("*ini").query()).
            page(1).
            size(1000).
            orderBy("username ASC").
            build();

    CSVPushSpec spec = new CSVPushSpec.Builder(AnyTypeKind.USER.name()).ignorePaging(true).
            field("username").
            field("status").
            plainAttr("firstname").
            plainAttr("surname").
            plainAttr("email").
            plainAttr("loginDate").
            build();

    Response response = service.push(anyQuery, spec);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    assertEquals(
            "attachment; filename=" + SyncopeConstants.MASTER_DOMAIN + ".csv",
            response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION));

    PagedResult<UserTO> users = userService.search(anyQuery);
    assertNotNull(users);

    MappingIterator<Map<String, String>> reader = new CsvMapper().readerFor(Map.class).
            with(CsvSchema.emptySchema().withHeader()).readValues((InputStream) response.getEntity());

    int rows = 0;
    for (; reader.hasNext(); rows++) {
        Map<String, String> row = reader.next();

        assertEquals(users.getResult().get(rows).getUsername(), row.get("username"));
        assertEquals(users.getResult().get(rows).getStatus(), row.get("status"));

        switch (row.get("username")) {
            case "rossini":
                assertEquals(spec.getNullValue(), row.get("email"));
                assertTrue(row.get("loginDate").contains(spec.getArrayElementSeparator()));
                break;

            case "verdi":
                assertEquals("[email protected]", row.get("email"));
                assertEquals(spec.getNullValue(), row.get("loginDate"));
                break;

            case "bellini":
                assertEquals(spec.getNullValue(), row.get("email"));
                assertFalse(row.get("loginDate").contains(spec.getArrayElementSeparator()));
                break;

            default:
                break;
        }
    }
    assertEquals(rows, users.getTotalCount());
}