Skip to content

Instantly share code, notes, and snippets.

@dashorst
Created April 14, 2013 20:29
Show Gist options
  • Save dashorst/5384083 to your computer and use it in GitHub Desktop.
Save dashorst/5384083 to your computer and use it in GitHub Desktop.
Mongo wicket example connection provider. The MongoFilter was created as a servlet Filter because we also wanted to enable mongo through our rest api. A wicket filter would not be accessible through the rest api.
package com.example.mongo;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MongoFilter implements Filter {
@Inject
private MongoResource mongo;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
try {
mongo.beginRequest();
chain.doFilter(request, response);
} finally {
mongo.endRequest();
}
}
@Override
public void destroy() {
}
}
package com.example.mongo;
import java.io.PrintStream;
import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import com.mongodb.DB;
import com.mongodb.MongoURI;
@Singleton
public class MongoResource {
private static MongoURI mongoURI;
private DB connectedDB;
public MongoResource() {
}
@PostConstruct
private void init() throws Exception {
if (mongoURI == null)
mongoURI = validateSettings();
connectedDB = mongoURI.connectDB();
if (mongoURI.getUsername() != null && !mongoURI.getUsername().isEmpty())
connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
}
public DB getDB() {
return connectedDB;
}
public static void validateSettings(String uri) {
try {
mongoURI = new MongoURI(uri);
System.out.println("Connecting MongoDB using: " + mongoURI.getUsername() + "@" + mongoURI.getDatabase());
} catch (Exception e) {
System.err.println("Unable to connect to mongo: " + e);
System.exit(1);
}
}
public static MongoURI validateSettings() {
String uri = System.getenv("MONGOLAB_URI");
if (uri == null || uri.isEmpty()) {
PrintStream err = System.err;
err.printf(
"No %s has been set as an environment variable, format is: mongodb://user:password@hostname:port/databasename\n",
"MONGOLAB_URI");
err.println();
err.println("Without authentication:");
err.printf(" export %s=mongodb://localhost:27017/topiconf\n", "MONGOLAB_URI");
err.println();
err.println("With authentication:");
err.printf(" export %s=mongodb://john:doe@localhost:27017/topiconf", "MONGOLAB_URI");
err.println();
System.exit(1);
}
try {
MongoURI mongoURI = new MongoURI(uri);
System.out.println("Connecting MongoDB using: " + mongoURI.getUsername() + "@" + mongoURI.getDatabase());
return mongoURI;
} catch (Exception e) {
System.err.println("Unable to connect to mongo: " + e);
System.exit(1);
return null;
}
}
public void beginRequest() {
connectedDB.requestStart();
}
public void endRequest() {
connectedDB.requestDone();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment