Skip to content

Commit c7d3385

Browse files
committed
Merge pull request #28 from indigo-dc/auth_header_builder
Auth header builder
2 parents 1e6740f + a087afe commit c7d3385

21 files changed

+1165
-204
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>es.upv.i3m.grycap</groupId>
55
<artifactId>im-java-api</artifactId>
6-
<version>0.4.4</version>
6+
<version>0.4.5</version>
77
<name>IM Java API</name>
88
<description>Java client for the REST API of the IM</description>
99

@@ -20,6 +20,7 @@
2020
<maven-javadoc-plugin.version>2.10.3</maven-javadoc-plugin.version>
2121
<maven-checkstyle-plugin.version>2.17</maven-checkstyle-plugin.version>
2222
<mockserver-netty.version>3.10.4</mockserver-netty.version>
23+
<jacoco-maven-plugin.version>0.7.7.201606060606</jacoco-maven-plugin.version>
2324
</properties>
2425

2526
<dependencies>
@@ -131,7 +132,7 @@
131132
<plugin>
132133
<groupId>org.jacoco</groupId>
133134
<artifactId>jacoco-maven-plugin</artifactId>
134-
<version>0.7.5.201505241946</version>
135+
<version>${jacoco-maven-plugin.version}</version>
135136
<executions>
136137
<execution>
137138
<id>default-prepare-agent</id>

src/main/java/es/upv/i3m/grycap/im/InfrastructureManager.java

Lines changed: 132 additions & 146 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package es.upv.i3m.grycap.im.auth;
2+
3+
import es.upv.i3m.grycap.im.auth.credential.Credential;
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
public class AuthorizationHeader {
10+
11+
private static final String ERROR_MESSAGE = "Credentials must not be null";
12+
private List<Credential<?>> credentials = new ArrayList<>();
13+
14+
public List<Credential<?>> getCredentials() {
15+
return credentials;
16+
}
17+
18+
/**
19+
* Sets the credentials information.
20+
*/
21+
public void setCredentialsAuthInfos(List<Credential<?>> credentials) {
22+
if (credentials == null) {
23+
throw new IllegalArgumentException(ERROR_MESSAGE);
24+
}
25+
this.credentials = credentials;
26+
}
27+
28+
public void addCredential(Credential<?> credential) {
29+
credentials.add(credential);
30+
}
31+
32+
/**
33+
* Returns a string with the credentials information.
34+
*/
35+
public String serialize() {
36+
StringBuilder sb = new StringBuilder();
37+
Iterator<Credential<?>> it = credentials.iterator();
38+
while (it.hasNext()) {
39+
String serializedAuthInfo = it.next().serialize();
40+
sb.append(serializedAuthInfo);
41+
if (it.hasNext()) {
42+
sb.append("\\n");
43+
}
44+
}
45+
return sb.toString();
46+
}
47+
48+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
public abstract class AbstractCredential<T extends AbstractCredential<T>>
4+
implements Credential<T> {
5+
6+
private String id;
7+
8+
protected <B extends AbstractCredentialBuilder<B, T>> AbstractCredential(
9+
B builder) {
10+
id = builder.getId();
11+
}
12+
13+
public String getId() {
14+
return id;
15+
}
16+
17+
public void setId(String id) {
18+
this.id = id;
19+
}
20+
21+
@Override
22+
public String serialize() {
23+
return serialize(null).toString();
24+
}
25+
26+
protected StringBuilder serialize(StringBuilder sb) {
27+
if (sb == null) {
28+
sb = new StringBuilder();
29+
}
30+
if (!isNullOrEmpty(id)) {
31+
sb.append("id = ").append(id).append(" ; ");
32+
}
33+
sb.append("type = ").append(getServiceType().getValue());
34+
return sb;
35+
}
36+
37+
//@formatter:off
38+
public abstract static class AbstractCredentialBuilder
39+
<B extends AbstractCredentialBuilder<B, T>, T extends AbstractCredential<T>>
40+
implements CredentialBuilder<T> {
41+
//@formatter:on
42+
43+
private String id;
44+
45+
public String getId() {
46+
return id;
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
public B withId(String id) {
51+
this.id = id;
52+
return (B) this;
53+
}
54+
}
55+
56+
public static boolean isNullOrEmpty(String string) {
57+
return (string == null) || string.isEmpty();
58+
}
59+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
public abstract class AbstractTokenCredential<T extends AbstractTokenCredential<T>>
4+
extends AbstractCredential<T> {
5+
6+
protected String token;
7+
8+
protected <B extends AbstractTokenCredentialBuilder<B, T>> AbstractTokenCredential(
9+
B builder) {
10+
super(builder);
11+
setToken(builder.getToken());
12+
}
13+
14+
public String getToken() {
15+
return token;
16+
}
17+
18+
private void setToken(String token) {
19+
if (isNullOrEmpty(token)) {
20+
throw new IllegalArgumentException("token must not be blank");
21+
}
22+
this.token = token;
23+
}
24+
25+
@Override
26+
protected StringBuilder serialize(StringBuilder sb) {
27+
sb = super.serialize(sb);
28+
sb.append(" ; token = ").append(token);
29+
return sb;
30+
}
31+
32+
//@formatter:off
33+
public abstract static class AbstractTokenCredentialBuilder
34+
<B extends AbstractTokenCredentialBuilder<B, T>, T extends AbstractTokenCredential<T>>
35+
extends AbstractCredentialBuilder<B, T> {
36+
//@formatter:on
37+
private String token;
38+
39+
@SuppressWarnings("unchecked")
40+
public B withToken(String token) {
41+
this.token = token;
42+
return (B) this;
43+
}
44+
45+
public String getToken() {
46+
return token;
47+
}
48+
}
49+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
//@formatter:off
4+
public abstract class AbstractUsernamePasswordCredential<T extends
5+
AbstractUsernamePasswordCredential<T>>
6+
extends AbstractCredential<T> {
7+
//@formatter:on
8+
9+
private String username;
10+
private String password;
11+
12+
//@formatter:off
13+
protected <B extends AbstractUsernamePasswordCredentialBuilder<B, T>>
14+
AbstractUsernamePasswordCredential(B builder) {
15+
//@formatter:on
16+
17+
super(builder);
18+
username = builder.getUsername();
19+
password = builder.getPassword();
20+
}
21+
22+
public String getUsername() {
23+
return username;
24+
}
25+
26+
public void setUsername(String username) {
27+
this.username = username;
28+
}
29+
30+
public String getPassword() {
31+
return password;
32+
}
33+
34+
public void setPassword(String password) {
35+
this.password = password;
36+
}
37+
38+
@Override
39+
public StringBuilder serialize(StringBuilder sb) {
40+
sb = super.serialize(sb);
41+
if (!isNullOrEmpty(username)) {
42+
sb.append(" ; username = ").append(username);
43+
}
44+
if (!isNullOrEmpty(password)) {
45+
sb.append(" ; password = ").append(password);
46+
}
47+
return sb;
48+
}
49+
50+
//@formatter:off
51+
public abstract static class AbstractUsernamePasswordCredentialBuilder
52+
<B extends AbstractUsernamePasswordCredentialBuilder<B, T>,
53+
T extends AbstractUsernamePasswordCredential<T>>
54+
extends AbstractCredentialBuilder<B, T> {
55+
//@formatter:on
56+
57+
private String username;
58+
private String password;
59+
60+
@SuppressWarnings("unchecked")
61+
public B withUsername(String username) {
62+
this.username = username;
63+
return (B) this;
64+
}
65+
66+
@SuppressWarnings("unchecked")
67+
public B withPassword(String password) {
68+
this.password = password;
69+
return (B) this;
70+
}
71+
72+
public String getUsername() {
73+
return username;
74+
}
75+
76+
public String getPassword() {
77+
return password;
78+
}
79+
80+
}
81+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
public interface Credential<T extends Credential<T>> {
4+
5+
public ServiceType getServiceType();
6+
7+
public String serialize();
8+
9+
public interface CredentialBuilder<T> {
10+
11+
public T build();
12+
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
public class DummyCredential extends AbstractCredential<DummyCredential> {
4+
5+
protected DummyCredential(DummyCredentialBuilder builder) {
6+
super(builder);
7+
}
8+
9+
@Override
10+
public ServiceType getServiceType() {
11+
return ServiceType.DUMMY;
12+
}
13+
14+
public static DummyCredentialBuilder getBuilder() {
15+
return new DummyCredentialBuilder();
16+
}
17+
18+
public static class DummyCredentialBuilder extends
19+
AbstractCredentialBuilder<DummyCredentialBuilder, DummyCredential> {
20+
21+
@Override
22+
public DummyCredential build() {
23+
return new DummyCredential(this);
24+
}
25+
26+
}
27+
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package es.upv.i3m.grycap.im.auth.credential;
2+
3+
public enum ServiceType {
4+
5+
//@formatter:off
6+
INFRASTRUCTURE_MANAGER("InfrastructureManager"),
7+
VMRC("VMRC"),
8+
DUMMY("Dummy"),
9+
OPENNEBULA("OpenNebula"),
10+
EC2("EC2"),
11+
FOG_BOW("FogBow"),
12+
OPENSTACK("OpenStack"),
13+
OCCI("OCCI"),
14+
LIB_CLOUD("LibCloud"),
15+
DOCKER("Docker"),
16+
GCE("GCE"),
17+
AZURE("Azure"),
18+
KUBERNETES("Kubernetes"),
19+
LIB_VIRT("LibVirt");
20+
//@formatter:on
21+
22+
private final String value;
23+
24+
ServiceType(String value) {
25+
this.value = value;
26+
}
27+
28+
public final String getValue() {
29+
return value;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)