Skip to content

Commit

Permalink
Updated blogging example to eliminate Routes class (and latest versio…
Browse files Browse the repository at this point in the history
…n of RestExpress)
  • Loading branch information
tfredrich committed Jun 11, 2012
1 parent 82211bc commit 38feedd
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 107 deletions.
Binary file not shown.
Binary file not shown.
100 changes: 76 additions & 24 deletions examples/blogging/src/java/com/blogging/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@
package com.blogging;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.blogging.controller.BlogController;
import com.blogging.controller.BlogEntryController;
import com.blogging.controller.CommentController;
import com.blogging.persistence.BlogEntryRepository;
import com.blogging.persistence.BlogRepository;
import com.blogging.persistence.CommentRepository;
import com.blogging.persistence.MongoBlogEntryRepository;
import com.blogging.persistence.MongoBlogRepository;
import com.blogging.persistence.MongoCommentRepository;
import com.mongodb.Mongo;
import com.mongodb.ServerAddress;
import com.strategicgains.restexpress.Format;
import com.strategicgains.restexpress.RestExpress;
import com.strategicgains.restexpress.exception.ConfigurationException;
import com.strategicgains.restexpress.util.Environment;

/**
Expand All @@ -50,7 +56,14 @@ public class Configuration
private String name;
private String persistence;
private String defaultFormat;
private List<ServerAddress> bootstraps;

private BlogController blogController;
private BlogEntryController entryController;
private CommentController commentController;

private BlogRepository blogRepository;
private BlogEntryRepository entryRepository;
private CommentRepository commentRepository;

@Override
protected void fillValues(Properties p)
Expand All @@ -60,13 +73,37 @@ protected void fillValues(Properties p)
this.persistence = p.getProperty(PERSISTENCE_PROPERTY, MONGODB_PERSISTENCE);
this.defaultFormat = p.getProperty(DEFAULT_FORMAT_PROPERTY, Format.JSON);
String bootstrapString = p.getProperty(BOOTSTRAPS_PROPERTY);
Mongo mongo = null;

if (bootstrapString != null)
try
{
if (bootstrapString != null)
{
mongo = new Mongo(parseBootstraps(bootstrapString));
}
else
{
mongo = new Mongo();
}
}
catch(UnknownHostException e)
{
bootstraps = parseBootstraps(bootstrapString);
throw new ConfigurationException(e);
}

initialize(mongo);
}

private void initialize(Mongo mongo)
{
blogRepository = createBlogRespository(mongo);
entryRepository = createEntryRespository(mongo);
commentRepository = createCommentRespository(mongo);
blogController = createBlogController();
entryController = createEntryController();
commentController = createCommentController();
}

public int getPort()
{
return port;
Expand All @@ -81,47 +118,62 @@ public String getDefaultFormat()
{
return defaultFormat;
}

public BlogController getBlogController()
{
return blogController;
}

public BlogEntryController getEntryController()
{
return entryController;
}

public CommentController getCommentController()
{
return commentController;
}

private BlogController createBlogController()
{
return new BlogController(blogRepository);
}

private BlogEntryController createEntryController()
{
return new BlogEntryController(entryRepository);
}

private CommentController createCommentController()
{
return new CommentController(commentRepository);
}

public BlogRepository getBlogRespository()
private BlogRepository createBlogRespository(Mongo mongo)
{
if (MONGODB_PERSISTENCE.equals(persistence))
{
if (bootstraps == null)
{
throw new RuntimeException("MongoDB bootstraps not set");
}

return new MongoBlogRepository(bootstraps);
return new MongoBlogRepository(mongo);
}

return null;
}

public BlogEntryRepository getEntriesRespository()
private BlogEntryRepository createEntryRespository(Mongo mongo)
{
if (MONGODB_PERSISTENCE.equals(persistence))
{
if (bootstraps == null)
{
throw new RuntimeException("MongoDB bootstraps not set");
}

return new MongoBlogEntryRepository(bootstraps);
return new MongoBlogEntryRepository(mongo);
}

return null;
}

public CommentRepository getCommentsRespository()
private CommentRepository createCommentRespository(Mongo mongo)
{
if (MONGODB_PERSISTENCE.equals(persistence))
{
if (bootstraps == null)
{
throw new RuntimeException("MongoDB bootstraps not set");
}

return new MongoCommentRepository(bootstraps);
return new MongoCommentRepository(mongo);
}

return null;
Expand Down
52 changes: 49 additions & 3 deletions examples/blogging/src/java/com/blogging/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import java.io.FileNotFoundException;
import java.io.IOException;

import org.jboss.netty.handler.codec.http.HttpMethod;

import com.blogging.controller.BlogController;
import com.blogging.controller.BlogEntryController;
import com.blogging.controller.CommentController;
import com.blogging.postprocessor.LastModifiedHeaderPostprocessor;
import com.blogging.serialization.ResponseProcessors;
import com.strategicgains.repoexpress.exception.DuplicateItemException;
import com.strategicgains.repoexpress.exception.InvalidObjectIdException;
import com.strategicgains.repoexpress.exception.ItemNotFoundException;
import com.strategicgains.restexpress.Format;
import com.strategicgains.restexpress.Parameters;
Expand Down Expand Up @@ -37,8 +43,7 @@ public static void main(String[] args)
throws Exception
{
Configuration config = loadEnvironment(args);
RestExpress server = new RestExpress(new Routes(config.getBlogRespository(),
config.getEntriesRespository(), config.getCommentsRespository()))
RestExpress server = new RestExpress()
.setName(config.getName())
.setPort(config.getPort())
.putResponseProcessor(Format.JSON, ResponseProcessors.json())
Expand All @@ -49,6 +54,8 @@ public static void main(String[] args)
.addMessageObserver(new SimpleConsoleLogMessageObserver())
.addPostprocessor(new LastModifiedHeaderPostprocessor());

defineRoutes(server, config);

new RoutesMetadataPlugin()
.register(server)
.parameter(Parameters.Cache.MAX_AGE, 86400); // 24 hours, in seconds.
Expand All @@ -61,6 +68,44 @@ public static void main(String[] args)
server.awaitShutdown();
}

private static void defineRoutes(RestExpress server, Configuration config)
{
BlogController blogController = config.getBlogController();
BlogEntryController entryController = config.getEntryController();
CommentController commentController = config.getCommentController();

//Create a new blog. Auto-assigned ID.
server.uri("/blogs.{format}", blogController)
.method(HttpMethod.POST);

server.uri("/blogs/authors/{author}.{format}", blogController)
.action("readOwnedBlogs", HttpMethod.GET);

// Read, update, delete a blog.
server.uri("/blogs/{blogId}.{format}", blogController)
.method(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE)
.name(Constants.BLOG_URL_NAME);

// List the blog entries.
server.uri("/blogs/{blogId}/entries.{format}", entryController)
.action("list", HttpMethod.GET)
.method(HttpMethod.POST);

// Read, update, delete a blog entry.
server.uri("/blogs/{blogId}/entries/{entryId}.{format}", entryController)
.method(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE)
.name("BlogEntryUri");

// List the comments for an entry.
server.uri("/blogs/{blogId}/entries/{entryId}/comments.{format}", commentController)
.action("list", HttpMethod.GET)
.method(HttpMethod.POST);

// Read, update, delete a blog entry comment.
server.uri("/blogs/{blogId}/entries/{entryId}/comments/{commentId}.{format}", commentController)
.method(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE);
}

/**
* @param server
*/
Expand All @@ -69,7 +114,8 @@ private static void mapExceptions(RestExpress server)
server
.mapException(ItemNotFoundException.class, NotFoundException.class)
.mapException(DuplicateItemException.class, ConflictException.class)
.mapException(ValidationException.class, BadRequestException.class);
.mapException(ValidationException.class, BadRequestException.class)
.mapException(InvalidObjectIdException.class, BadRequestException.class);
}

