public ActionForward execute()

in flow/src/java/org/apache/struts/flow/FlowAction.java [92:202]


    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
             throws Exception {

        // Create and populate a Context for this request
        ServletWebContext context = new ServletWebContext();
        context.initialize(servlet.getServletContext(), request, response);
        context.put(Constants.ACTION_SERVLET_KEY,
                this.servlet);
        context.put(Constants.ACTION_CONFIG_KEY,
                mapping);
        context.put(Constants.ACTION_FORM_KEY,
                form);
        context.put(Constants.MESSAGE_RESOURCES_KEY,
                getResources(request));
        context.put(Constants.ACTION_KEY, this);

        String contid = request.getParameter("contid");
       
        // A FlowCall means the request came from client-side javascript that
        // expects the return type to be JSON
        boolean isFlowCall = (request.getParameter("FlowCall") != null);
        String func = getProperty("function", request, mapping);
        String controller = getProperty("controller", request, mapping);
        
        if ("true".equals(FlowConfiguration.getInstance().getProperty("flow.devMode"))) {
            SqlMap.reloadConfig();
        }
       
        Forward forward = new Forward();
        forward.setModule(mapping.getModuleConfig().getPrefix());
        forward.setAction(func);
        forward.setController(controller);
        context.put(Constants.FORWARD_KEY, forward);

        Interpreter interp = null;
        String scriptPattern = getProperty("script", request, mapping);
        if (scriptPattern == null) {
            interp = getInterpreter(mapping.getModuleConfig().getPrefix());
        } else {
            interp = getInterpreter(mapping.getModuleConfig().getPrefix(),
                forward.toUri(scriptPattern));
        }

        if (contid == null || contid.length() == 0) {

            // --- start a new flow

            List args = new LinkedList();

            if (func == null || func.length() == 0) {
                throw new ServletException("You must specify a function name to call");
            } else {
                
                String id = mapping.getProperty("id");
                if (id != null) {
                    args.add(new Interpreter.Argument("id", id));
                }
               
                // modify controller name
                StringBuffer sb = new StringBuffer();
                sb.append(Character.toUpperCase(controller.charAt(0)));
                sb.append(controller.substring(1));
                sb.append("Controller");
               
                // call control script function
                interp.callController(sb.toString(), func, args, context);
                
                return dispatchToPage(request, response, mapping, forward);
            }
        } else {
            // --- continue an existing flow

            // validate JSON in the body of a flowcall
            if (isFlowCall) {
                StringBuffer sb = new StringBuffer();
                char[] buffer = new char[1024];
                int len = 0;
                BufferedReader reader = request.getReader();
                while ((len = reader.read(buffer)) > 0 ) {
                    sb.append(buffer, 0, len);
                }
                String json = sb.toString();
                if (log.isDebugEnabled()) {
                    log.debug("processing json:"+json);
                }
                if (isValidJSON(json)) {
                    context.put("json", json);
                }
                
            }
            
            interp.handleContinuation(
                    request.getParameter("contid"), new LinkedList(), context);

            if (isFlowCall) {
                String json = new JSONSerializer().serialize(forward.getBizData());
                if (log.isDebugEnabled()) {
                    log.debug("returning json: "+json);
                }
                response.getWriter().write(json);
                response.getWriter().flush();
                response.getWriter().close();
                return null;
            } else {     
                return dispatchToPage(request, response, mapping, forward);
            }
        }
    }