Search in sources :

Example 21 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project cloudbreak by hortonworks.

the class YarnHttpClient method validateApiEndpoint.

@Override
public void validateApiEndpoint() throws YarnClientException, MalformedURLException {
    YarnEndpoint dashEndpoint = new YarnEndpoint(apiEndpoint, YarnResourceConstants.APPLICATIONS_PATH);
    ClientConfig clientConfig = new DefaultClientConfig();
    Client client = Client.create(clientConfig);
    WebResource webResource = client.resource(dashEndpoint.getFullEndpointUrl().toString());
    ClientResponse response = webResource.accept("application/json").type("application/json").get(ClientResponse.class);
    // Validate HTTP 200 status code
    if (response.getStatus() != YarnResourceConstants.HTTP_SUCCESS) {
        String msg = String.format("Received %d status code from url %s, reason: %s", response.getStatus(), dashEndpoint.getFullEndpointUrl().toString(), response.getEntity(String.class));
        LOGGER.debug(msg);
        throw new YarnClientException(msg);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) YarnClientException(com.sequenceiq.cloudbreak.cloud.yarn.client.exception.YarnClientException) WebResource(com.sun.jersey.api.client.WebResource) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) Client(com.sun.jersey.api.client.Client) YarnEndpoint(com.sequenceiq.cloudbreak.cloud.yarn.client.api.YarnEndpoint)

Example 22 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project commons by twitter.

the class HttpStatsFilterIntegrationTest method setUp.

@Before
public void setUp() {
    Stats.flush();
    server = new JettyHttpServerDispatch();
    server.listen(0);
    server.registerFilter(GuiceFilter.class, "/*");
    clock = new FakeClock();
    final Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(TestServlet.class).in(Singleton.class);
            bind(Clock.class).toInstance(clock);
            bind(HttpStatsFilter.class).in(Singleton.class);
        }
    }, new JerseyServletModule() {

        @Override
        protected void configureServlets() {
            filter("/*").through(HttpStatsFilter.class);
            serve("/*").with(GuiceContainer.class, ImmutableMap.of(PROPERTY_CONTAINER_RESPONSE_FILTERS, HttpStatsFilter.class.getName()));
        }
    });
    server.getRootContext().addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return injector;
        }
    });
    ClientConfig config = new DefaultClientConfig();
    client = Client.create(config);
}
Also used : JettyHttpServerDispatch(com.twitter.common.net.http.JettyHttpServerDispatch) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) FakeClock(com.twitter.common.util.testing.FakeClock) Injector(com.google.inject.Injector) Singleton(com.google.inject.Singleton) JerseyServletModule(com.sun.jersey.guice.JerseyServletModule) GuiceContainer(com.sun.jersey.guice.spi.container.servlet.GuiceContainer) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 23 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project incubator-atlas by apache.

the class AtlasBaseClient method getClient.

@VisibleForTesting
protected Client getClient(Configuration configuration, UserGroupInformation ugi, String doAsUser) {
    DefaultClientConfig config = new DefaultClientConfig();
    // Enable POJO mapping feature
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    int readTimeout = configuration.getInt("atlas.client.readTimeoutMSecs", 60000);
    int connectTimeout = configuration.getInt("atlas.client.connectTimeoutMSecs", 60000);
    if (configuration.getBoolean(TLS_ENABLED, false)) {
        // configuration object, persist it, then subsequently pass in an empty configuration to SSLFactory
        try {
            SecureClientUtils.persistSSLClientConfiguration(configuration);
        } catch (Exception e) {
            LOG.info("Error processing client configuration.", e);
        }
    }
    final URLConnectionClientHandler handler;
    if ((AuthenticationUtil.isKerberosAuthenticationEnabled())) {
        handler = SecureClientUtils.getClientConnectionHandler(config, configuration, doAsUser, ugi);
    } else {
        if (configuration.getBoolean(TLS_ENABLED, false)) {
            handler = SecureClientUtils.getUrlConnectionClientHandler();
        } else {
            handler = new URLConnectionClientHandler();
        }
    }
    Client client = new Client(handler, config);
    client.setReadTimeout(readTimeout);
    client.setConnectTimeout(connectTimeout);
    return client;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) URLConnectionClientHandler(com.sun.jersey.client.urlconnection.URLConnectionClientHandler) Client(com.sun.jersey.api.client.Client) ConnectException(java.net.ConnectException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 24 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project hive by apache.

the class RangerRestClientImpl method getRangerClient.

@VisibleForTesting
synchronized Client getRangerClient(HiveConf hiveConf) {
    Client ret = null;
    ClientConfig config = new DefaultClientConfig();
    config.getClasses().add(MultiPartWriter.class);
    config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
    config.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, (int) hiveConf.getTimeVar(HiveConf.ConfVars.REPL_EXTERNAL_CLIENT_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS));
    config.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, (int) hiveConf.getTimeVar(HiveConf.ConfVars.REPL_RANGER_CLIENT_READ_TIMEOUT, TimeUnit.MILLISECONDS));
    ret = Client.create(config);
    return ret;
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Client(com.sun.jersey.api.client.Client) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 25 with DefaultClientConfig

