Search in sources :

Example 31 with HTTPBasicAuthFilter

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

the class RestClientFactory method getRESTClient.

public RestClientItf getRESTClient(URI endpoint, String username, String password, boolean authFilter) {
    RestClientItf clientApi = _clientMap.get(endpoint.toString() + ":" + username + ":" + password);
    if (clientApi == null) {
        Client jerseyClient = new ApacheHttpClient(_clientHandler);
        if (authFilter) {
            jerseyClient.addFilter(new HTTPBasicAuthFilter(username, password));
        }
        clientApi = createNewRestClient(endpoint, username, password, jerseyClient);
        _clientMap.putIfAbsent(endpoint.toString() + ":" + username + ":" + password, clientApi);
    }
    return clientApi;
}
Also used : ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) ApacheHttpClient(com.sun.jersey.client.apache.ApacheHttpClient) HttpClient(org.apache.commons.httpclient.HttpClient) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 32 with HTTPBasicAuthFilter

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

the class LogServiceTest method initToken.

/**
 * Create https client after invoking the login api to get security token
 *
 * @return
 */
private static void initToken() {
    Client client = Client.create();
    client.setFollowRedirects(false);
    client.addFilter(new HTTPBasicAuthFilter(SYSADMIN, SYSADMIN_PASSWORD));
    ClientResponse loginResp = client.resource(LOGIN_URI).get(ClientResponse.class);
    Assert.assertEquals(200, loginResp.getStatus());
    authToken = loginResp.getHeaders().getFirst(AUTH_TOKEN_HEADER);
    Assert.assertNotNull(authToken);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Client(com.sun.jersey.api.client.Client) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 33 with HTTPBasicAuthFilter

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

the class RESTClientUtil method authenticate.

private void authenticate(final Client c, final int authenticateCallCount) throws NoSuchAlgorithmException {
    final String methodName = "authenticate(): ";
    log.debug(methodName + "Called");
    disableCertificateValidation();
    c.setFollowRedirects(false);
    if (log.isTraceEnabled()) {
        c.addFilter(new LoggingFilter());
    }
    c.addFilter(new HTTPBasicAuthFilter(this._username, this._password));
    c.addFilter(new ClientFilter() {

        private Object authToken;

        // private int callCount = authenticateCallCount;
        @Override
        public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
            if (authToken != null) {
                request.getHeaders().put(AUTH_TOKEN_HEADER, Collections.singletonList(authToken));
            }
            ClientResponse response = getNext().handle(request);
            if (response.getHeaders() != null && response.getHeaders().containsKey(AUTH_TOKEN_HEADER)) {
                authToken = (response.getHeaders().getFirst(AUTH_TOKEN_HEADER));
            }
            if (response.getStatus() == 302) {
                WebResource wb = c.resource(response.getLocation());
                response = wb.header(AUTH_TOKEN_HEADER, authToken).get(ClientResponse.class);
            }
            return response;
        }
    });
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) ClientResponse(com.sun.jersey.api.client.ClientResponse) LoggingFilter(com.sun.jersey.api.client.filter.LoggingFilter) ClientFilter(com.sun.jersey.api.client.filter.ClientFilter) WebResource(com.sun.jersey.api.client.WebResource) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter) ClientRequest(com.sun.jersey.api.client.ClientRequest)

Example 34 with HTTPBasicAuthFilter

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

the class AuthClient method login.

/**
 * Performs a login operation. The token is automatically associated with this client
 * connection.
 *
 * @param username The username.
 * @param password The password.
 * @return The authentication token.
 */
public String login(String username, String password) {
    WebResource resource = client.getClient().resource(client.uriBuilder("/login").build());
    resource.addFilter(new HTTPBasicAuthFilter(username, password));
    ClientResponse response = resource.get(ClientResponse.class);
    response.close();
    client.setLoginTime(System.currentTimeMillis());
    return client.getAuthToken();
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) WebResource(com.sun.jersey.api.client.WebResource) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Example 35 with HTTPBasicAuthFilter

use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project pentaho-platform by pentaho.

the class CommandLineProcessor method initRestService.

/**
 * Used only for REST Jersey calls
 *
 * @throws ParseException
 */
private void initRestService() throws ParseException, InitializationException {
    // get information about the remote connection
    String username = getOptionValue(INFO_OPTION_USERNAME_NAME, true, false);
    String password = getOptionValue(INFO_OPTION_PASSWORD_NAME, true, false);
    password = KettleTwoWayPasswordEncoder.decryptPasswordOptionallyEncrypted(password);
    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    client = Client.create(clientConfig);
    client.addFilter(new HTTPBasicAuthFilter(username, password));
}
Also used : DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) DefaultClientConfig(com.sun.jersey.api.client.config.DefaultClientConfig) HTTPBasicAuthFilter(com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)

Aggregations

HTTPBasicAuthFilter (com.sun.jersey.api.client.filter.HTTPBasicAuthFilter)45 Client (com.sun.jersey.api.client.Client)31 WebResource (com.sun.jersey.api.client.WebResource)19 ClientResponse (com.sun.jersey.api.client.ClientResponse)15 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)14 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)14 LoggingFilter (com.sun.jersey.api.client.filter.LoggingFilter)7 ArrayList (java.util.ArrayList)7 ClientFilter (com.sun.jersey.api.client.filter.ClientFilter)6 HTTPSProperties (com.sun.jersey.client.urlconnection.HTTPSProperties)5 ApacheHttpClient (com.sun.jersey.client.apache.ApacheHttpClient)4 HostnameVerifier (javax.net.ssl.HostnameVerifier)4 SSLSession (javax.net.ssl.SSLSession)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)3 ClientRequest (com.sun.jersey.api.client.ClientRequest)3 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)3 FormDataMultiPart (com.sun.jersey.multipart.FormDataMultiPart)3 FileInputStream (java.io.FileInputStream)3 URI (java.net.URI)3 KeyStore (java.security.KeyStore)3