return debugger;
}
@Override
public void executeRest(String realPath, boolean throwExcpetion) throws PageException {
ApplicationListener listener=null;//config.get ApplicationListener();
try{
String pathInfo = req.getPathInfo();
// charset
try{
String charset=HTTPUtil.splitMimeTypeAndCharset(req.getContentType(),new String[]{"",""})[1];
if(StringUtil.isEmpty(charset))charset=ThreadLocalPageContext.getConfig().getWebCharset();
java.net.URL reqURL = new java.net.URL(req.getRequestURL().toString());
String path=ReqRspUtil.decode(reqURL.getPath(),charset,true);
String srvPath=req.getServletPath();
if(path.startsWith(srvPath)) {
pathInfo=path.substring(srvPath.length());
}
}
catch (Exception e){}
// Service mapping
if(StringUtil.isEmpty(pathInfo) || pathInfo.equals("/")) {// ToDo
// list available services (if enabled in admin)
if(config.getRestList()) {
try {
HttpServletRequest _req = getHttpServletRequest();
write("Available sevice mappings are:<ul>");
railo.runtime.rest.Mapping[] mappings = config.getRestMappings();
railo.runtime.rest.Mapping _mapping;
String path;
for(int i=0;i<mappings.length;i++){
_mapping=mappings[i];
Resource p = _mapping.getPhysical();
path=_req.getContextPath()+ReqRspUtil.getScriptName(_req)+_mapping.getVirtual();
write("<li "+(p==null || !p.isDirectory()?" style=\"color:red\"":"")+">"+path+"</li>");
}
write("</ul>");
} catch (IOException e) {
throw Caster.toPageException(e);
}
}
else
RestUtil.setStatus(this, 404, null);
return;
}
// check for matrix
int index;
String entry;
Struct matrix=new StructImpl();
while((index=pathInfo.lastIndexOf(';'))!=-1){
entry=pathInfo.substring(index+1);
pathInfo=pathInfo.substring(0,index);
if(StringUtil.isEmpty(entry,true)) continue;
index=entry.indexOf('=');
if(index!=-1)matrix.setEL(entry.substring(0,index).trim(), entry.substring(index+1).trim());
else matrix.setEL(entry.trim(), "");
}
// get accept
List<MimeType> accept = ReqRspUtil.getAccept(this);
MimeType contentType = ReqRspUtil.getContentType(this);
// check for format extension
//int format = getApplicationContext().getRestSettings().getReturnFormat();
int format;
boolean hasFormatExtension=false;
if(StringUtil.endsWithIgnoreCase(pathInfo, ".json")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-5);
format = UDF.RETURN_FORMAT_JSON;
accept.clear();
accept.add(MimeType.APPLICATION_JSON);
hasFormatExtension=true;
}
else if(StringUtil.endsWithIgnoreCase(pathInfo, ".wddx")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-5);
format = UDF.RETURN_FORMAT_WDDX;
accept.clear();
accept.add(MimeType.APPLICATION_WDDX);
hasFormatExtension=true;
}
else if(StringUtil.endsWithIgnoreCase(pathInfo, ".cfml")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-5);
format = UDF.RETURN_FORMAT_SERIALIZE;
accept.clear();
accept.add(MimeType.APPLICATION_CFML);
hasFormatExtension=true;
}
else if(StringUtil.endsWithIgnoreCase(pathInfo, ".serialize")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-10);
format = UDF.RETURN_FORMAT_SERIALIZE;
accept.clear();
accept.add(MimeType.APPLICATION_CFML);
hasFormatExtension=true;
}
else if(StringUtil.endsWithIgnoreCase(pathInfo, ".xml")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-4);
format = UDF.RETURN_FORMAT_XML;
accept.clear();
accept.add(MimeType.APPLICATION_XML);
hasFormatExtension=true;
}
else if(StringUtil.endsWithIgnoreCase(pathInfo, ".java")) {
pathInfo=pathInfo.substring(0,pathInfo.length()-5);
format = UDFPlus.RETURN_FORMAT_JAVA;
accept.clear();
accept.add(MimeType.APPLICATION_JAVA);
hasFormatExtension=true;
}
else {
format = getApplicationContext().getRestSettings().getReturnFormat();
//MimeType mt=MimeType.toMimetype(format);
//if(mt!=null)accept.add(mt);
}
if(accept.size()==0) accept.add(MimeType.ALL);
// loop all mappings
//railo.runtime.rest.Result result = null;//config.getRestSource(pathInfo, null);
RestRequestListener rl=null;
railo.runtime.rest.Mapping[] restMappings = config.getRestMappings();
railo.runtime.rest.Mapping m,mapping=null,defaultMapping=null;
//String callerPath=null;
if(restMappings!=null)for(int i=0;i<restMappings.length;i++) {
m = restMappings[i];
if(m.isDefault())defaultMapping=m;
if(pathInfo.startsWith(m.getVirtualWithSlash(),0) && m.getPhysical()!=null) {
mapping=m;
//result = m.getResult(this,callerPath=pathInfo.substring(m.getVirtual().length()),format,matrix,null);
rl=new RestRequestListener(m,pathInfo.substring(m.getVirtual().length()),matrix,format,hasFormatExtension,accept,contentType,null);
break;
}
}
// default mapping
if(mapping==null && defaultMapping!=null && defaultMapping.getPhysical()!=null) {
mapping=defaultMapping;
//result = mapping.getResult(this,callerPath=pathInfo,format,matrix,null);
rl=new RestRequestListener(mapping,pathInfo,matrix,format,hasFormatExtension,accept,contentType,null);
}
//base = PageSourceImpl.best(config.getPageSources(this,null,realPath,true,false,true));
if(mapping==null || mapping.getPhysical()==null){
RestUtil.setStatus(this,404,"no rest service for ["+pathInfo+"] found");
}
else {
base=config.toPageSource(null, mapping.getPhysical(), null);
listener=((MappingImpl)base.getMapping()).getApplicationListener();
listener.onRequest(this, base,rl);
}
}
catch(Throwable t) {
PageException pe = Caster.toPageException(t);
if(!Abort.isSilentAbort(pe)){
log(true);
if(fdEnabled){
FDSignal.signal(pe, false);
}
if(listener==null) {
if(base==null)listener=config.getApplicationListener();
else listener=((MappingImpl)base.getMapping()).getApplicationListener();
}
listener.onError(this,pe);
}
else log(false);
if(throwExcpetion) throw pe;
}