Example #1
0
        public static USSDResponse executeRequest(USSDApplication application, USSDRequest request, USSDSession <Object> session)
        {
            USSDResponse         ussdResponse = new BaseUSSDResponse();
            USSDSession <object> ussdSession  = session;

            if (ussdSession == null)
            {
                SessionProvider sessionProvider = application.getSessionProvider();
                if (sessionProvider == null)
                {
                    throw new BantUException(String.Format("The instance must not be null")); // ToDo: Implement as is in Java
                }
                ussdSession = sessionProvider.getSession(request);
                if (ussdSession == null)
                {
                    throw new NullReferenceException(String.Format("The %s instance returned by %s must not be null"));
                }
            }

            request.setApplication(application);
            ussdResponse.setSession(session);

            if (!application.getFilters().Contains(CoreFilter))
            {
                application.getFilters().Add(CoreFilter);
            }

            USSDFilteringChain chain = createFilteringChain(application);

            chain.proceed(request, ussdSession, ussdResponse);

            if (request is GetRequest || request is PostRequest)
            {
                if (ussdResponse.getWindow().isForm())
                {
                    ussdResponse.setResponseType(ResponseType.FORM);
                }
                else
                {
                    ussdResponse.setResponseType(ResponseType.MESSAGE);
                }
            }
            else
            {
                ussdResponse.setResponseType(ResponseType.MESSAGE);
            }

            if (ussdResponse.getResponseType() == ResponseType.FORM)
            {
                BaseNavigationCache navigationCache = (BaseNavigationCache)application.getNavigationCache();

                if (navigationCache != null)
                {
                    try
                    {
                        navigationCache.storeWindow(ussdResponse.getWindow(), request, session);
                    }
                    catch (Exception ex)
                    {
                        throw new WindowStoreFailedException(ussdResponse.getWindow().getId(), request, ussdResponse, session);
                    }
                }
                else
                {
                    ussdSession.close();
                }
            }

            return(ussdResponse);
        }