Created
July 22, 2014 06:49
-
-
Save purplefox/3468300cc63e99f88bb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RouteMatcher rm = new RouteMatcher(); | |
rm.post("/" + PRIMARY_RESOURCE + "/" + VERSION + "/" | |
+ SECONDARY_RESOURCE, new Handler<HttpServerRequest>() { | |
@Override | |
public void handle(final HttpServerRequest req) { | |
MultiMap params = req.headers(); | |
boolean isuserValidated = false; | |
try { | |
isuserValidated = Util.authorize(params, config); | |
if (isuserValidated) { | |
req.bodyHandler(new Handler<Buffer>() { | |
@Override | |
public void handle(Buffer event) { | |
final String body = event.getString(0, | |
event.length()); | |
log.info("User Creation Request Obtained is " + body); | |
User userObj = new Gson().fromJson(body, | |
User.class); | |
try { | |
boolean isRequestValid = Util | |
.validateRequestBody(userObj); | |
if(isRequestValid){ | |
log.debug("Request Validated"); | |
//Some code to geenrate response JSON | |
String response = new Gson().toJson(respJson); | |
req.response().headers().add("Content-Length",String.valueOf(response.length())).add("Access-Control-Allow-Origin","*"); | |
req.response().write(response); | |
req.response().setStatusCode(201).setStatusMessage("CREATED"); | |
vertx.eventBus().send("user.provision.event.bus", new String(body.getBytes()),new Handler<Message<String>>() { | |
@Override | |
public void handle(Message<String> message) { | |
log.info("Message obtained is "+message.body()); | |
ResponseJson json = new Gson().fromJson(message.body(), ResponseJson.class); | |
if(json.getJsonResponse() == null){ | |
req.response().setStatusCode(json.getStatusCode()).setStatusMessage(json.getStatusMessage()); | |
req.response().end(); | |
} | |
else{ | |
req.response().headers().add("Content-Length",String.valueOf(json.getJsonResponse().length())); | |
req.response().write(json.getJsonResponse()); | |
req.response().setStatusCode(json.getStatusCode()).setStatusMessage(json.getStatusMessage()); | |
req.response().end(); | |
} | |
} | |
}); | |
} | |
} catch (BadRequestException e) { | |
req.response().headers().add("Access-Control-Allow-Origin","*"); | |
req.response().setStatusCode(e.getCode()).setStatusMessage(e.getMessage()).end(); | |
} | |
} | |
}); | |
} | |
} catch (UserNotAuthorizedException e) { | |
req.response().setStatusCode(e.getCode()) | |
.setStatusMessage(e.getMessage()).end(); | |
} | |
req.exceptionHandler(new Handler<Throwable>() { | |
@Override | |
public void handle(Throwable e) { | |
log.error("An error occured while receiving the request " | |
+ e.getMessage()); | |
log.error("The detailed error is \n"); | |
e.printStackTrace(); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment