Route the request to the cometd implementation. If the request point to
a static file, delegate the call to the Grizzly WebServer implementation.
MessageBytes mb = req.requestURI();
ByteChunk requestURI = mb.getByteChunk();
String uri = req.requestURI().toString();
File file = new File(getRootFolder(),uri);
if (file.isDirectory()) {
uri += "index.html";
file = new File(file,uri);
}
if (file.canRead()) {
super.service(req,res);
return;
}
CometEngine cometEngine = CometEngine.getEngine();
CometContext cometContext = cometEngine.getCometContext(contextPath);
if (!initialized){
synchronized(cometContext){
if (!initialized){
bayeuxCometHandler = new BayeuxCometHandler();
eventRouter = new EventRouterImpl(cometContext);
int mainHandlerHash =
cometContext.addCometHandler(bayeuxCometHandler,true);
cometContext.addAttribute(BayeuxCometHandler.BAYEUX_COMET_HANDLER,
mainHandlerHash);
initialized = true;
}
}
}
CometdRequest cometdReq = (CometdRequest) req.getNote(ADAPTER_NOTES);
CometdResponse cometdRes = (CometdResponse) res.getNote(ADAPTER_NOTES);
if (cometdReq == null) {
cometdReq = new CometdRequest<Request>(req) {
private boolean requestParametersParsed = false;
protected byte[] postData = null;
private ByteChunk chunk = new ByteChunk();
private byte[] body = new byte[8192];
public String[] getParameterValues(String s) {
Parameters parameters = request.getParameters();
requestParametersParsed = true;
parameters.setEncoding
(org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING);
parameters.setQueryStringEncoding
(org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING);
parameters.handleQueryParameters();
int len = request.getContentLength();
if (len > 0) {
try {
byte[] formData = getPostBody();
if (formData != null) {
parameters.processParameters(formData, 0, len);
}
} catch (Throwable t) {
; // Ignore
}
}
return parameters.getParameterValues(s);
}
protected byte[] getPostBody() throws IOException {
int len = request.getContentLength();
int actualLen = readPostBody(chunk, len);
if (actualLen == len) {
chunk.substract(body,0,chunk.getLength());
chunk.recycle();
return body;
}
return null;
}
/**
* Read post body in an array.
*/
protected int readPostBody(ByteChunk chunk,int len) throws IOException {
int offset = 0;
do {
int inputLen = request.doRead(chunk);
if (inputLen <= 0) {
return offset;
}
offset += inputLen;
} while ((len - offset) > 0);
return len;
}
};
cometdRes = new CometdResponse<Response>(res) {
private StringBuffer buf = new StringBuffer();
private ByteChunk chunk = new ByteChunk();
public void write(String s) throws IOException {
buf.append(s);
}
public void flush() throws IOException {
int length = buf.length();
response.addHeader("Server",
SelectorThread.SERVER_NAME);
response.sendHeaders();
chunk.setBytes(buf.toString().getBytes(),0,length);
res.doWrite(chunk);
chunk.recycle();
buf.delete(0,length);
}
public void setContentType(String s) {
response.setContentType(s);
}
};
// Set as notes so we don't create them on every request.'
req.setNote(ADAPTER_NOTES, cometdReq);
res.setNote(ADAPTER_NOTES, cometdRes);
} else {
cometdReq.setRequest(req);
cometdRes.setResponse(res);
}
eventRouter.route(cometdReq,cometdRes);