org.glassfish.jersey.client.spi.ConnectorProvider Java Examples

The following examples show how to use org.glassfish.jersey.client.spi.ConnectorProvider. 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: RestClientConfigurator.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Register third-party {@link ConnectorProvider}, like org.glassfish.jersey.apache.connector.ApacheConnectorProvider, etc, along with (optional) configuration properties for the provider.<br>
 * If not specified, <code>org.glassfish.jersey.client.HttpUrlConnectorProvider</code> is used as a connection provider.<br>
 * <br>Note: Currently only Apache connector is expected to work properly. Other connector providers may or may not work if additional configuration is needed.
 * 
 * @param connectorProvider - the connection provider
 * @param properties - (optional) configuration properties for the connection provider
 **/
@PublicAtsApi
public void registerConnectorProvider( ConnectorProvider connectorProvider,
                                       Map<String, Object> properties ) {

    this.connectorProvider = connectorProvider;
    if (properties != null) {
        this.connectorProviderProperties.putAll(properties);
    }

}
 
Example #2
Source File: RestClientConfigurator.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Use org.glassfish.jersey.apache.connector.ApacheConnectorProvider as a provider.<br>
 * Note that an additional dependency (jersey-apache-connector) must be specified before using this method.
 */
@PublicAtsApi
public void registerApacheConnectorProvider() {

    try {
        registerApacheConnectorProvider((ConnectorProvider) Class.forName(RestClient.APACHE_CONNECTOR_CLASSNAME)
                                                                 .newInstance(),
                                        null, null, null);
    } catch (Exception e) {
        throw new RuntimeException("Could not register connector provider '" + RestClient.APACHE_CONNECTOR_CLASSNAME
                                   + "'", e);
    }

}
 
Example #3
Source File: RestClientConfigurator.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Use org.glassfish.jersey.apache.connector.ApacheConnectorProvider as a provider
 * @param properties - (optional) configuration properties for the connection provider
 */
@PublicAtsApi
public void registerApacheConnectorProvider( Map<String, Object> properties ) {

    try {
        registerApacheConnectorProvider((ConnectorProvider) Class.forName(RestClient.APACHE_CONNECTOR_CLASSNAME)
                                                                 .newInstance(),
                                        properties, null, null);
    } catch (Exception e) {
        throw new RuntimeException("Could not register connector provider '" + RestClient.APACHE_CONNECTOR_CLASSNAME
                                   + "'", e);
    }
}
 
Example #4
Source File: ClientBuilder.java    From soabase with Apache License 2.0 5 votes vote down vote up
public Client buildJerseyClient(JerseyClientConfiguration configuration, String clientName)
{
    ConnectorProvider localConnectorProvider;
    if ( connectorProvider != null )
    {
        localConnectorProvider = connectorProvider;
    }
    else
    {
        HttpClientBuilder apacheHttpClientBuilder = new HttpClientBuilder(environment).using(configuration);
        CloseableHttpClient closeableHttpClient = apacheHttpClientBuilder.build(clientName);
        localConnectorProvider = new JerseyRetryConnectorProvider(retryComponents, closeableHttpClient, configuration.isChunkedEncodingEnabled());
    }
    JerseyClientBuilder builder = new JerseyClientBuilder(environment)
        .using(configuration)
        .using(localConnectorProvider);
    for ( Class<?> klass : providerClasses )
    {
        builder = builder.withProvider(klass);
    }
    for ( Object provider : providers )
    {
        builder = builder.withProvider(provider);
    }
    Client client = builder
        .build(clientName);

    SoaBundle.getFeatures(environment).putNamed(client, Client.class, clientName);

    return client;
}
 
Example #5
Source File: RestClientConfigurator.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/** <strong>Note</strong>: For internal usage only. Using this method may lead to undesired effects
 * Use org.glassfish.jersey.apache.connector.ApacheConnectorProvider as a provider<br>
 * @param connectorProvider - the connector provider's class
 * @param properties - (optional) configuration properties for the connection provider.
 * <strong>Note</strong>: If connections will be done over SSL (HTTPS), any of the needed configuration must be done by you.
 * ATS will NOT apply any of the logic, related to that functionality if connectionManager parameter is not null.
 * @param connectionManager - (optional) specify the connection manager to be used with the connector provider. If this parameter is not null, connection factory must also be provider
 * @param connectionFactory - (optional) specify the connection factory
 */
