Skip to content

Commit

Permalink
Bulk application of code formatting, rather than having other commits…
Browse files Browse the repository at this point in the history
… break
  • Loading branch information
justinsb committed Apr 14, 2012
1 parent 07d6343 commit e835e93
Show file tree
Hide file tree
Showing 723 changed files with 34,693 additions and 33,804 deletions.
@@ -1,5 +1,5 @@
package org.openstack.keystone.auth.client;

public class Keystone {
public static final String AUTH_HEADER = "X-Auth-Token";
public static final String AUTH_HEADER = "X-Auth-Token";
}
Expand Up @@ -25,102 +25,105 @@
import org.slf4j.LoggerFactory;

public class KeystoneAuthenticationClient {
static final Logger log = LoggerFactory.getLogger(KeystoneAuthenticationClient.class);

final String authenticationUrl;

public static final String DEFAULT_AUTHENTICATION_URL = "http://127.0.0.1:" + WellKnownPorts.PORT_PLATFORMLAYER_AUTH_USER + "/v2.0/";

public static final Integer HTTP_500_ERROR = new Integer(500);

protected static final int MAX_RETRIES = 10;

static Random random = new Random();

public KeystoneAuthenticationClient(String authenticationUrl) {
this.authenticationUrl = authenticationUrl;
}

public KeystoneAuthenticationClient() {
this(DEFAULT_AUTHENTICATION_URL);
}

public TenantsList listTenants(KeystoneAuthenticationToken token) throws KeystoneAuthenticationException {
return doSimpleRequest(token, "GET", "tokens", null, TenantsList.class);
}

public KeystoneAuthenticationToken authenticate(String tenantName, PasswordCredentials passwordCredentials) throws KeystoneAuthenticationException {
Auth auth = new Auth();
auth.setPasswordCredentials(passwordCredentials);
auth.setTenantName(tenantName);

AuthenticateRequest request = new AuthenticateRequest();
request.setAuth(auth);

AuthenticateResponse response = doSimpleRequest(null, "POST", "tokens", request, AuthenticateResponse.class);
return new KeystoneAuthenticationToken(response.getAccess());
}

private <T> T doSimpleRequest(KeystoneAuthenticationToken token, String method, String relativeUri, Object postObject, Class<T> responseClass) throws KeystoneAuthenticationException {
try {
URI uri = new URI(authenticationUrl + relativeUri);

SimpleHttpRequest httpRequest = SimpleHttpRequest.build(method, uri);

httpRequest.setRequestHeader("Accept", "application/xml");

if (token != null) {
token.populateRequest(httpRequest);
}

if (postObject != null) {
httpRequest.setRequestHeader("Content-Type", "application/xml");
String xml = serializeXml(postObject);
httpRequest.getOutputStream().write(Utf8.getBytes(xml));
}

SimpleHttpResponse response = httpRequest.doRequest();

int responseCode = response.getHttpResponseCode();
switch (responseCode) {
case 401:
throw new KeystoneAuthenticationException("Platformlayer credentials were not correct");

case 200:
case 203: {
if (responseClass.equals(String.class)) {
return CastUtils.as(IoUtils.readAll(response.getInputStream()), responseClass);
} else {
return deserializeXml(response.getInputStream(), responseClass);
}
}

default:
throw new KeystoneAuthenticationException("Unexpected result code: " + responseCode);
}
} catch (IOException e) {
throw new KeystoneAuthenticationException("Error communicating with authentication service", e);
} catch (URISyntaxException e) {
throw new KeystoneAuthenticationException("Error building authentication URI", e);
}

}

public static <T> T deserializeXml(InputStream is, Class<T> clazz) throws KeystoneAuthenticationException {
try {
return JaxbHelper.deserializeXmlObject(is, clazz, true);
} catch (UnmarshalException e) {
throw new KeystoneAuthenticationException("Error reading authentication response data", e);
}
}

public static String serializeXml(Object object) throws KeystoneAuthenticationException {
try {
boolean formatted = false;
return JaxbHelper.toXml(object, formatted);
} catch (JAXBException e) {
throw new KeystoneAuthenticationException("Error serializing data", e);
}
}
static final Logger log = LoggerFactory.getLogger(KeystoneAuthenticationClient.class);

final String authenticationUrl;

public static final String DEFAULT_AUTHENTICATION_URL = "http://127.0.0.1:"
+ WellKnownPorts.PORT_PLATFORMLAYER_AUTH_USER + "/v2.0/";

public static final Integer HTTP_500_ERROR = new Integer(500);

protected static final int MAX_RETRIES = 10;

static Random random = new Random();

public KeystoneAuthenticationClient(String authenticationUrl) {
this.authenticationUrl = authenticationUrl;
}

public KeystoneAuthenticationClient() {
this(DEFAULT_AUTHENTICATION_URL);
}

public TenantsList listTenants(KeystoneAuthenticationToken token) throws KeystoneAuthenticationException {
return doSimpleRequest(token, "GET", "tokens", null, TenantsList.class);
}

public KeystoneAuthenticationToken authenticate(String tenantName, PasswordCredentials passwordCredentials)
throws KeystoneAuthenticationException {
Auth auth = new Auth();
auth.setPasswordCredentials(passwordCredentials);
auth.setTenantName(tenantName);

AuthenticateRequest request = new AuthenticateRequest();
request.setAuth(auth);

AuthenticateResponse response = doSimpleRequest(null, "POST", "tokens", request, AuthenticateResponse.class);
return new KeystoneAuthenticationToken(response.getAccess());
}

private <T> T doSimpleRequest(KeystoneAuthenticationToken token, String method, String relativeUri,
Object postObject, Class<T> responseClass) throws KeystoneAuthenticationException {
try {
URI uri = new URI(authenticationUrl + relativeUri);

SimpleHttpRequest httpRequest = SimpleHttpRequest.build(method, uri);

httpRequest.setRequestHeader("Accept", "application/xml");

if (token != null) {
token.populateRequest(httpRequest);
}

if (postObject != null) {
httpRequest.setRequestHeader("Content-Type", "application/xml");
String xml = serializeXml(postObject);
httpRequest.getOutputStream().write(Utf8.getBytes(xml));
}

SimpleHttpResponse response = httpRequest.doRequest();

int responseCode = response.getHttpResponseCode();
switch (responseCode) {
case 401:
throw new KeystoneAuthenticationException("Platformlayer credentials were not correct");

case 200:
case 203: {
if (responseClass.equals(String.class)) {
return CastUtils.as(IoUtils.readAll(response.getInputStream()), responseClass);
} else {
return deserializeXml(response.getInputStream(), responseClass);
}
}

default:
throw new KeystoneAuthenticationException("Unexpected result code: " + responseCode);
}
} catch (IOException e) {
throw new KeystoneAuthenticationException("Error communicating with authentication service", e);
} catch (URISyntaxException e) {
throw new KeystoneAuthenticationException("Error building authentication URI", e);
}

}

public static <T> T deserializeXml(InputStream is, Class<T> clazz) throws KeystoneAuthenticationException {
try {
return JaxbHelper.deserializeXmlObject(is, clazz, true);
} catch (UnmarshalException e) {
throw new KeystoneAuthenticationException("Error reading authentication response data", e);
}
}

public static String serializeXml(Object object) throws KeystoneAuthenticationException {
try {
boolean formatted = false;
return JaxbHelper.toXml(object, formatted);
} catch (JAXBException e) {
throw new KeystoneAuthenticationException("Error serializing data", e);
}
}

}
Expand Up @@ -3,13 +3,13 @@
import org.platformlayer.auth.OpenstackAuthenticationException;

