-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from indigo-dc/auth_header_builder
Auth header builder
- Loading branch information
Showing
21 changed files
with
1,165 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
278 changes: 132 additions & 146 deletions
278
src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java
Large diffs are not rendered by default.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/main/java/es/upv/i3m/grycap/im/auth/AuthorizationHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractTokenCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/es/upv/i3m/grycap/im/auth/credential/AbstractUsernamePasswordCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/es/upv/i3m/grycap/im/auth/credential/Credential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/es/upv/i3m/grycap/im/auth/credential/DummyCredential.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/es/upv/i3m/grycap/im/auth/credential/ServiceType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.