Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wegener committed Oct 11, 2017
2 parents 13adef9 + 3fe8b51 commit e5a8613
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gdk-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>com.github.libgraviton</groupId>
<artifactId>gdk</artifactId>
<version>0.4.1</version>
<version>0.5.0</version>
</parent>
<artifactId>gdk-core</artifactId>
<name>gdk-core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.github.libgraviton.gdk.api.endpoint.exception.UnableToLoadEndpointAssociationsException;
import com.github.libgraviton.gdk.api.header.HeaderBag;
import com.github.libgraviton.gdk.api.query.rql.Rql;
import com.github.libgraviton.gdk.auth.HeaderAuth;
import com.github.libgraviton.gdk.auth.NoAuth;
import com.github.libgraviton.gdk.data.GravitonBase;
import com.github.libgraviton.gdk.exception.SerializationException;
import com.github.libgraviton.gdk.serialization.JsonPatcher;
Expand Down Expand Up @@ -48,6 +50,8 @@ public class GravitonApi {

private RequestExecutor executor;

private HeaderAuth auth;

public GravitonApi() {
setup();
this.baseUrl = properties.getProperty("graviton.base.url");
Expand All @@ -65,12 +69,18 @@ public GravitonApi(String baseUrl, EndpointManager endpointManager) {
this.endpointManager = endpointManager;
}

public GravitonApi(String baseUrl, GeneratedEndpointManager endpointManager, HeaderAuth auth) {
this(baseUrl, endpointManager);
this.auth = auth;
}

protected void setup() {
try {
this.properties = PropertiesLoader.load();
} catch (IOException e) {
throw new IllegalStateException("Unable to load properties files.", e);
}
this.auth = new NoAuth();
this.objectMapper = new GravitonObjectMapper(properties);
this.executor = new RequestExecutor(objectMapper);
}
Expand Down Expand Up @@ -234,8 +244,11 @@ public ObjectMapper getObjectMapper() {

// TODO make it configurable
protected HeaderBag.Builder getDefaultHeaders() {
return new HeaderBag.Builder()
HeaderBag.Builder headerBuilder = new HeaderBag.Builder()
.set("Content-Type", "application/json")
.set("Accept", "application/json");
auth.addHeader(headerBuilder);

return headerBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.libgraviton.gdk.auth;

import com.github.libgraviton.gdk.api.header.HeaderBag;
import okio.ByteString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;


public class BasicAuth implements HeaderAuth {

private static final Logger LOG = LoggerFactory.getLogger(BasicAuth.class);

private String username;
private String password;

public BasicAuth(String username, String password) {
this.username = username;
this.password = password;
}

@Override
public void addHeader(HeaderBag.Builder headerBuilder) {
LOG.debug("Add basic auth header to request");
headerBuilder.set("Authorization", basic());
}

public String basic() {
return basic(Charset.forName("ISO-8859-1"));
}

public String basic(Charset charset) {
String usernameAndPassword = username + ":" + password;
byte[] bytes = usernameAndPassword.getBytes(charset);
String encoded = ByteString.of(bytes).base64();
return "Basic " + encoded;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.libgraviton.gdk.auth;

import com.github.libgraviton.gdk.api.header.HeaderBag;

public interface HeaderAuth {

void addHeader(HeaderBag.Builder headerBuilder);
}
15 changes: 15 additions & 0 deletions gdk-core/src/main/java/com/github/libgraviton/gdk/auth/NoAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.libgraviton.gdk.auth;

import com.github.libgraviton.gdk.api.header.HeaderBag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NoAuth implements HeaderAuth {

private static final Logger LOG = LoggerFactory.getLogger(NoAuth.class);

@Override
public void addHeader(HeaderBag.Builder headerBuilder) {
LOG.debug("No auth selected.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.libgraviton.gdk.auth;

import com.github.libgraviton.gdk.api.header.Header;
import com.github.libgraviton.gdk.api.header.HeaderBag;
import com.github.libgraviton.gdk.exception.CommunicationException;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class BasicAuthTest {

@Test
public void testEncodedCredentials() throws CommunicationException {
String username = "asdf";
String password = "112233";
BasicAuth auth = new BasicAuth(username, password);

String encodedCredentials = auth.basic();
assertEquals("Basic YXNkZjoxMTIyMzM=", encodedCredentials);
}

@Test
public void testAddedHeader() throws CommunicationException {
String username = "asdf";
String password = "112233";
BasicAuth auth = new BasicAuth(username, password);

HeaderBag.Builder builder = new HeaderBag.Builder();
auth.addHeader(builder);

Map<String, Header> headers = builder.build().all();
assertEquals(1, headers.size());
assertNotNull(headers.get("Authorization"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.libgraviton.gdk.auth;

import com.github.libgraviton.gdk.api.header.Header;
import com.github.libgraviton.gdk.api.header.HeaderBag;
import com.github.libgraviton.gdk.exception.CommunicationException;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class NoAuthTest {

@Test
public void testNoAddedHeader() throws CommunicationException {
NoAuth auth = new NoAuth();

HeaderBag.Builder builder = new HeaderBag.Builder();
auth.addHeader(builder);

Map<String, Header> headers = builder.build().all();
assertEquals(0, headers.size());
}
}
4 changes: 2 additions & 2 deletions gdk-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.github.libgraviton</groupId>
<artifactId>gdk</artifactId>
<version>0.4.1</version>
<version>0.5.0</version>
</parent>
<artifactId>gdk-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
Expand Down Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.github.libgraviton</groupId>
<artifactId>gdk-core</artifactId>
<version>0.4.1</version>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.jsonschema2pojo</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.libgraviton.gdk.api.endpoint.EndpointInclusionStrategy;
import com.github.libgraviton.gdk.api.endpoint.GeneratedEndpointManager;
import com.github.libgraviton.gdk.api.endpoint.exception.UnableToLoadEndpointAssociationsException;
import com.github.libgraviton.gdk.auth.BasicAuth;
import com.github.libgraviton.gdk.generator.Generator;
import com.github.libgraviton.gdk.generator.exception.GeneratorException;
import com.github.libgraviton.gdk.generator.instructionloader.grvprofile.GrvProfileInstructionLoader;
Expand All @@ -22,6 +23,12 @@ public class GenerateMojo extends Jsonschema2PojoMojo {
@Parameter(required = true)
private String gravitonUrl;

@Parameter(required = false)
private String username;

@Parameter(required = false)
private String password;

@Parameter(required = false)
private String endpointBlacklistPath;

Expand All @@ -43,7 +50,8 @@ public void execute() throws MojoExecutionException
endpointManager.setEndpointInclusionStrategy(getEndpointInclusionStrategy());
GravitonApi gravitonApi = new GravitonApi(
gravitonUrl,
endpointManager
endpointManager,
new BasicAuth(username, password)
);
Generator generator = new Generator(
generatorConfig,
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.libgraviton</groupId>
<artifactId>gdk</artifactId>
<version>0.4.1</version>
<version>0.5.0</version>
<packaging>pom</packaging>
<name>gdk</name>
<description>Graviton Development Kit</description>
Expand Down

0 comments on commit e5a8613

Please sign in to comment.