org.apache.cxf.jaxrs.client.ClientConfiguration Java Examples

The following examples show how to use org.apache.cxf.jaxrs.client.ClientConfiguration. 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: ClientImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Similar to <code>testClientConfigCopiedOnPathCallWithTemplates</code>,
 * this test uses a template, but in the initial call to target().  At this
 * point, the WebTargetImpl's targetClient field will be null, so we need
 * this test to ensure that there are no null pointers when creating and
 * using a template on the first call to target().
 */
@Test
public void testTemplateInInitialTarget() {
    String address = "http://localhost:8080/bookstore/{a}/simple";
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target(address).resolveTemplate("a", "bookheaders");
    webTarget.request("application/xml").header("a", "b");
    WebClient webClient = getWebClient(webTarget);
    
    ClientConfiguration clientConfig = WebClient.getConfig(webClient);
    clientConfig.setOutInterceptors(Arrays.asList(new MyInterceptor()));
    assertTrue("Precondition failed - original WebTarget is missing expected interceptor",
               doesClientConfigHaveMyInterceptor(webClient));
    
    WebTarget webTargetAfterPath = webTarget.path("/rest/{key}/").resolveTemplate("key", "myKey");
    WebClient webClientAfterPath = getWebClient(webTargetAfterPath);
    assertTrue("New WebTarget is missing expected interceptor specified on 'parent' WebTarget's client impl",
               doesClientConfigHaveMyInterceptor(webClientAfterPath));

}
 
Example #2
Source File: ClientImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * This test checks that we do not lose track of registered interceptors
 * on the original client implementation after we create a new impl with
 * the path(...) method - particularly when the path passed in to the
 * path(...) method contains a template.
 */
@Test
public void testClientConfigCopiedOnPathCallWithTemplates() {
    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target("http://localhost:8080/");
    WebClient webClient = getWebClient(webTarget);
    
    ClientConfiguration clientConfig = WebClient.getConfig(webClient);
    clientConfig.setOutInterceptors(Arrays.asList(new MyInterceptor()));
    assertTrue("Precondition failed - original WebTarget is missing expected interceptor",
               doesClientConfigHaveMyInterceptor(webClient));

    WebTarget webTargetAfterPath = webTarget.path("/rest/{key}/").resolveTemplate("key", "myKey");
    WebClient webClientAfterPath = getWebClient(webTargetAfterPath);
    assertTrue("New WebTarget is missing expected interceptor specified on 'parent' WebTarget's client impl",
               doesClientConfigHaveMyInterceptor(webClientAfterPath));
}
 
Example #3
Source File: ClientImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void setConnectionProperties(Map<String, Object> configProps, ClientConfiguration clientCfg) {
    Long connTimeOutValue = getLongValue(configProps.get(HTTP_CONNECTION_TIMEOUT_PROP));
    if (connTimeOutValue != null) {
        clientCfg.getHttpConduit().getClient().setConnectionTimeout(connTimeOutValue);
    }
    Long recTimeOutValue = getLongValue(configProps.get(HTTP_RECEIVE_TIMEOUT_PROP));
    if (recTimeOutValue != null) {
        clientCfg.getHttpConduit().getClient().setReceiveTimeout(recTimeOutValue);
    }
    Object proxyServerValue = configProps.get(HTTP_PROXY_SERVER_PROP);
    if (proxyServerValue != null) {
        clientCfg.getHttpConduit().getClient().setProxyServer((String)proxyServerValue);
    }
    Integer proxyServerPortValue = getIntValue(configProps.get(HTTP_PROXY_SERVER_PORT_PROP));
    if (proxyServerPortValue != null) {
        clientCfg.getHttpConduit().getClient().setProxyServerPort(proxyServerPortValue);
    }
    Boolean autoRedirectValue = getBooleanValue(configProps.get(HTTP_AUTOREDIRECT_PROP));
    if (autoRedirectValue != null) {
        clientCfg.getHttpConduit().getClient().setAutoRedirect(autoRedirectValue);
    }
    Boolean mantainSessionValue = getBooleanValue(configProps.get(HTTP_MAINTAIN_SESSION_PROP));
    if (mantainSessionValue != null) {
        clientCfg.getRequestContext().put(Message.MAINTAIN_SESSION, mantainSessionValue);
    }
}
 
