*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
JSONRequest jsonRequest = null;
JSONResponse jsonResponse = null;
String body = null;
String agentUrl = null;
String agentId = null;
try {
// retrieve the agent url and the request body
body = StringUtil.streamToString(req.getInputStream());
jsonRequest = new JSONRequest(body);
agentUrl = req.getRequestURI();
agentId = httpTransport.getAgentId(agentUrl);
if (agentId == null || agentId.isEmpty()) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"No agentId found in url.");
return;
}
Agent agent = agentHost.getAgent(agentId);
if (agent == null){
resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Agent not found at this host.");
return;
}
if (JSONRPC.hasPrivate(agent.getClass())
&& !handleSession(req, resp)) {
if (!resp.isCommitted()) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
return;
}
// Attach the claimed senderId, or null if not given.
RequestParams requestParams = new RequestParams();
String senderUrl = req.getHeader("X-Eve-SenderUrl");
if (senderUrl == null || senderUrl.equals("")) {
senderUrl = "web://" + req.getRemoteUser() + "@"
+ req.getRemoteAddr();
}
requestParams.put(Sender.class, senderUrl);
// invoke the agent
jsonResponse = agentHost.receive(agentId, jsonRequest,
requestParams);
} catch (Exception err) {
// generate JSON error response
LOG.log(Level.WARNING, "", err);
JSONRPCException jsonError = null;
if (err instanceof JSONRPCException) {
jsonError = (JSONRPCException) err;
} else {
jsonError = new JSONRPCException(
JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());
jsonError.setData(err);
}
jsonResponse = new JSONResponse(jsonError);
}
// return response
resp.addHeader("Content-Type", "application/json");
resp.getWriter().println(jsonResponse.toString());
resp.getWriter().close();
}