public class KeystoneAuthenticationException extends OpenstackAuthenticationException {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;

public KeystoneAuthenticationException(String message) {
super(message);
}
public KeystoneAuthenticationException(String message) {
super(message);
}

public KeystoneAuthenticationException(String message, Exception e) {
super(message, e);
}
public KeystoneAuthenticationException(String message, Exception e) {
super(message, e);
}
}
Expand Up @@ -9,37 +9,39 @@
import com.google.common.base.Objects;

public class KeystoneAuthenticationToken implements AuthenticationToken {
private final Access access;

public KeystoneAuthenticationToken(Access access) {
this.access = access;
}

public String getAuthTokenValue() {
return access.getToken().getId();
}

@Override
public String getServiceUrl(String serviceKey) {
for (Service service : access.getServiceCatalog()) {
if (Objects.equal(service.getType(), serviceKey)) {
String bestUrl = null;
for (ServiceEndpoint endpoint : service.getEndpoints()) {
bestUrl = endpoint.getPublicURL();
if (bestUrl != null)
break;
}

if (bestUrl != null)
return bestUrl;
}
}
return null;
}

@Override
public void populateRequest(SimpleHttpRequest httpRequest) {
httpRequest.setRequestHeader("X-Auth-Token", getAuthTokenValue());
}
private final Access access;

public KeystoneAuthenticationToken(Access access) {
this.access = access;
}

public String getAuthTokenValue() {
return access.getToken().getId();
}

@Override
public String getServiceUrl(String serviceKey) {
for (Service service : access.getServiceCatalog()) {
if (Objects.equal(service.getType(), serviceKey)) {
String bestUrl = null;
for (ServiceEndpoint endpoint : service.getEndpoints()) {
bestUrl = endpoint.getPublicURL();
if (bestUrl != null) {
break;
}
}

if (bestUrl != null) {
return bestUrl;
}
}
}
return null;
}

@Override
public void populateRequest(SimpleHttpRequest httpRequest) {
httpRequest.setRequestHeader("X-Auth-Token", getAuthTokenValue());
}

}
Expand Up @@ -8,49 +8,49 @@
import org.platformlayer.auth.Authenticator;