Example #4
Source File: JAXRSAsyncClientTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatchBookTimeout() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/patch";
    WebClient wc = WebClient.create(address);
    wc.type("application/xml");
    ClientConfiguration clientConfig = WebClient.getConfig(wc);
    clientConfig.getRequestContext().put("use.async.http.conduit", true);
    HTTPClientPolicy clientPolicy = clientConfig.getHttpConduit().getClient();
    clientPolicy.setReceiveTimeout(500);
    clientPolicy.setConnectionTimeout(500);
    try {
        Book book = wc.invoke("PATCH", new Book("Timeout", 123L), Book.class);
        fail("should throw an exception due to timeout, instead got " + book);
    } catch (javax.ws.rs.ProcessingException e) {
        //expected!!!
    }
}
 
Example #5
Source File: BatchRequest.java    From syncope with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
    if (tlsClientParameters != null) {
        ClientConfiguration config = WebClient.getConfig(webClient);
        HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
        httpConduit.setTlsClientParameters(tlsClientParameters);
    }

    String body = BatchPayloadGenerator.generate(bcfb.getBatchRequestItems(), boundary);
    LOG.debug("Batch request body:\n{}", body);

    initBatchClientFactoryBean();

    return new BatchResponse(boundary, jwt, tlsClientParameters, webClient.post(body));
}
 
Example #6
Source File: BatchResponse.java    From syncope with Apache License 2.0 6 votes vote down vote up
/**
 * If asynchronous processing was requested, queries the monitor URI.
 *
 * @param monitor monitor URI
 * @param jwt authorization JWT
 * @param boundary mutipart / mixed boundary
 * @param tlsClientParameters (optional) TLS client parameters
 *
 * @return the last Response received from the Batch service
 */
public static Response poll(
        final URI monitor,
        final String jwt,
        final String boundary,
        final TLSClientParameters tlsClientParameters) {

    WebClient webClient = WebClient.create(monitor).
            header(HttpHeaders.AUTHORIZATION, "Bearer " + jwt).
            type(RESTHeaders.multipartMixedWith(boundary.substring(2)));
    if (tlsClientParameters != null) {
        ClientConfiguration config = WebClient.getConfig(webClient);
        HTTPConduit httpConduit = (HTTPConduit) config.getConduit();
        httpConduit.setTlsClientParameters(tlsClientParameters);
    }

    return webClient.get();
}
 
Example #7
Source File: ConsoleApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", ConsoleApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #8
Source File: CrxApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", CrxApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #9
Source File: AmbariClientBuilder.java    From components with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the transport level conduit in the client. Reopening a new connection, requires creating a new client
 * object using the build() method in this builder.
 *
 * @param root The resource returned by the build() method of this builder class
 */
public static void closeClient(ApiRootResource root) {
    ClientConfiguration config = WebClient.getConfig(root);
    HTTPConduit conduit = config.getHttpConduit();
    if (conduit == null) {
        throw new IllegalArgumentException("Client is not using the HTTP transport");
    }
    conduit.close();
}
 
Example #10
Source File: ResourceProducer.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
@Produces
@GazpachoResource
@RequestScoped
public QuestionnaireResource createQuestionnairResource(HttpServletRequest request) {
    RespondentAccount principal = (RespondentAccount) request.getUserPrincipal();
    String apiKey = principal.getApiKey();
    String secret = principal.getSecret();

    logger.info("Getting QuestionnaireResource using api key {}/{} ", apiKey, secret);

    JacksonJsonProvider jacksonProvider = new JacksonJsonProvider();
    ObjectMapper mapper = new ObjectMapper();
    // mapper.findAndRegisterModules();
    mapper.registerModule(new JSR310Module());
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    jacksonProvider.setMapper(mapper);

    QuestionnaireResource resource = JAXRSClientFactory.create(BASE_URI, QuestionnaireResource.class,
            Collections.singletonList(jacksonProvider), null);
    // proxies
    // WebClient.client(resource).header("Authorization", "GZQ " + apiKey);

    Client client = WebClient.client(resource);
    ClientConfiguration config = WebClient.getConfig(client);
    config.getOutInterceptors().add(new HmacAuthInterceptor(apiKey, secret));
    return resource;
}
 
Example #11
Source File: ClientInterceptorTest.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
private QuestionnaireResource getQuestionnaireResource() {
    QuestionnaireResource questionnaireResource = JAXRSClientFactory.create(BASE_URI, QuestionnaireResource.class,
            Collections.singletonList(getJacksonProvider()), null);

    Client client = WebClient.client(questionnaireResource);
    ClientConfiguration config = WebClient.getConfig(client);

    String apiKey = "FGFQM8T6YPVSW4Q";
    String secret = "39JYOYPWYR46R38OAOTVRZJMEXNJ46HL";
    config.getOutInterceptors().add(new HmacAuthInterceptor(apiKey, secret));
    return questionnaireResource;
}
 
