Beispiel #1
0
        protected IHttpActionResult CallAsyncMacroExecute(string macroId, string data = "", string sessionMode = "MS", string accessMode = "RW")
        {
            // Get UserInfo
            var userInfo = (UserInfo)Request.Properties["UserInfo"];

            // Get values from X-HopexContext
            IEnumerable <string> hopexContextHeader;
            HopexContext         hopexContext;

            if (!Request.Headers.TryGetValues("X-HopexContext", out hopexContextHeader) || !HopexServiceHelper.TryGetHopexContext(hopexContextHeader.FirstOrDefault(), out hopexContext))
            {
                const string message = "Parameter \"X-Hopex-Context\" must be set in the header of your request. Example: HopexContext:{\"EnvironmentId\":\"IdAbs\",\"RepositoryId\":\"IdAbs\",\"ProfileId\":\"IdAbs\",\"DataLanguageId\":\"IdAbs\",,\"GuiLanguageId\":\"IdAbs\"}";
                Logger.Debug(message);
                return(BadRequest(message));
            }

            // Find the Hopex session
            var sspUrl      = ConfigurationManager.AppSettings["MegaSiteProvider"];
            var securityKey = ((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))["SecurityKey"];
            var mwasUrl     = HopexService.FindSession(sspUrl, securityKey, hopexContext.EnvironmentId, hopexContext.DataLanguageId, hopexContext.GuiLanguageId, hopexContext.ProfileId, userInfo.HopexAuthPerson, true);

            if (mwasUrl == null)
            {
                const string message = "Unable to get MWAS url. Please retry later and check your configuration if it doesn't work.";
                Logger.Debug(message);
                return(BadRequest(message));
            }
            mwasUrl = mwasUrl.ToLower().Replace("hopexmwas", "hopexapimwas");

            // Open the Hopex session
            var hopexService = new HopexService(mwasUrl, securityKey);
            var mwasSettings = InitMwasSettings();
            var mwasSessionConnectionParameters = InitMwasSessionConnectionParameters(sessionMode, accessMode, hopexContext, userInfo);

            if (!hopexService.TryOpenSession(mwasSettings, mwasSessionConnectionParameters, findSession: true))
            {
                var message = "Unable to open an Hopex session. Please check the values in the HopexContext header and retry.";
                Logger.Debug(message);
                return(BadRequest(message));
            }

            // Call the execution of the macro in async mode
            var asyncMacroResult = hopexService.CallAsyncMacroExecute(macroId, data);

            // If error occurs
            if (asyncMacroResult.Status != "InProgress")
            {
                // Close the Hopex session
                hopexService.CloseSession();
                // Return the bare result
                return(Ok(asyncMacroResult));
            }

            // Return the action id
            var response     = new HttpResponseMessage(HttpStatusCode.OK);
            var hopexSession = HopexServiceHelper.EncryptHopexSessionInfo(hopexService.MwasUrl, hopexService.HopexSessionToken);

            response.Headers.Add("X-HopexSession", hopexSession);
            response.Content = new StringContent(asyncMacroResult.ActionId);
            return(ResponseMessage(response));
        }