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

Commit

Permalink
Implemented APIKeys, as well as getting to conform to check style
Browse files Browse the repository at this point in the history
  • Loading branch information
jecortez committed Oct 6, 2015
1 parent 1b958b8 commit c58b03f
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ reports/

# Local database files
*.db

# YAML
*.yaml
5 changes: 2 additions & 3 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
<parent>
<groupId>com.mainstreethub.project</groupId>
<artifactId>parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</parent>

<name>api</name>
<artifactId>api</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.api.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mainstreethub.project;
package com.mainstreethub.project.api.v1;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -9,6 +9,13 @@ public class CreateUserRequest {
private final String last;
private final String password;

/**
* Creates a user request based on passed in parameters.
* @param username username of request to create
* @param first first name of user
* @param last last name of user
* @param password user's password
*/
@JsonCreator
public CreateUserRequest(@JsonProperty("username") String username,
@JsonProperty("first") String first,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mainstreethub.project;
package com.mainstreethub.project.api.v1;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -8,6 +8,12 @@ public class User {
private final String first;
private final String last;

/**
* User object.
* @param username User's username
* @param first User's first name
* @param last User's last name.
*/
@JsonCreator
public User(@JsonProperty("username") String username,
@JsonProperty("first") String first,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.mainstreethub.project;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Resources;
import com.mainstreethub.project.api.v1.User;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.testing.FixtureHelpers;
import java.io.IOException;
Expand Down
51 changes: 51 additions & 0 deletions application/config.yaml.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This is the development configuration for the Demo Application.
# The actual production and test configs are generated at deployment time.
# Make sure to update deployment configs in the ops repo when updating the
# Configuration class. Make

database:
# the name of your JDBC driver
driverClass: com.mysql.jdbc.Driver

# the username - Make a copy of this file and change the value for dev work but don't check it in
user: CHANGEME

# the password - Make a copy of this file and change the value for dev work but don't check it in
password: CHANGEMETOO

# the JDBC URL
url: jdbc:mysql://CHANGEMETHREE

# the maximum amount of time to wait on an empty pool before throwing an exception
maxWaitForConnection: 1s

# the SQL query to run when validating a connection's liveness
validationQuery: "/* Demo Health Check */ SELECT 1"

# the timeout before a connection validation queries fail
validationQueryTimeout: 3s

# the minimum number of connections to keep open
minSize: 8

# the maximum number of connections to keep open
maxSize: 32

# whether or not idle connections should be validated
checkConnectionWhileIdle: false

# the amount of time to sleep between runs of the idle connection validation, abandoned cleaner and idle pool resizing
evictionInterval: 10s

# the minimum amount of time an connection must sit idle in the pool before it is eligible for eviction
minIdleTime: 1 minute

# whether or not connections will be validated before being borrowed from the pool
checkConnectionOnBorrow: true

authentication:
basic-http:
cache-spec: maximumSize=1000, expireAfterAccess=10m
realm: ProjectApplication
keys:
testKey: CHANGEME
12 changes: 10 additions & 2 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<parent>
<groupId>com.mainstreethub.project</groupId>
<artifactId>parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</parent>

<name>application</name>
<artifactId>application</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
Expand Down Expand Up @@ -70,6 +70,14 @@
<artifactId>api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard-bundles</groupId>
<artifactId>dropwizard-api-key-bundle</artifactId>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package com.mainstreethub.project;

import com.mainstreethub.project.dao.JDBIModule;
import com.mainstreethub.project.dao.JdbiModule;

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.bundles.apikey.ApiKeyBundle;
import io.dropwizard.bundles.version.VersionBundle;
import io.dropwizard.bundles.version.VersionSupplier;
import io.dropwizard.bundles.version.suppliers.MavenVersionSupplier;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;


public class ProjectApplication extends Application<ProjectConfiguration> {
public static void main(String[] args) throws Exception {
new ProjectApplication().run(args);
}


@Override
public void initialize(Bootstrap<ProjectConfiguration> bootstrap) {
initVersion(bootstrap);
initVersion(bootstrap);
bootstrap.addBundle(new ApiKeyBundle<ProjectConfiguration>());
}

@Override
public void run(ProjectConfiguration configuration, Environment environment) throws Exception {
ProjectComponent component = DaggerProjectComponent.builder()
.jDBIModule(new JDBIModule(configuration.getDatabase(), environment))
.projectModule(new ProjectModule())
.build();

.jdbiModule(new JdbiModule(configuration.getDatabase(), environment))
.projectModule(new ProjectModule())
.build();
environment.jersey().register(component.getUsersResource());
}

private void initVersion(Bootstrap<ProjectConfiguration> bootstrap) {
VersionSupplier supplier = new MavenVersionSupplier("com.mainstreethub.project", "application");
bootstrap.addBundle(new VersionBundle(supplier));
VersionSupplier supplier = new MavenVersionSupplier("com.mainstreethub.project", "application");
bootstrap.addBundle(new VersionBundle(supplier));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mainstreethub.project;

import com.mainstreethub.project.dao.JDBIModule;
import com.mainstreethub.project.resources.UsersResource;
import com.mainstreethub.project.dao.JdbiModule;
import com.mainstreethub.project.resources.v1.UsersResource;
import dagger.Component;
import javax.inject.Singleton;

@Singleton
@Component(modules = {JDBIModule.class, ProjectModule.class})
@Component(modules = {JdbiModule.class, ProjectModule.class})
public interface ProjectComponent {
UsersResource getUsersResource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;
import io.dropwizard.bundles.apikey.ApiKeyBundleConfiguration;
import io.dropwizard.bundles.apikey.ApiKeyConfiguration;
import io.dropwizard.db.DataSourceFactory;

public class ProjectConfiguration extends Configuration {
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public class ProjectConfiguration extends Configuration implements ApiKeyBundleConfiguration{
@JsonProperty("database")
private DataSourceFactory database = new DataSourceFactory();

@JsonProperty("authentication")
@Valid
@NotNull
private ApiKeyConfiguration apiKeyConfiguration;

public DataSourceFactory getDatabase() {
return database;
}

@Override
public ApiKeyConfiguration getApiKeyConfiguration() {
return apiKeyConfiguration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
import static com.google.common.base.Preconditions.checkNotNull;

@Module
public class JDBIModule {
public class JdbiModule {
private final DataSourceFactory configuration;
private final Environment environment;

public JDBIModule(DataSourceFactory configuration, Environment environment) {
public JdbiModule(DataSourceFactory configuration, Environment environment) {
this.configuration = checkNotNull(configuration);
this.environment = checkNotNull(environment);
}

@Singleton
@Provides
public DBIFactory provideDBIFactory() {
public DBIFactory provideDbiFactory() {
return new DBIFactory();
}

@Singleton
@Provides
public DBI provideDBI(DBIFactory factory) {
public DBI provideDbi(DBIFactory factory) {
return factory.build(environment, configuration, "db");
}

@Singleton
@Provides
public UserDAO provideUserDAO(DBI dbi) {
return dbi.onDemand(UserDAO.class);
public UserDao provideUserDao(DBI dbi) {
return dbi.onDemand(UserDao.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.skife.jdbi.v2.sqlobject.customizers.Mapper;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

public interface UserDAO {
public interface UserDao {
@SqlUpdate("INSERT INTO users (username, first, last, salt, hash) "
+ "VALUES (:username, :first, :last, :salt, :hash)")
@GetGeneratedKeys
Expand All @@ -22,7 +22,7 @@ long createUser(@Bind("username") String username,

@SqlQuery("SELECT username, first, last FROM users WHERE id=:id")
@Mapper(UserMapper.class)
DBUser findUserById(@Bind("id") long id);
DbUser findUserById(@Bind("id") long id);

@SqlUpdate("DELETE FROM users WHERE id=:id")
int deleteUser(@Bind("id") long id);
Expand All @@ -32,22 +32,22 @@ int updatePassword(@Bind("id") long id,
@Bind("salt") String salt,
@Bind("hash") String hash);

static final class DBUser {
static final class DbUser {
public final String username;
public final String first;
public final String last;

public DBUser(String username, String first, String last) {
public DbUser(String username, String first, String last) {
this.username = username;
this.first = first;
this.last = last;
}
}

static final class UserMapper implements ResultSetMapper<DBUser> {
static final class UserMapper implements ResultSetMapper<DbUser> {
@Override
public DBUser map(int index, ResultSet r, StatementContext ctx) throws SQLException {
return new DBUser(r.getString("username"), r.getString("first"), r.getString("last"));
public DbUser map(int index, ResultSet r, StatementContext ctx) throws SQLException {
return new DbUser(r.getString("username"), r.getString("first"), r.getString("last"));
}
}
}
Loading

0 comments on commit c58b03f

Please sign in to comment.