Skip to content

Commit

Permalink
Merge pull request #28 from indigo-dc/auth_header_builder
Browse files Browse the repository at this point in the history
Auth header builder
  • Loading branch information
Alfonso Pérez committed Jun 9, 2016
2 parents 1e6740f + a087afe commit c7d3385
Show file tree
Hide file tree
Showing 21 changed files with 1,165 additions and 204 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.upv.i3m.grycap</groupId>
<artifactId>im-java-api</artifactId>
<version>0.4.4</version>
<version>0.4.5</version>
<name>IM Java API</name>
<description>Java client for the REST API of the IM</description>

Expand All @@ -20,6 +20,7 @@
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
<maven-checkstyle-plugin.version>2.17</maven-checkstyle-plugin.version>
<mockserver-netty.version>3.10.4</mockserver-netty.version>
<jacoco-maven-plugin.version>0.7.7.201606060606</jacoco-maven-plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -131,7 +132,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<version>${jacoco-maven-plugin.version}</version>
<executions>
<execution>
<id>default-prepare-agent</id>
Expand Down
278 changes: 132 additions & 146 deletions src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package es.upv.i3m.grycap.im.auth;

import es.upv.i3m.grycap.im.auth.credential.Credential;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AuthorizationHeader {

private static final String ERROR_MESSAGE = "Credentials must not be null";
private List<Credential<?>> credentials = new ArrayList<>();

public List<Credential<?>> getCredentials() {
return credentials;
}

/**
* Sets the credentials information.
*/
public void setCredentialsAuthInfos(List<Credential<?>> credentials) {
if (credentials == null) {
throw new IllegalArgumentException(ERROR_MESSAGE);
}
this.credentials = credentials;
}

public void addCredential(Credential<?> credential) {
credentials.add(credential);
}

/**
* Returns a string with the credentials information.
*/
public String serialize() {
StringBuilder sb = new StringBuilder();
Iterator<Credential<?>> it = credentials.iterator();
while (it.hasNext()) {
String serializedAuthInfo = it.next().serialize();
sb.append(serializedAuthInfo);
if (it.hasNext()) {
sb.append("\\n");
}
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package es.upv.i3m.grycap.im.auth.credential;

public abstract class AbstractCredential<T extends AbstractCredential<T>>
implements Credential<T> {

private String id;

protected <B extends AbstractCredentialBuilder<B, T>> AbstractCredential(
B builder) {
id = builder.getId();
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public String serialize() {
return serialize(null).toString();
}

protected StringBuilder serialize(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
if (!isNullOrEmpty(id)) {
sb.append("id = ").append(id).append(" ; ");
}
sb.append("type = ").append(getServiceType().getValue());
return sb;
}

//@formatter:off
public abstract static class AbstractCredentialBuilder
<B extends AbstractCredentialBuilder<B, T>, T extends AbstractCredential<T>>
implements CredentialBuilder<T> {
//@formatter:on

private String id;

public String getId() {
return id;
}

@SuppressWarnings("unchecked")
public B withId(String id) {
this.id = id;
return (B) this;
}
}

public static boolean isNullOrEmpty(String string) {
return (string == null) || string.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package es.upv.i3m.grycap.im.auth.credential;

public abstract class AbstractTokenCredential<T extends AbstractTokenCredential<T>>
extends AbstractCredential<T> {

protected String token;

protected <B extends AbstractTokenCredentialBuilder<B, T>> AbstractTokenCredential(
B builder) {
super(builder);
setToken(builder.getToken());
}

public String getToken() {
return token;
}

private void setToken(String token) {
if (isNullOrEmpty(token)) {
throw new IllegalArgumentException("token must not be blank");
}
this.token = token;
}

@Override
protected StringBuilder serialize(StringBuilder sb) {
sb = super.serialize(sb);
sb.append(" ; token = ").append(token);
return sb;
}

//@formatter:off
public abstract static class AbstractTokenCredentialBuilder
<B extends AbstractTokenCredentialBuilder<B, T>, T extends AbstractTokenCredential<T>>
extends AbstractCredentialBuilder<B, T> {
//@formatter:on
private String token;

@SuppressWarnings("unchecked")
public B withToken(String token) {
this.token = token;
return (B) this;
}

public String getToken() {
return token;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package es.upv.i3m.grycap.im.auth.credential;

//@formatter:off
public abstract class AbstractUsernamePasswordCredential<T extends
AbstractUsernamePasswordCredential<T>>
extends AbstractCredential<T> {
//@formatter:on

private String username;
private String password;

//@formatter:off
protected <B extends AbstractUsernamePasswordCredentialBuilder<B, T>>
AbstractUsernamePasswordCredential(B builder) {
//@formatter:on

super(builder);
username = builder.getUsername();
password = builder.getPassword();
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public StringBuilder serialize(StringBuilder sb) {
sb = super.serialize(sb);
if (!isNullOrEmpty(username)) {
sb.append(" ; username = ").append(username);
}
if (!isNullOrEmpty(password)) {
sb.append(" ; password = ").append(password);
}
return sb;
}

//@formatter:off
public abstract static class AbstractUsernamePasswordCredentialBuilder
<B extends AbstractUsernamePasswordCredentialBuilder<B, T>,
T extends AbstractUsernamePasswordCredential<T>>
extends AbstractCredentialBuilder<B, T> {
//@formatter:on

private String username;
private String password;

@SuppressWarnings("unchecked")
public B withUsername(String username) {
this.username = username;
return (B) this;
}

@SuppressWarnings("unchecked")
public B withPassword(String password) {
this.password = password;
return (B) this;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

}
}
14 changes: 14 additions & 0 deletions src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package es.upv.i3m.grycap.im.auth.credential;

public interface Credential<T extends Credential<T>> {

public ServiceType getServiceType();

public String serialize();

public interface CredentialBuilder<T> {

public T build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package es.upv.i3m.grycap.im.auth.credential;

public class DummyCredential extends AbstractCredential<DummyCredential> {

protected DummyCredential(DummyCredentialBuilder builder) {
super(builder);
}

@Override
public ServiceType getServiceType() {
return ServiceType.DUMMY;
}

public static DummyCredentialBuilder getBuilder() {
return new DummyCredentialBuilder();
}

public static class DummyCredentialBuilder extends
AbstractCredentialBuilder<DummyCredentialBuilder, DummyCredential> {

@Override
public DummyCredential build() {
return new DummyCredential(this);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package es.upv.i3m.grycap.im.auth.credential;

public enum ServiceType {

//@formatter:off
INFRASTRUCTURE_MANAGER("InfrastructureManager"),
VMRC("VMRC"),
DUMMY("Dummy"),
OPENNEBULA("OpenNebula"),
EC2("EC2"),
FOG_BOW("FogBow"),
OPENSTACK("OpenStack"),
OCCI("OCCI"),
LIB_CLOUD("LibCloud"),
DOCKER("Docker"),
GCE("GCE"),
AZURE("Azure"),
KUBERNETES("Kubernetes"),
LIB_VIRT("LibVirt");
//@formatter:on

private final String value;

ServiceType(String value) {
this.value = value;
}

public final String getValue() {
return value;
}

}
Loading

0 comments on commit c7d3385

Please sign in to comment.