[simple] [java-vertx] Why I can not subclass / re-use HttpServerResponseIml?

53 views
Skip to first unread message

Paris Apostolopoulos

unread,
Nov 27, 2015, 10:07:08 AM11/27/15
to vert.x
Hello, maybe my idea is not on the right direction. I am wondering why the concrete implementation of  io.vertx.core.http.HttpServerResponse -> io.vertx.core.http.HttpServerResponseImpl ,
can not be sub-classed? Is it a design decision? I was thinking on creating a base response that would have some kind of basic functionality, for example I dont have to 
put always in the code the same headers or other properties, so I was thinking reusing the existing base class and adding some of my 'preferences'.

Unless I am supposed to do this by chaining handlers and mutating the response along the way?

Thanks for your time.

Paris Apostolopoulos

unread,
Nov 27, 2015, 11:39:23 AM11/27/15
to vert.x
To sort of answer my question, most probably I was not in the right direction. The best way would be to do something similar as the BodyHandler. - as indicated in the blog post http://vertx.io/blog/some-rest-with-vert-x/

So I created my base handler that sets always the content type or other stuff, I would like to have for most of my routes / resources

public class BaseHandler  implements Handler<RoutingContext>{

@Override
public void handle(RoutingContext context) {
HttpServerResponse response = context.response();
response.putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json");
//other stuff!
response.setChunked(true);
context.next();
}
}

And in my Verticle I register my base handler - overall

@Override
public void start(Future<Void> fut) {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
//register the base handler to do basic stuff
router.route().handler(new BaseHandler());

router.route("/status/").handler(new StatusHandler());
router.route("/users/").handler(new UsersHandler());
router.route("/products/").handler(new ProductsHandler());
server.requestHandler(router::accept).listen(8080);
}

Thanks
Reply all
Reply to author
Forward
0 new messages