Example #12
Source File: TrustedIdpFacebookProtocolHandler.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private String getSubjectName(String apiEndpoint, String accessToken, TrustedIdp trustedIdp) {
    WebClient client = WebClient.create(apiEndpoint,
                              Collections.singletonList(new JsonMapObjectProvider()),
                              "cxf-tls.xml");
    client.path("/me");
    ClientConfiguration config = WebClient.getConfig(client);

    if (LOG.isDebugEnabled()) {
        config.getOutInterceptors().add(new LoggingOutInterceptor());
        config.getInInterceptors().add(new LoggingInInterceptor());
    }

    client.accept("application/json");
    client.query("access_token", accessToken);

    String subjectName = getProperty(trustedIdp, SUBJECT_CLAIM);
    if (subjectName == null || subjectName.isEmpty()) {
        subjectName = "email";
    }
    client.query("fields", subjectName);
    JsonMapObject mapObject = client.get(JsonMapObject.class);

    String parsedSubjectName = (String)mapObject.getProperty(subjectName);
    if (subjectName.contains("email")) {
        parsedSubjectName = parsedSubjectName.replace("\\u0040", "@");
    }
    return parsedSubjectName;
}
 
Example #13
Source File: TrustedIdpFacebookProtocolHandler.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private ClientAccessToken getAccessTokenUsingCode(String tokenEndpoint, String code, String clientId,
                                                  String clientSecret, String redirectURI) {
    // Here we need to get the AccessToken using the authorization code
    List<Object> providers = new ArrayList<>();
    providers.add(new OAuthJSONProvider());

    WebClient client =
        WebClient.create(tokenEndpoint, providers, "cxf-tls.xml");

    ClientConfiguration config = WebClient.getConfig(client);

    if (LOG.isDebugEnabled()) {
        config.getOutInterceptors().add(new LoggingOutInterceptor());
        config.getInInterceptors().add(new LoggingInInterceptor());
    }

    client.type("application/x-www-form-urlencoded");
    client.accept("application/json");

    Form form = new Form();
    form.param("grant_type", "authorization_code");
    form.param("code", code);
    form.param("client_id", clientId);
    form.param("redirect_uri", redirectURI);
    form.param("client_secret", clientSecret);
    Response response = client.post(form);

    return response.readEntity(ClientAccessToken.class);
}
 
Example #14
Source File: ClientImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private boolean doesClientConfigHaveMyInterceptor(WebClient webClient) {
    ClientConfiguration clientConfigAfterPath = WebClient.getConfig(webClient);
    boolean foundMyInterceptor = false;
    for (Interceptor<?> i : clientConfigAfterPath.getOutInterceptors()) {
        if (MY_INTERCEPTOR_NAME.equals(i.toString())) {
            foundMyInterceptor = true;
            break;
        }
    }
    return foundMyInterceptor;
}
 
Example #15
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 #16
Source File: ActionConfigApiTest.java    From opencps-v2 with GNU Affero General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("https://virtserver.swaggerhub.com/binhthgc/opencps/1.0.0", ActionConfigApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #17
Source File: CqApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", CqApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #18
Source File: CustomApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", CustomApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #19
Source File: SlingApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", SlingApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #20
Source File: CustomApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", CustomApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #21
Source File: CqApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", CqApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #22
Source File: SlingApiTest.java    From swagger-aem with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", SlingApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #23
Source File: BlueOceanApiTest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", BlueOceanApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #24
Source File: RemoteAccessApiTest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", RemoteAccessApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #25
Source File: BlueOceanApiTest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", BlueOceanApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #26
Source File: RemoteAccessApiTest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", RemoteAccessApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #27
Source File: BaseRemoteAccessApiTest.java    From swaggy-jenkins with MIT License 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://localhost", BaseRemoteAccessApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #28
Source File: DossierStatisticApiTest.java    From opencps-v2 with GNU Affero General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("https://virtserver.swaggerhub.com/binhthgc/opencps/1.0.0", DossierStatisticApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #29
Source File: StatisticReportApiTest.java    From opencps-v2 with GNU Affero General Public License v3.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("https://virtserver.swaggerhub.com/binhthgc/opencps/1.0.0", StatisticReportApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}
 
Example #30
Source File: StoreApiTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    JacksonJsonProvider provider = new JacksonJsonProvider();
    List providers = new ArrayList();
    providers.add(provider);
    
    api = JAXRSClientFactory.create("http://petstore.swagger.io/v2", StoreApi.class, providers);
    org.apache.cxf.jaxrs.client.Client client = WebClient.client(api);
    
    ClientConfiguration config = WebClient.getConfig(client); 
}