use of com.sun.jersey.api.client.config.DefaultClientConfig in project coprhd-controller by CoprHD.

the class ApiTestBase method createCookieHttpsClient.

/**
 * Use this client if you want to use cookies instead of the http headers for holding the
 * auth token
 */
protected Client createCookieHttpsClient(final String username, final String password) throws NoSuchAlgorithmException {
    // Disable server certificate validation as we are using
    // self-signed certificate
    disableCertificateValidation();
    final ClientConfig config = new DefaultClientConfig();
    final Client c = Client.create(config);
    c.addFilter(new LoggingFilter());
    c.setFollowRedirects(false);
    c.addFilter(new HTTPBasicAuthFilter(username, password));
    c.addFilter(new ClientFilter() {

        private ArrayList<Object> cookies;

        private ArrayList<Object> getCookiesToSet() {
            if (cookies != null && !cookies.isEmpty()) {
                ArrayList<Object> cookiesToSet = new ArrayList<Object>();
                StringBuilder cookieToAdd = new StringBuilder();
                for (Object cookieRaw : cookies) {
                    NewCookie cookie = (NewCookie) cookieRaw;
                    cookieToAdd.append(cookie.getName());
                    cookieToAdd.append("=");
                    cookieToAdd.append(cookie.getValue());
                    cookieToAdd.append("; ");
                }
                cookiesToSet.add(cookieToAdd);
                return cookiesToSet;
            }
            return null;
        }

        @Override
        public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
            ArrayList<Object> cookiesToSet = getCookiesToSet();
            if (cookiesToSet != null) {
                request.getHeaders().put("Cookie", cookiesToSet);
            }
            ClientResponse response = getNext().handle(request);
            if (response.getCookies() != null) {
                if (cookies == null) {
                    cookies = new ArrayList<Object>();
                }
                // simple addAll just for illustration (should probably check for duplicates and expired cookies)
                cookies.addAll(response.getCookies());
            }
            if (response.getStatus() == 302) {
                WebResource wb = c.resource(response.getLocation());
                cookiesToSet = getCookiesToSet();
                if (cookiesToSet != null) {
                    response = wb.header("Cookie", cookiesToSet).get(ClientResponse.class);
                } else {
                    response = wb.get(ClientResponse.class);
                }
            }
            return response;
        }
    });
    return c;
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) ClientResponse(com.sun.jersey.api.client.ClientResponse) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) LoggingFilter(com.sun.jersey.api.client.filter.LoggingFilter) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) Client(com.sun.jersey.api.client.Client) ClientRequest(com.sun.jersey.api.client.ClientRequest) NewCookie(javax.ws.rs.core.NewCookie)

Aggregations

DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)65 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)55 Client (com.sun.jersey.api.client.Client)42 WebResource (com.sun.jersey.api.client.WebResource)21 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)15 ClientResponse (com.sun.jersey.api.client.ClientResponse)14 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)9 HostnameVerifier (javax.net.ssl.HostnameVerifier)7 SSLSession (javax.net.ssl.SSLSession)7 TrustManager (javax.net.ssl.TrustManager)7 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)6 URLConnectionClientHandler (com.sun.jersey.client.urlconnection.URLConnectionClientHandler)6 IOException (java.io.IOException)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)5 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)5 SecureRandom (java.security.SecureRandom)5 SSLContext (javax.net.ssl.SSLContext)5 URISyntaxException (java.net.URISyntaxException)4 ArrayList (java.util.ArrayList)4