Initial commit from SVN.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package com.foundation.web;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.common.debug.Debug;
|
||||
import com.common.io.StreamSupport;
|
||||
import com.common.thread.IRunnable;
|
||||
import com.common.util.LiteHashMap;
|
||||
import com.foundation.web.controller.HtmlViewController;
|
||||
import com.foundation.web.controller.WebRequestController;
|
||||
import com.foundation.web.interfaces.IRequest;
|
||||
import com.foundation.web.interfaces.IResponse;
|
||||
|
||||
/**
|
||||
* Used to automate creating views in the HTML using stateful view controllers.
|
||||
*/
|
||||
public final class FrameworkController extends WebRequestController {
|
||||
private static final Integer INVALID_CLIENT_ID = new Integer(0);
|
||||
/* (non-Javadoc)
|
||||
* @see com.foundation.web.IWebRequestHandler#processRequest(com.foundation.web.interfaces.IRequest, com.foundation.web.interfaces.IResponse, com.foundation.web.SessionData, com.foundation.SecureSessionData)
|
||||
*/
|
||||
public void processRequest(IRequest request, IResponse response, SessionData sessionData, SecureSessionData secureSessionData) {
|
||||
String result = null;
|
||||
|
||||
//Note that all calls to this method within the context of a single session are single threaded - the session's request handler is used by the web application to thread the requests on the session's thread.//
|
||||
try {
|
||||
String command = request.getParameterAsString("Request", null, true, true);
|
||||
WebSession session = (WebSession) request.getSession();
|
||||
|
||||
if(command.equalsIgnoreCase("CreateId")) {
|
||||
int clientId = session.getNextClientId();
|
||||
|
||||
session.setCurrentClientId(clientId);
|
||||
result = "{\"result\": " + clientId + " }";
|
||||
}//if//
|
||||
else {
|
||||
int clientId = request.getParameterAsInteger("ClientId", INVALID_CLIENT_ID).intValue();
|
||||
|
||||
if(clientId == 0) {
|
||||
//TODO: Report an error.
|
||||
}//if//
|
||||
else if(clientId != session.getCurrentClientId()) {
|
||||
//Set the current client ID as this one.//
|
||||
session.setCurrentClientId(clientId);
|
||||
//Send a response to the client asking for the client view metadata so that the view controllers can be re-initialized to service their requests.//
|
||||
//The client should then re-send the current request.//
|
||||
result = "{\"result\":\"client-switch\"}";
|
||||
}//else if//
|
||||
else {
|
||||
if(command.equals("CallView")) {
|
||||
Integer id = request.getParameterAsInteger("DisplayId", null);
|
||||
HtmlViewController controller = session.getFrameworkView(id.intValue());
|
||||
|
||||
if(controller != null) {
|
||||
controller.processRequest(request.getParameterAsString("Query", null, true, true), request, response, sessionData, secureSessionData);
|
||||
}//if//
|
||||
else {
|
||||
Debug.log(new RuntimeException("Failed to retrieve a valid controller."));
|
||||
result = "{\"result\":\"not-found\"}";
|
||||
}//else//
|
||||
}//if//
|
||||
else if(command.equals("RefreshView")) {
|
||||
Integer id = request.getParameterAsInteger("DisplayId", null);
|
||||
HtmlViewController controller = session.getFrameworkView(id.intValue());
|
||||
|
||||
if(controller != null) {
|
||||
result = "{\"result\":\"success\", \"content\":\"" + HtmlViewController.encodeFramework(controller.getView()) + "\"}";
|
||||
}//if//
|
||||
else {
|
||||
Debug.log(new RuntimeException("Failed to retrieve a valid controller while refreshing."));
|
||||
result = "{\"result\":\"not-found\"}";
|
||||
}//else//
|
||||
}//else if//
|
||||
else if(command.equals("OpenView")) {
|
||||
String controllerName = request.getParameterAsString("Controller", null);
|
||||
Integer id = request.getParameterAsInteger("DisplayId", null);
|
||||
Class controllerClass = Class.forName(controllerName);
|
||||
|
||||
if(controllerClass == null) {
|
||||
result = "{\"result\":\"missing-class\"}";
|
||||
}//if//
|
||||
else if(HtmlViewController.class.isAssignableFrom(controllerClass)) {
|
||||
if(id != null) {
|
||||
HtmlViewController controller = (HtmlViewController) controllerClass.newInstance();
|
||||
|
||||
//Initialize the framework components of the view.//
|
||||
((BaseHtmlViewController) controller).initialize(id.intValue(), session, sessionData, secureSessionData);
|
||||
|
||||
//If the view can be initialized then place it in the map and return success and the view's content.//
|
||||
//Ensure that the request is run on the view's thread.//
|
||||
if(controller.initialize()) {
|
||||
HtmlViewController old = session.unregisterFrameworkView(id.intValue());
|
||||
|
||||
//Note: It is possible for the client and server to become out of sync if the client is refreshed because the server will retain all the view controller info, but the client will have no knowledge of the views.//
|
||||
if(old != null) {
|
||||
old.release();
|
||||
}//if//
|
||||
|
||||
//Map the view to the id for later retrieval.//
|
||||
session.registerFrameworkView(id.intValue(), controller);
|
||||
//Ensure that the view creation is run on the view's thread in case reflections need to be generated.//
|
||||
result = "{\"result\":\"success\", \"content\":\"" + HtmlViewController.encodeFramework(controller.getView()) + "\"}";
|
||||
}//if//
|
||||
else {
|
||||
controller.release();
|
||||
result = "{\"result\":\"disallowed\"}";
|
||||
}//else//
|
||||
}//if//
|
||||
else {
|
||||
result = "{\"result\":\"System Failure: Missing display ID.\"}";
|
||||
}//else//
|
||||
}//else if//
|
||||
else {
|
||||
result = "{\"result\":\"invalid-class\"}";
|
||||
}//else//
|
||||
}//else if//
|
||||
else if(command.equalsIgnoreCase("CloseView")) {
|
||||
Integer id = request.getParameterAsInteger("DisplayId", null);
|
||||
HtmlViewController controller = session.unregisterFrameworkView(id.intValue());
|
||||
|
||||
if(controller != null) {
|
||||
controller.release();
|
||||
result = "{\"result\":\"success\"}";
|
||||
}//if//
|
||||
else {
|
||||
//Note: This might occur normally if the client looses its session.//
|
||||
result = "{\"result\":\"not-found\"}";
|
||||
}//else//
|
||||
}//else if//
|
||||
}//else//
|
||||
}//else//
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
}//catch//
|
||||
|
||||
if(result != null) {
|
||||
response.setContent(new JsonContent(result));
|
||||
}//if//
|
||||
}//processRequest()//
|
||||
}//FrameworkController//
|
||||
Reference in New Issue
Block a user