private static Configuration loadEnvironment(String[] args)
Expand Down
66 changes: 0 additions & 66 deletions examples/blogging/src/java/com/blogging/Routes.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package com.blogging.persistence;

import java.util.List;

import org.bson.types.ObjectId;

import com.mongodb.ServerAddress;
import com.mongodb.Mongo;
import com.strategicgains.repoexpress.domain.TimestampedIdentifiable;
import com.strategicgains.repoexpress.event.DefaultTimestampedIdentifiableRepositoryObserver;
import com.strategicgains.repoexpress.mongodb.MongodbRepository;
Expand All @@ -35,9 +33,9 @@ public abstract class AbstractMongoDbRepository<T extends TimestampedIdentifiabl
private static final String DATABASE_NAME = "blogging";

@SuppressWarnings("unchecked")
public AbstractMongoDbRepository(List<ServerAddress> bootstraps, Class<T> type)
public AbstractMongoDbRepository(Mongo mongo, Class<T> type)
{
super(bootstraps, DATABASE_NAME, type);
super(mongo, DATABASE_NAME, type);
initializeObservers();
setIdentifierAdapter(new ObjectIdAdapter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import com.blogging.domain.BlogEntry;
import com.google.code.morphia.query.Query;
import com.mongodb.ServerAddress;
import com.mongodb.Mongo;

public class MongoBlogEntryRepository
extends AbstractMongoDbRepository<BlogEntry>
implements BlogEntryRepository
{
public MongoBlogEntryRepository(List<ServerAddress> bootstraps)
public MongoBlogEntryRepository(Mongo mongo)
{
super(bootstraps, BlogEntry.class);
super(mongo, BlogEntry.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.util.List;

import com.blogging.domain.Blog;
import com.mongodb.ServerAddress;
import com.mongodb.Mongo;

public class MongoBlogRepository
extends AbstractMongoDbRepository<Blog>
implements BlogRepository
{
public MongoBlogRepository(List<ServerAddress> bootstraps)
public MongoBlogRepository(Mongo mongo)
{
super(bootstraps, Blog.class);
super(mongo, Blog.class);
}

@Override
Expand Down
Loading

0 comments on commit 38feedd

Please sign in to comment.