Search in sources :

Example 1 with ClientFilter

use of com.sun.jersey.api.client.filter.ClientFilter in project eureka by Netflix.

the class EurekaHttpClientsTest method testAddingAdditionalFilters.

@Test
public void testAddingAdditionalFilters() throws Exception {
    TestFilter testFilter = new TestFilter();
    Collection<ClientFilter> additionalFilters = Arrays.<ClientFilter>asList(testFilter);
    TransportClientFactory transportClientFactory = new Jersey1TransportClientFactories().newTransportClientFactory(clientConfig, additionalFilters, MY_INSTANCE);
    EurekaHttpClient client = transportClientFactory.newClient(clusterResolver.getClusterEndpoints().get(0));
    client.getApplication("foo");
    assertThat(testFilter.await(30, TimeUnit.SECONDS), is(true));
}
Also used : Jersey1TransportClientFactories(com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories) ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) Test(org.junit.Test)

Example 2 with ClientFilter

use of com.sun.jersey.api.client.filter.ClientFilter in project eureka by Netflix.

the class Jersey1TransportClientFactories method newTransportClientFactory.

@Deprecated
public TransportClientFactory newTransportClientFactory(final Collection<ClientFilter> additionalFilters, final EurekaJerseyClient providedJerseyClient) {
    ApacheHttpClient4 apacheHttpClient = providedJerseyClient.getClient();
    if (additionalFilters != null) {
        for (ClientFilter filter : additionalFilters) {
            if (filter != null) {
                apacheHttpClient.addFilter(filter);
            }
        }
    }
    final TransportClientFactory jerseyFactory = new JerseyEurekaHttpClientFactory(providedJerseyClient, false);
    final TransportClientFactory metricsFactory = MetricsCollectingEurekaHttpClient.createFactory(jerseyFactory);
    return new TransportClientFactory() {

        @Override
        public EurekaHttpClient newClient(EurekaEndpoint serviceUrl) {
            return metricsFactory.newClient(serviceUrl);
        }

        @Override
        public void shutdown() {
            metricsFactory.shutdown();
            jerseyFactory.shutdown();
        }
    };
}
Also used : ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) ApacheHttpClient4(com.sun.jersey.client.apache4.ApacheHttpClient4) TransportClientFactory(com.netflix.discovery.shared.transport.TransportClientFactory) EurekaEndpoint(com.netflix.discovery.shared.resolver.EurekaEndpoint)

Example 3 with ClientFilter

use of com.sun.jersey.api.client.filter.ClientFilter in project exhibitor by soabase.

the class ExhibitorMain method addRemoteAuth.

private void addRemoteAuth(ExhibitorArguments.Builder builder, String remoteAuthSpec) {
    String[] parts = remoteAuthSpec.split(":");
    Preconditions.checkArgument(parts.length == 2, "Badly formed remote client authorization: " + remoteAuthSpec);
    String type = parts[0].trim();
    String userName = parts[1].trim();
    String password = Preconditions.checkNotNull(users.get(userName), "Realm user not found: " + userName);
    ClientFilter filter;
    if (type.equals("basic")) {
        filter = new HTTPBasicAuthFilter(userName, password);
    } else if (type.equals("digest")) {
        filter = new HTTPDigestAuthFilter(userName, password);
    } else {
        throw new IllegalStateException("Unknown remote client authorization type: " + type);
    }
    builder.remoteConnectionConfiguration(new RemoteConnectionConfiguration(Arrays.asList(filter)));
}
Also used : HTTPDigestAuthFilter(com.sun.jersey.api.client.filter.HTTPDigestAuthFilter) ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) RemoteConnectionConfiguration(com.netflix.exhibitor.core.RemoteConnectionConfiguration) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 4 with ClientFilter

use of com.sun.jersey.api.client.filter.ClientFilter 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)

Example 5 with ClientFilter

use of com.sun.jersey.api.client.filter.ClientFilter in project coprhd-controller by CoprHD.

the class ApiTestBase method createHttpsClient.

protected BalancedWebResource createHttpsClient(final String username, final String password, List<String> hosts, boolean addAuthFilters) throws NoSuchAlgorithmException {
    String verb = System.getenv("API_TEST_VERBOSE");
    boolean verbose = false;
    if (verb != null && verb.equalsIgnoreCase("true")) {
        verbose = true;
    }
    // Disable server certificate validation as we are using
    // self-signed certificate
    disableCertificateValidation();
    BalancedWebResource lbw = new BalancedWebResource();
    for (String h : hosts) {
        final ClientConfig config = new DefaultClientConfig();
        final Client c = Client.create(config);
        if (verbose) {
            c.addFilter(new LoggingFilter());
        }
        if (addAuthFilters) {
            c.setFollowRedirects(false);
            c.addFilter(new HTTPBasicAuthFilter(username, password));
            c.addFilter(new ClientFilter() {

                @Override
                public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
                    if (_savedTokens.containsKey(username)) {
                        ArrayList<Object> token = new ArrayList<Object>();
                        token.add(_savedTokens.get(username));
                        request.getHeaders().put(AUTH_TOKEN_HEADER, token);
                    }
                    ClientResponse response = getNext().handle(request);
                    // this only used to inspect cookies after the fact.
                    if (null == _lastUsedAuthTokenCookie) {
                        System.out.println("Cookie was null");
                        List<NewCookie> allCookies = response.getCookies();
                        System.out.println("Cookie list size:" + allCookies.size());
                        for (NewCookie ck : allCookies) {
                            System.out.print("Cookie name " + ck.getName());
                            if (ck.getName().equals(AUTH_TOKEN_HEADER)) {
                                _lastUsedAuthTokenCookie = ck.getValue();
                                break;
                            }
                        }
                    }
                    if (response.getHeaders() != null && response.getHeaders().get(AUTH_TOKEN_HEADER) != null) {
                        _savedTokens.put(username, response.getHeaders().getFirst(AUTH_TOKEN_HEADER));
                    }
                    if (response.getHeaders() != null && response.getHeaders().get(AUTH_PROXY_TOKEN_HEADER) != null) {
                        _savedProxyTokens.put(username, response.getHeaders().getFirst(AUTH_PROXY_TOKEN_HEADER));
                    }
                    if (response.getStatus() == 302) {
                        WebResource wb = c.resource(response.getLocation());
                        response = wb.header(AUTH_TOKEN_HEADER, _savedTokens.get(username)).get(ClientResponse.class);
                    }
                    return response;
                }
            });
        } else {
            c.setFollowRedirects(true);
        }
        lbw.addWebResource(c.resource(h));
    }
    return lbw;
}
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) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) 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

ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)10 HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)6 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)5 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)5 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)5 ArrayList (java.util.ArrayList)5 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)4 ClientRequest (com.sun.jersey.api.client.ClientRequest)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 Client (com.sun.jersey.api.client.Client)3 WebResource (com.sun.jersey.api.client.WebResource)3 NewCookie (javax.ws.rs.core.NewCookie)3 EurekaEndpoint (com.netflix.discovery.shared.resolver.EurekaEndpoint)1 TransportClientFactory (com.netflix.discovery.shared.transport.TransportClientFactory)1 Jersey1TransportClientFactories (com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories)1 RemoteConnectionConfiguration (com.netflix.exhibitor.core.RemoteConnectionConfiguration)1 HTTPDigestAuthFilter (com.sun.jersey.api.client.filter.HTTPDigestAuthFilter)1 ApacheHttpClient4 (com.sun.jersey.client.apache4.ApacheHttpClient4)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1