Package com.emc.vipr.client

Source Code of com.emc.vipr.client.AuthClient

package com.emc.vipr.client;

import com.emc.vipr.client.impl.Constants;
import com.emc.vipr.client.impl.RestClient;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import javax.ws.rs.core.MultivaluedMap;
import java.net.URI;

public class AuthClient {
    protected RestClient client;

    AuthClient(RestClient client) {
        this.client = client;
    }

    public AuthClient(String host) {
        this(new ClientConfig().withHost(host));
    }

    public AuthClient(ClientConfig config) {
        this(config.newClient());
    }

    /**
     * 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();
        return client.getAuthToken();
    }

    public String proxyToken() {
        ClientResponse response = client.resource("/proxytoken").get(ClientResponse.class);
        MultivaluedMap<String,String> headers = response.getHeaders();
        String proxyToken = headers.getFirst(Constants.PROXY_TOKEN_KEY);
        response.close();
        return proxyToken;
    }

    public void logout() {
        if (isLoggedIn()) {
            ClientResponse response = client.resource("/logout").get(ClientResponse.class);
            response.close();
        }
    }

    public void forceLogout() {
        if (isLoggedIn()) {
            URI uri = client.uriBuilder("/logout").queryParam("force", "true").build();
            ClientResponse response = client.resource(uri).get(ClientResponse.class);
            response.close();
        }
    }
   
    public boolean isLoggedIn() {
        return (client.getAuthToken() != null) && !("".equals(client.getAuthToken()));
    }
}
TOP

Related Classes of com.emc.vipr.client.AuthClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.