public void registerApacheConnectorProvider( ConnectorProvider connectorProvider,
                                             Map<String, Object> properties,
                                             HttpClientConnectionManager connectionManager,
                                             HttpConnectionFactory connectionFactory ) {

    try {
        Class<?> apacheClientProperties = Class.forName(RestClient.APACHE_CLIENT_PROPERTIES_CLASSNAME);

        if (properties != null) {
            if (properties.get((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG")
                                                              .get(null)) != null) {
                // do nothing the user has provided such property
            } else {
                // add pool request timeout of 30 seconds
                RequestConfig requestConfig = (RequestConfig) properties.get((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG")
                                                                                                            .get(null));
                if (requestConfig != null) {
                    // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec
                    requestConfig = RequestConfig.copy(requestConfig)
                                                 .setConnectionRequestTimeout(30 * 1000)
                                                 .build();
                } else {
                    // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec
                    requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30 * 1000).build();
                }

            }
        } else {
            // construct properties maps and add pool request timeout of 30 seconds
            properties = new HashMap<String, Object>();
            // Throw an org.apache.http.conn.ConnectionPoolTimeoutException exception if connection can not be leased/obtained from the pool after 30 sec
            properties.put((String) apacheClientProperties.getDeclaredField("REQUEST_CONFIG").get(null),
                           RequestConfig.custom().setConnectionRequestTimeout(30 * 1000).build());
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to register connector provider '" + RestClient.APACHE_CONNECTOR_CLASSNAME
                                   + "'", e);
    }

    registerConnectorProvider(connectorProvider, properties);
    this.connectionManager = connectionManager;
    this.connectionFactory = connectionFactory;
}
 
Example #6
Source File: RestClientHelper.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
public static ClientConfig getClientConfig(final ServerContext.Type type,
                                           final Credentials credentials,
                                           final String serverUri,
                                           final boolean includeProxySettings) {

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    final ConnectorProvider connectorProvider = new ApacheConnectorProvider();
    // custom json provider ignores new fields that aren't recognized
    final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    final ClientConfig clientConfig = new ClientConfig(jacksonJsonProvider).connectorProvider(connectorProvider);
    clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);
    clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);

    // For TFS OnPrem we only support NTLM authentication right now. Since 2016 servers support Basic as well,
    // we need to let the server and client negotiate the protocol instead of preemptively assuming Basic.
    // TODO: This prevents PATs from being used OnPrem. We need to fix this soon to support PATs onPrem.
    clientConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, type != ServerContext.Type.TFS);

    //Define a local HTTP proxy
    if (includeProxySettings) {
        final HttpProxyService proxyService = PluginServiceProvider.getInstance().getHttpProxyService();
        final String proxyUrl = proxyService.getProxyURL();
        clientConfig.property(ClientProperties.PROXY_URI, proxyUrl);
        if (proxyService.isAuthenticationRequired()) {
            // To work with authenticated proxies and TFS, we provide the proxy credentials if they are registered
            final AuthScope ntlmAuthScope =
                    new AuthScope(proxyService.getProxyHost(), proxyService.getProxyPort(),
                            AuthScope.ANY_REALM, AuthScope.ANY_SCHEME);
            credentialsProvider.setCredentials(ntlmAuthScope,
                    new UsernamePasswordCredentials(proxyService.getUserName(), proxyService.getPassword()));
        }
    }

    // register a filter to set the User Agent header
    clientConfig.register(new ClientRequestFilter() {
        @Override
        public void filter(final ClientRequestContext requestContext) throws IOException {
            // The default user agent is something like "Jersey/2.6"
            final String userAgent = VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client", HttpClientBuilder.class);
            // Finally, we can add the header
            requestContext.getHeaders().add(HttpHeaders.USER_AGENT, userAgent);
        }
    });

    return clientConfig;
}
 
Example #7
Source File: ClientBuilder.java    From soabase with Apache License 2.0 4 votes vote down vote up
public ClientBuilder withConnectorProvider(ConnectorProvider connectorProvider) {
    this.connectorProvider = connectorProvider;
    return this;
}
 
Example #8
Source File: RestClientConfigurator.java    From ats-framework with Apache License 2.0 2 votes vote down vote up
ConnectorProvider getConnectorProvider() {

        return connectorProvider;
    }