public void incoming(Channel channel, HttpRequest request) {
boolean isStrest = StrestUtil.isStrest(request);
// Build the response object.
ResponseBuilder response = new ResponseBuilder(request);
this.connections.putIfAbsent(channel, new StrestConnectionChannel(channel));
StrestConnectionChannel con = this.connections.get(channel);
String txnId = request.getHeader(StrestUtil.HEADERS.TXN_ID);
con.incoming(request);
StrestController controller = null;
try {
try {
controller = this.getRouteLookup().find(request.getUri());
if (controller == null) {
throw StrestHttpException.NOT_FOUND();
}
controller.setRouter(this);
controller.setStrest(isStrest);
if (isStrest) {
controller.setStrestTxnId(txnId);
}
//parse the get string params
controller.setParamsGET(DynMapFactory.instanceFromURL(request.getUri()));
//parse any post params
String contentType = request.getHeader(CONTENT_TYPE);
if (contentType != null && contentType.contains("form-urlencoded")) {
String pms = request.getContent().toString(Charset.forName("utf8"));
if (pms != null) {
controller.setParamsPOST(DynMapFactory.instanceFromURLEncoded(pms));
}
}
controller.getParams().putAll(controller.getParamsGET());
controller.getParams().putAll(controller.getParamsPOST());
controller.setRequest(request);
controller.setResponse(response.getResponse());
controller.setChannelConnection(con);
//before filters
for (StrestControllerFilter f : controller.getFilters()) {
f.before(controller);
}
for (StrestControllerFilter f : this.defaultFilters) {
f.before(controller);
}
//now execution the appropriate action.
if (request.getMethod() == HttpMethod.GET) {
controller.handleGET(controller.getParams());
} else if (request.getMethod() == HttpMethod.POST) {
controller.handlePOST(controller.getParams());
} else if (request.getMethod() == HttpMethod.PUT) {
controller.handlePUT(controller.getParams());
} else if (request.getMethod() == HttpMethod.DELETE) {
controller.handleDELETE(controller.getParams());
} else {
throw StrestHttpException.METHOD_NOT_ALLOWED();
}
for (StrestControllerFilter f : controller.getFilters()) {
f.after(controller);
}
for (StrestControllerFilter f : this.defaultFilters) {
f.after(controller);
}
response.setResponse(controller.getResponse());
if (!controller.isSendResponse()) {
return;
}
} catch (StrestHttpException e) {
throw e;
} catch (Exception x) {
StrestHttpException e = StrestHttpException.INTERNAL_SERVER_ERROR();
e.setCause(x);
log.error("Caught", x);
throw e;
}
} catch (StrestHttpException e) {
response.status(e.getCode(), e.getMessage());
response.txnStatus(StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE);
//run the error filters
if (controller != null) {
for (StrestControllerFilter f : controller.getFilters()) {
f.error(controller, response.getResponse(), e);
}
}
for (StrestControllerFilter f : this.defaultFilters) {
f.error(controller, response.getResponse(), e);
}
}
String txnStatus = response.getTxnStatus();
if (txnStatus == null) {
txnStatus = StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE;
}
//client only accepts single transactions.
if (StrestUtil.HEADERS.TXN_ACCEPT_VALUES.SINGLE.equalsIgnoreCase(request.getHeader(StrestUtil.HEADERS.TXN_ACCEPT))) {
txnStatus = StrestUtil.HEADERS.TXN_STATUS_VALUES.COMPLETE;
}
//now set the status
response.txnStatus(txnStatus);
// Write the response.
// System.out.println(response.getResponse());
// System.out.println("*****");
// System.out.println(response.getResponse().getContent().toString());
ChannelFuture future = con.sendMessage(response);
// Close the non-keep-alive connection after the write operation is done.
if (!isStrest) {
// log.info("CLOSING NON STREST CONNECTION");
future.addListener(ChannelFutureListener.CLOSE);