Skip to content

Instantly share code, notes, and snippets.

@cleuton
Created March 20, 2014 11:35
Show Gist options
  • Save cleuton/9661903 to your computer and use it in GitHub Desktop.
Save cleuton/9661903 to your computer and use it in GitHub Desktop.
A RESTful / JSON Webservice made with Node.js, and using a Java MongoDB DAO
// (c) http://www.obomprogramador.com
// REST / JSON WS implementation with Node.js calling Java
var restify = require('restify')
var server = restify.createServer()
var fs = require('fs')
var java = require('java')
java.classpath.push("lib/joda-time-2.3.jar")
java.classpath.push("lib/mongo-java-driver-2.11.4.jar")
java.classpath.push("lib/jsondao.jar");
java.classpath.push("lib/json-20140107.jar");
debugger
// Callbacks
// Página da aplicação:
function home(req,res,next) {
fs.createReadStream('./index.html', {autoClose: true}).pipe(res)
}
// Função de logon:
function logon(req,res,next) {
var dao = java.newInstanceSync("com.obomprogramador.discoarq.microblog.persistence.implementation.DaoMongoDB")
console.log("Find user: " + req.params.username + " password: " + req.params.password);
var usuario = dao.findUserSync(req.params.username, req.params.password);
console.log("User found: " + usuario);
var objetoUsuario = java.newInstanceSync("org.json.JSONObject",usuario.toString())
console.log("JSON User: " + objetoUsuario.toStringSync());
var sessao = dao.getSessionSync(objetoUsuario)
res.send(sessao.toStringSync())
next()
}
// Userdata:
function userdata(req,res,next) {
var dao = java.newInstanceSync("com.obomprogramador.discoarq.microblog.persistence.implementation.DaoMongoDB")
console.log("Getting user data... " + req.body)
var sessao = req.body
var objetoSessao = java.newInstanceSync("org.json.JSONObject",sessao)
var usuario = dao.getUserFromSessionSync(objetoSessao)
console.log("User data: " + usuario);
res.send(usuario.toStringSync())
next()
}
// Mensagens:
function messages(req,res,next) {
var dao = java.newInstanceSync("com.obomprogramador.discoarq.microblog.persistence.implementation.DaoMongoDB")
var sessao = req.body
console.log("Validating session... " + JSON.stringify(sessao))
var objetoSessao = java.newInstanceSync("org.json.JSONObject",sessao.toString())
var isvalid = dao.validateSessionSync(objetoSessao)
console.log("Session validation: " + isvalid)
if (isvalid) {
console.log("Session is valid! Getting messages... ");
var usuario = dao.getUserFromSessionSync(objetoSessao)
console.log("User data: " + usuario.toStringSync());
var result = dao.getMessagesSync(usuario);
console.log("Messages: " + result.toStringSync());
console.log("Got them!")
}
var mensagens = eval(result.toStringSync())
res.send(mensagens)
next()
}
server.use(restify.bodyParser({ mapParams: false }));
// Home page:
server.get('/mb',home)
// Logon call: /mb/server/session/<username>/<password>
server.get('/mb/server/session/:username/:password', logon);
// Userdata: POST /mb/server/userdata <session>
server.post('/mb/server/userdata',userdata);
// Messages: POST /mb/server/messages <session>
server.post('/mb/server/messages',messages);
// Start server
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
@cleuton
Copy link
Author

cleuton commented Mar 20, 2014

This is a Node.js RESTful Webservice, which uses JSON as transfer model, and a MongoDB DAO, written in Java. It uses "restify" and "node-java" npm packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment