Skip to content

Commit

Permalink
fixed routematcher
Browse files Browse the repository at this point in the history
  • Loading branch information
purplefox committed Feb 13, 2012
1 parent 1720630 commit 1e3d4fe
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 233 deletions.
Expand Up @@ -111,7 +111,6 @@ private void handleRequest(HttpServerRequest req) {
try {
this.currentRequest = req;
pendingResponse = req.response;

if (requestHandler != null) {
requestHandler.handle(req);
}
Expand Down Expand Up @@ -230,6 +229,7 @@ protected ChannelFuture sendFile(File file) {

private void processMessage(Object msg) {
if (msg instanceof HttpRequest) {

HttpRequest request = (HttpRequest) msg;
HttpServerRequest req = new HttpServerRequest(this, request);
handleRequest(req);
Expand Down
48 changes: 37 additions & 11 deletions src/main/javascript/core/filesystem.js
Expand Up @@ -201,21 +201,47 @@ if (!vertx.FileSystem) {
if (fut.succeeded) {
var j_af = fut.result();

/*
TODO - we need to somehow override the read and write methods on
AsyncFile and provide versions which take a handler as final argument
This is currently tricky with Rhino
var wrapped = ....
*/

handler(null,j_af);
var wrapped = {
close: function(handler) {
if (handler) {
j_af.closeDeferred().handler(handler).execute();
} else {
j_af.close();
}
},

write: function(buffer, position, handler) {
var fut = j_af.write(buffer, position);
wrapHandler(handler, fut);
},

read: function(buffer, offset, position, length, handler) {
var fut = j_af.read(buffer, offset, position, length);
wrapHandler(handler, fut);
},

getWriteStream: function() {
return j_af.getWriteStream();
},

getReadStream: function() {
return j_af.getReadStream();
},

flush: function(handler) {
if (handler) {
j_af.flushDeferred().handler(handler).execute();
} else {
j_af.flush();
}
}
}

handler(null, wrapped);
} else {
handler(fut.exception(), null);
}
});

wrapHandler(handler, fut);
}

vertx.FileSystem.createFile = function(path, handler) {
Expand Down
182 changes: 133 additions & 49 deletions src/main/javascript/core/http.js
Expand Up @@ -12,70 +12,68 @@ if (!vertx.HttpServer) {
return map;
}

vertx.HttpServer = function() {
function wrappedRequestHandler(handler) {
return function(req) {

var j_server = new org.vertx.java.core.http.HttpServer();
//We need to add some functions to the request and the response

var that = this;
var reqHeaders = null;
var reqParams = null;

this.requestHandler = function(handler) {
var reqProto = {

if (handler) {

var theHandler;
if (handler.handle) {

theHandler = function(req) {
handler.handle(req);
headers: function() {
if (!reqHeaders) {
reqHeaders = convertMap(req.getAllHeaders());
}
return reqHeaders;
},
params: function() {
if (!reqParams) {
reqParams = convertMap(req.getAllParams());
}
return reqParams;
}
};

} else {
theHandler = handler;
var respProto = {
putHeaders: function(hash) {
for (k in hash) {
req.response.putHeader(k, hash[k]);
}
},
putTrailers: function(hash) {
for (k in hash) {
req.response.putTrailer(k, hash[k]);
}
}
}

var wrappedHandler = function(req) {
req.__proto__ = reqProto;
req.response.__proto__ = respProto;

//We need to add some functions to the request and the response
handler(req);
}
}

var reqHeaders = null;
var reqParams = null;
vertx.HttpServer = function() {

var reqProto = {
var j_server = new org.vertx.java.core.http.HttpServer();

headers: function() {
if (!reqHeaders) {
reqHeaders = convertMap(req.getAllHeaders());
}
return reqHeaders;
},
params: function() {
if (!reqParams) {
reqParams = convertMap(req.getAllParams());
}
return reqParams;
}
};

var respProto = {
putHeaders: function(hash) {
for (k in hash) {
req.response.putHeader(k, hash[k]);
}
},
putTrailers: function(hash) {
for (k in hash) {
req.response.putTrailer(k, hash[k]);
}
}
}
var that = this;

req.__proto__ = reqProto;
req.response.__proto__ = respProto;
this.requestHandler = function(handler) {

theHandler(req);
if (handler) {

if (typeof handler === 'function') {
handler = wrappedRequestHandler(handler);
} else {
// It's a route matcher
handler = handler._to_java_handler();
}

j_server.requestHandler(wrappedHandler);
j_server.requestHandler(handler);
}
return that;
};
Expand Down Expand Up @@ -406,6 +404,92 @@ if (!vertx.HttpServer) {
}

vertx.RouteMatcher = function() {
return new org.vertx.java.core.http.RouteMatcher();

var j_rm = new org.vertx.java.core.http.RouteMatcher();

this.get = function(pattern, handler) {
j_rm.get(pattern, wrappedRequestHandler(handler));
}

this.put = function(pattern, handler) {
j_rm.put(pattern, wrappedRequestHandler(handler));
}

this.post = function(pattern, handler) {
j_rm.post(pattern, wrappedRequestHandler(handler));
}

this.delete = function(pattern, handler) {
j_rm.delete(pattern, wrappedRequestHandler(handler));
}

this.options = function(pattern, handler) {
j_rm.options(pattern, wrappedRequestHandler(handler));
}

this.head = function(pattern, handler) {
j_rm.head(pattern, wrappedRequestHandler(handler));
}

this.trace = function(pattern, handler) {
j_rm.trace(pattern, wrappedRequestHandler(handler));
}

this.connect = function(pattern, handler) {
j_rm.connect(pattern, wrappedRequestHandler(handler));
}

this.patch = function(pattern, handler) {
j_rm.patch(pattern, wrappedRequestHandler(handler));
}

this.all = function(pattern, handler) {
j_rm.all(pattern, wrappedRequestHandler(handler));
}

this.getWithRegEx = function(pattern, handler) {
j_rm.getWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.putWithRegEx = function(pattern, handler) {
j_rm.putWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.postWithRegEx = function(pattern, handler) {
j_rm.postWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.deleteWithRegEx = function(pattern, handler) {
j_rm.deleteWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.optionsWithRegEx = function(pattern, handler) {
j_rm.optionsWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.headWithRegEx = function(pattern, handler) {
j_rm.headWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.traceWithRegEx = function(pattern, handler) {
j_rm.traceWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.connectWithRegEx = function(pattern, handler) {
j_rm.connectWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.patchWithRegEx = function(pattern, handler) {
j_rm.patchWithRegEx(pattern, wrappedRequestHandler(handler));
}

this.allWithRegEx = function(pattern, handler) {
j_rm.allWithRegEx(pattern, wrappedRequestHandler(handler));
}

this._to_java_handler = function() {
return j_rm;
}

}
}
8 changes: 0 additions & 8 deletions src/main/javascript/core/routematch.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/main/ruby/core/http.rb
Expand Up @@ -987,6 +987,15 @@ def connect(pattern, proc = nil, &hndlr)
@j_del.connect(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
end

# Specify a handler that will be called for any matching HTTP request
# @param [String] The simple pattern
# @param [Proc] proc A proc to be used as the handler
# @param [Block] hndlr A block to be used as the handler
def all(pattern, proc = nil, &hndlr)
hndlr = proc if proc
@j_del.all(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
end

# Specify a handler that will be called for a matching HTTP GET
# @param [String] A regular expression for a pattern
# @param [Proc] proc A proc to be used as the handler
Expand Down Expand Up @@ -1070,5 +1079,14 @@ def connect_re(pattern, proc = nil, &hndlr)
@j_del.connectWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
end

# Specify a handler that will be called for any matching HTTP request
# @param [String] A regular expression for a pattern
# @param [Proc] proc A proc to be used as the handler
# @param [Block] hndlr A block to be used as the handler
def all_re(pattern, proc = nil, &hndlr)
hndlr = proc if proc
@j_del.allWithRegEx(pattern) { |j_req| hndlr.call(HttpServerRequest.new(j_req)) }
end

end
end

0 comments on commit 1e3d4fe

Please sign in to comment.