private JSONObject serveAsJson(IHTTPSession request, Session session) {
		try
		{
			// get the handler class by URI
			// for example, "/welcome/login" => "api.welcome.login"
			String uri = request.getUri();
			
			Class<?> cls = Class.forName("api" + uri.replaceAll("/", "."));
			
			
			try
			{
				ApiHandler apiHandler = ((Class<ApiHandler>) cls).newInstance();
				
				// get params
				Map<String, String> files = new HashMap<String, String>();
			    Method method = request.getMethod();
			    if (Method.PUT.equals(method) || Method.POST.equals(method)) {
			    	request.parseBody(files);
			    }
			    Map<String, String> params = request.getParms();
			    
				// give some help text
				if(params.containsKey("_help")) {
					JSONObject rtn = new JSONObject();
					rtn.put("rtnCode", "200 ok");
					rtn.put("help", apiHandler.showHelp());
					return rtn;
				}
				
				// check if login is required
				if(apiHandler.requireLogin && session.getActiveUserId()<=0) {
					
					return CommonResponses.showUnauthorized();
				}
				
				// check params
				
				if(!apiHandler.checkParams(params)) {
					JSONObject rtn = CommonResponses.showParamError();
					rtn.put("help", apiHandler.showHelp());
					return rtn;
				}
				
				// pass the request to the handler
				return apiHandler.main(params, session);
				
			} catch (Exception e)
			{
				return CommonResponses.showException(e);
			}

		} catch (ClassNotFoundException e)
		{
			// handler class not found
			return CommonResponses.showNotFound();
		}
	}