public class KeystoneAuthenticator implements Authenticator {
final String tenantId;

final String username;
final String password;

final KeystoneAuthenticationClient client;

AuthenticationToken token = null;

public KeystoneAuthenticator(String tenantId, String username, String password, String server) {
this.tenantId = tenantId;
this.username = username;
this.password = password;
String authenticationUrl = server != null ? server : KeystoneAuthenticationClient.DEFAULT_AUTHENTICATION_URL;

this.client = new KeystoneAuthenticationClient(authenticationUrl);
}

@Override
public AuthenticationToken getAuthenticationToken() throws KeystoneAuthenticationException {
if (token == null) {
PasswordCredentials passwordCredentials = new PasswordCredentials();
passwordCredentials.setUsername(username);
passwordCredentials.setPassword(password);

token = client.authenticate(tenantId, passwordCredentials);
}
return token;
}

@Override
public void clearAuthenticationToken() {
token = null;
}

@Override
public String getHost() {
try {
URL url = new URL(client.authenticationUrl);
return url.getHost();
} catch (MalformedURLException e) {
throw new IllegalStateException("Error parsing URL", e);
}
}
final String tenantId;

final String username;
final String password;

final KeystoneAuthenticationClient client;

AuthenticationToken token = null;

public KeystoneAuthenticator(String tenantId, String username, String password, String server) {
this.tenantId = tenantId;
this.username = username;
this.password = password;
String authenticationUrl = server != null ? server : KeystoneAuthenticationClient.DEFAULT_AUTHENTICATION_URL;

this.client = new KeystoneAuthenticationClient(authenticationUrl);
}

@Override
public AuthenticationToken getAuthenticationToken() throws KeystoneAuthenticationException {
if (token == null) {
PasswordCredentials passwordCredentials = new PasswordCredentials();
passwordCredentials.setUsername(username);
passwordCredentials.setPassword(password);

token = client.authenticate(tenantId, passwordCredentials);
}
return token;
}

@Override
public void clearAuthenticationToken() {
token = null;
}

@Override
public String getHost() {
try {
URL url = new URL(client.authenticationUrl);
return url.getHost();
} catch (MalformedURLException e) {
throw new IllegalStateException("Error parsing URL", e);
}
}

}

0 comments on commit e835e93

Please sign in to comment.