use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project coprhd-controller by CoprHD.
the class AuthSvcTests method createCookieHttpsClient.
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;
}
use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter 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;
}
use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter 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;
}
use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project coprhd-controller by CoprHD.
the class BuildRestRequest method makeClient.
public static Client makeClient(final ClientConfig config, final CoordinatorClient coordinator, final String auth, final String protocol, final String user, final String password) throws Exception {
if (StringUtils.isEmpty(protocol)) {
throw InternalServerErrorException.internalServerErrors.customServiceExecutionFailed("Protocol not defined" + protocol);
}
if (!protocol.equals("https")) {
logger.error("Only Https is supported. Protocol:{} is not supported", protocol);
throw InternalServerErrorException.internalServerErrors.customServiceExecutionFailed("Protocol not supported" + protocol);
}
final SSLContext context = SSLContext.getInstance("SSL");
final ViPRX509TrustManager trustManager = new ViPRX509TrustManager(coordinator);
context.init(null, new TrustManager[] { trustManager }, null);
config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(null, context));
final Client client = Client.create(config);
if (auth.equals(CustomServicesConstants.AuthType.BASIC.name())) {
if (!(StringUtils.isEmpty(user) && StringUtils.isEmpty(password))) {
client.addFilter(new HTTPBasicAuthFilter(user, password));
} else {
logger.error("user:{} or password not defined", user);
throw InternalServerErrorException.internalServerErrors.customServiceExecutionFailed("User or password not defined");
}
}
return client;
}
use of com.sun.jersey.api.client.filter.HTTPBasicAuthFilter in project coprhd-controller by CoprHD.
the class IsilonApiFactory method getRESTClient.
/**
* Create Isilon API client
*
* @param endpoint isilon endpoint
* @return
*/
public IsilonApi getRESTClient(URI endpoint, String username, String password) {
IsilonApi isilonApi = _clientMap.get(endpoint.toString() + ":" + username + ":" + password);
if (isilonApi == null) {
Client jerseyClient = new ApacheHttpClient(_clientHandler);
jerseyClient.addFilter(new HTTPBasicAuthFilter(username, password));
RESTClient restClient = new RESTClient(jerseyClient);
isilonApi = new IsilonApi(endpoint, restClient);
_clientMap.putIfAbsent(endpoint.toString() + ":" + username + ":" + password, isilonApi);
}
return isilonApi;
}
Aggregations