Ejemplo n.º 1
0
 private string GetHideId(EwsRequest request, PayloadDefinition definition)
 {
     try
     {
         if (definition.IsHideRequired)
         {
             //Getting the HideID by making request to the URL provided
             string         hideId       = string.Empty;
             var            hideUrl      = new Uri(string.Format(CultureInfo.CurrentCulture, definition.HideUrl, Device.Address));
             HttpWebRequest httpRequest  = BuildRequest(hideUrl, HttpVerb.Post, definition, SessionId, definition.Payload);
             var            hideResponse = HttpMessenger.ExecuteRequest(httpRequest);
             if (!string.IsNullOrEmpty(hideResponse.Body))
             {
                 int start = hideResponse.Body.IndexOf("name=\"Hide\" VALUE=\"", StringComparison.Ordinal) + 19;
                 int end   = hideResponse.Body.IndexOf("\"", start, StringComparison.Ordinal);
                 hideId = (hideResponse.Body.Substring(start, (end - start)));
             }
             //Getting the session ID from the response recieved
             if (!string.IsNullOrEmpty(hideId))
             {
                 request.AddWithoutValidate("HideID", hideId);
             }
             return(hideId);
         }
     }
     catch (ArgumentOutOfRangeException ex)
     {
         throw new DeviceCommunicationException("Exception occurred while fetching the Hide Id ", ex.InnerException);
     }
     throw new DeviceCommunicationException("Could not get the Hide Id, please check the Hide Id URL.");
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepares the Payload
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public string PreparePayload(EwsRequest request)
        {
            var configValues      = request?.PayloadValues;
            var allParamsInSchema = request?.SchemaParemeters;

            var filledPayload = Payload;

            if (configValues?.Count <= 0)
            {
                return(filledPayload);
            }

            foreach (string item in configValues.Keys)
            {
                filledPayload = configValues[item].Equals("donotsend") ? filledPayload.Replace("" + item + "={" + item + "}&", string.Empty) : filledPayload.Replace("{" + item + "}", HttpUtility.UrlEncode(configValues[item]));

                if (allParamsInSchema.Contains(item))
                {
                    allParamsInSchema.Remove(item);
                }
            }
            if (allParamsInSchema.Count <= 0)
            {
                return(filledPayload);
            }

            foreach (string param in allParamsInSchema)
            {
                filledPayload = filledPayload.Replace("" + param + "={" + param + "}&", string.Empty);
                filledPayload = filledPayload.Replace("<dd:" + param + ">{" + param + "}</dd:" + param + ">", string.Empty);
            }
            return(filledPayload);
        }
Ejemplo n.º 3
0
        internal static List <PayloadDefinition> CreatePayloadDefinitions(EwsRequest request, string firmware, string deviceFamily)
        {
            List <PayloadDefinition> payloadDefinitions = new List <PayloadDefinition>();
            XDocument xDoc = GetPayloadXDoc(request.RequestType);
            //Geting the Number of Requests
            int numberOfRequests = GetPayLoadRequestCount(firmware, deviceFamily, request.RequestType, request.RequestSubtype);

            for (int i = 1; i <= numberOfRequests; i++)
            {
                PayloadDefinition payloadDefinition = GetPayLoadFromFile(xDoc, firmware, deviceFamily, request.RequestSubtype, i);
                payloadDefinitions.Add(payloadDefinition);
            }
            return(payloadDefinitions);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Submits the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>An <see cref="EwsResult" /> containing the result of the HTTP request(s).</returns>
        /// <exception cref="System.Exception">Validation failed with contract provided.</exception>
        public EwsResult Submit(EwsRequest request)
        {
            ////Create XML file and validae with the respective XML schema depending on the Filter Type
            if (request.Validate())
            {
                //Get the communication request
                List <PayloadDefinition> payloadDefinitions = EwsPayloadFactory.CreatePayloadDefinitions(request, Device.GetDeviceInfo().FirmwareRevision, _deviceFamily);

                //Applying on the device based on the ICommunicator
                return(CommunicateWithDevice(payloadDefinitions, request));
            }

            throw new Exception("Validation failed with contract provided.");
        }
Ejemplo n.º 5
0
        private void FillStateValues(EwsRequest request, string sessionId, PayloadDefinition definition)
        {
            if (definition.IsViewStateRequired)
            {
                Uri viewStateUrl = null;
                //Getting the viewstate by making request to the URL provided
                //Implemented the below functionality to get the tray ID for which the setting needs to be implemented
                if (request.RequestType.Equals("ManageTrays") && request.RequestSubtype.Equals("Tray"))
                {
                    viewStateUrl = new Uri(string.Format(CultureInfo.CurrentCulture, definition.ViewStateUrl, Device.Address, request.PayloadValues["Tray"]));
                }
                else
                {
                    viewStateUrl = new Uri(string.Format(CultureInfo.CurrentCulture, definition.ViewStateUrl, Device.Address));
                }
                HttpWebRequest httpRequest       = BuildRequest(viewStateUrl, HttpVerb.Get, definition, sessionId);
                var            viewStateResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string viewstate = GetViewState(viewStateResponse.Body);
                if (!string.IsNullOrEmpty(viewstate))
                {
                    //Manage trays expects HPViewState and not just ViewState in the payload
                    if (request.RequestType.Equals("ManageTrays") && request.RequestSubtype.Equals("Tray"))
                    {
                        viewstate = System.Web.HttpUtility.HtmlDecode(viewstate);
                        request.AddWithoutValidate("HPViewState", viewstate);
                    }
                    else
                    {
                        request.AddWithoutValidate("ViewState", viewstate);
                    }
                }
            }

            if (definition.IsWizardIdRequired)
            {
                //Getting the viewstate by making request to the URL provided
                var            wizardIdUrl    = new Uri(string.Format(CultureInfo.CurrentCulture, definition.WizardIdUrl, Device.Address));
                HttpWebRequest httpRequest    = BuildRequest(wizardIdUrl, HttpVerb.Get, definition, sessionId);
                var            wizardResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string wizardId = GetWizardId(wizardResponse.Body);
                if (!string.IsNullOrEmpty(wizardId))
                {
                    request.AddWithoutValidate("WizardID", wizardId);
                }
            }
        }
Ejemplo n.º 6
0
        protected override string SignIn(string sessionId)
        {
            PayloadDefinition definition = EwsPayloadFactory.GetPayLoad(Device.GetDeviceInfo().FirmwareRevision, "Jedi", "SignIn", string.Empty, 1);

            EwsRequest signInRequest = new EwsRequest("SignIn", "SignIn", null);

            signInRequest.Add("Password", Password.Replace("admin:", string.Empty));
            string signInPayload  = definition.PreparePayload(signInRequest);
            string targetUrl      = string.Format(CultureInfo.CurrentCulture, JediDeviceSignInUrl, Device.Address);
            var    signInResponse = ExecuteRequest(new Uri(targetUrl), signInPayload, definition, null);

            sessionId = signInResponse.Headers.GetValues("Cookie")?.FirstOrDefault()?.Split(';').FirstOrDefault();
            sessionId = sessionId?.Replace("sessionId=", string.Empty);

            return(sessionId);
        }
        /// <summary>
        /// Execution Entry Point
        /// </summary>
        /// <param name="executionData"></param>
        /// <returns></returns>
        public PluginExecutionResult Execute(PluginExecutionData executionData)
        {
            if (executionData == null)
            {
                return(new PluginExecutionResult(PluginResult.Skipped, "Execution Data is empty!"));
            }

            _activityData = executionData.GetMetadata <EwsHeadlessActivityData>();
            var printDeviceInfo = (PrintDeviceInfo)executionData.Assets.First();

            _device = DeviceConstructor.Create(printDeviceInfo);
            IEwsCommunicator communicator = EwsCommunicatorFactory.Create(_device);

            EwsPayloadFactory.AddContractLocation(Path.Combine(executionData.Environment.PluginSettings["DATPayLoadRepository"], "EWSContractFiles"));
            EwsPayloadFactory.AddContractLocation(Path.Combine(executionData.Environment.PluginSettings["DATPayLoadRepository"], "EWSPayLoads"));

            if (_activityData.Operation.Equals(_activityData.DeviceSpecificOperation))
            {
                _activityData.DeviceSpecificOperation = string.Empty; //to match the subfilter type in the XML
            }

            //create request to access EWS DAT payloads
            EwsRequest ewsRequest = communicator.CreateRequest(_activityData.Operation, _activityData.DeviceSpecificOperation);

            foreach (var configurationValue in _activityData.ConfigurationValues)
            {
                string valueToBePassed = configurationValue.Value;
                if (valueToBePassed.Equals("[CurrentUser]", StringComparison.OrdinalIgnoreCase))
                {
                    valueToBePassed = executionData.Credential.UserName;
                }

                if ((_device is SiriusDevice) && (_activityData.Operation.Equals("Administration")) && (configurationValue.Key.Equals("Password")))
                {
                    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(valueToBePassed);
                    valueToBePassed = Convert.ToBase64String(plainTextBytes);
                }
                ewsRequest.Add(configurationValue.Key, valueToBePassed);
            }

            EwsResult result = communicator.Submit(ewsRequest);

            return(result.Exception != null ? new PluginExecutionResult(PluginResult.Failed, result.Exception) : new PluginExecutionResult(PluginResult.Passed));
        }
Ejemplo n.º 8
0
        protected virtual EwsResult CommunicateWithDevice(List <PayloadDefinition> payloadDefinitions, EwsRequest request)
        {
            List <HttpResponse> commResponseList = new List <HttpResponse>();

            try
            {
                string hideId     = string.Empty;
                bool   isSignedIn = false;

                foreach (PayloadDefinition definition in payloadDefinitions)
                {
                    bool executeRequest = true;
                    if (definition.IsSessionIdRequired && string.IsNullOrEmpty(SessionId))
                    {
                        SessionId = GetSessionId(definition);
                    }
                    if (definition.IsHideRequired && string.IsNullOrEmpty(hideId))
                    {
                        hideId         = GetHideId(request, definition);
                        executeRequest = false;
                    }
                    if (request.RequestSubtype.Equals("WebProxy"))
                    {
                        alternateCSRFUrl = string.Format(CultureInfo.CurrentCulture, "https://{0}/webserviceproxy.htm", Device.Address);
                    }
                    else if (request.RequestSubtype.Equals("NetworkSettings"))
                    {
                        alternateCSRFUrl = string.Format(CultureInfo.CurrentCulture, "https://{0}/snmp_creds.html", Device.Address);
                    }
                    else if (request.RequestSubtype.Equals("ManageSupplies") || request.RequestSubtype.Equals("WithHolePunch") || request.RequestSubtype.Equals("WithoutHolePunch") ||
                             request.RequestSubtype.Equals("FaxReceiveSetup"))
                    {
                        alternateCSRFUrl = string.Format(CultureInfo.CurrentCulture, definition.TargetUrl, Device.Address);
                    }

                    // Sign in with Password provided
                    if (!isSignedIn && !string.IsNullOrEmpty(Password))
                    {
                        SessionId  = SignIn(SessionId);
                        isSignedIn = true;
                    }

                    FillStateValues(request, SessionId, definition);

                    //This is the actual request with the session ID
                    string targetUrl     = string.Format(CultureInfo.CurrentCulture, definition.TargetUrl, Device.Address);
                    string filledPayload = definition.PreparePayload(request);
                    if (executeRequest)
                    {
                        var commResponse = ExecuteRequest(new Uri(targetUrl), filledPayload, definition, hideId);
                        commResponseList.Add(commResponse);
                    }
                    //let's sleep for a second
                    Delay.Wait(TimeSpan.FromSeconds(2));
                }

                return(new EwsResult(commResponseList));
            }
            catch (Exception ex)
            {
                return(new EwsResult(commResponseList, ex));
            }
        }