/// <summary>
        /// Gets the request stream
        /// </summary>
        /// <returns>The request stream</returns>
        /// <param name="contentlength">The content length to use</param>
        public Stream GetRequestStream(long contentlength = -1)
        {
            // Prevent in-memory buffering causing out-of-memory issues
            if (this.m_request is HttpWebRequest request)
            {
                if (contentlength >= 0)
                {
                    request.ContentLength = contentlength;
                }
                if (request.ContentLength >= 0)
                {
                    request.AllowWriteStreamBuffering = false;
                }
            }

            if (m_state == RequestStates.GetRequest)
            {
                return((Stream)m_asyncRequest.GetResponseOrStream());
            }

            if (m_state != RequestStates.Created)
            {
                throw new InvalidOperationException();
            }

            m_asyncRequest = new AsyncWrapper(this, true);
            m_state        = RequestStates.GetRequest;

            return(TrySetTimeout((Stream)m_asyncRequest.GetResponseOrStream(), m_activity_timeout));
        }
Exemple #2
0
 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 public void DataUpdateCallback(RequestStates requestState)
 {
     if (requestState == RequestStates.Completed)
     {
         needDataUpdate++;
     }
 }
Exemple #3
0
        /// <summary>
        /// Gets the request stream
        /// </summary>
        /// <returns>The request stream</returns>
        public Stream GetRequestStream()
        {
            if (m_state == RequestStates.GetRequest)
            {
                return((Stream)m_asyncRequest.GetResponseOrStream());
            }

            if (m_state != RequestStates.Created)
            {
                throw new InvalidOperationException();
            }

            m_asyncRequest = new AsyncWrapper(this, true);
            m_state        = RequestStates.GetRequest;

            return(TrySetTimeout((Stream)m_asyncRequest.GetResponseOrStream(), m_activity_timeout));
        }
        /// <summary>
        /// Gets the response object
        /// </summary>
        /// <returns>The web response</returns>
        public WebResponse GetResponse()
        {
            if (m_state == RequestStates.GetResponse)
            {
                return((WebResponse)m_asyncResponse.GetResponseOrStream());
            }

            if (m_state == RequestStates.Done)
            {
                throw new InvalidOperationException();
            }

            m_asyncRequest  = null;
            m_asyncResponse = new AsyncWrapper(this, false);
            m_state         = RequestStates.GetResponse;

            return((WebResponse)m_asyncResponse.GetResponseOrStream());
        }
        /// <summary>
        /// Gets the request stream
        /// </summary>
        /// <returns>The request stream</returns>
        public Stream GetRequestStream()
        {
            if (m_state == RequestStates.GetRequest)
            {
                return((Stream)m_asyncRequest.GetResponseOrStream());
            }

            if (m_state != RequestStates.Created)
            {
                throw new InvalidOperationException();
            }

            m_asyncRequest = new AsyncWrapper(this, true);
            m_state        = RequestStates.GetRequest;

            var s = (Stream)m_asyncRequest.GetResponseOrStream();

            try { s.WriteTimeout = m_activity_timeout; }
            catch { }

            return(s);
        }
        /// <summary>
        /// Executes 3 requests to the Text Analytics API in a defined order.
        /// 1 get the language, 2 get the key phrases, 3 get the sentiment of
        /// the <see cref="TextToAnalyse"/>.
        /// For each of the 3 phases, first configure, then send the request
        /// Wait for the result and resolve it. Finally, update the database.
        /// </summary>
        public async Task Start()
        {
            Response = new TextAnalyticsApiResponse();
            RequestConfiguration config;
            string jsonResponse;

            State = RequestStates.AwaitingResponse;

            // phase 1
            config       = Configure("languages");
            jsonResponse = await SendRequest(config);

            State = UpdateLanguage(jsonResponse);
            // phase 2
            config       = Configure("keyPhrases");
            jsonResponse = await SendRequest(config);

            State = UpdateKeyPhrases(jsonResponse);
            // phase 3
            config       = Configure("sentiment");
            jsonResponse = await SendRequest(config);

            State = UpdateSentiment(jsonResponse);

            // Prints results in the console
            Response.Describe();

            // TODO: extract method + interface?
            // Insert into database
            var DB        = new DataAccess();
            var dbSuccess = DB.UpdateTextAnalytics(Response.Language, Response.Score, TextToAnalyse);

            // Resolve final state
            if (dbSuccess)
            {
                State = RequestStates.DatabaseUpdated;
            }
            // TODO: GetStatistics
        }
        /// <summary>
        /// Gets the response object
        /// </summary>
        /// <returns>The web response</returns>
        public WebResponse GetResponse()
        {
            if (m_state == RequestStates.GetResponse)
                return (WebResponse)m_asyncResponse.GetResponseOrStream();

            if (m_state == RequestStates.Done)
                throw new InvalidOperationException();

            m_asyncRequest = null;
            m_asyncResponse = new AsyncWrapper(this, false);
            m_state = RequestStates.GetResponse;

            return (WebResponse)m_asyncResponse.GetResponseOrStream();
        }
        /// <summary>
        /// Gets the request stream
        /// </summary>
        /// <returns>The request stream</returns>
        public Stream GetRequestStream()
        {
            if (m_state == RequestStates.GetRequest)
                return (Stream)m_asyncRequest.GetResponseOrStream();

            if (m_state != RequestStates.Created)
                throw new InvalidOperationException();

            m_asyncRequest = new AsyncWrapper(this, true);
            m_state = RequestStates.GetRequest;

			return TrySetTimeout((Stream)m_asyncRequest.GetResponseOrStream(), m_activity_timeout);
        }
        /// <summary>
        /// Gets the request stream
        /// </summary>
        /// <returns>The request stream</returns>
        public Stream GetRequestStream()
        {
            if (m_state == RequestStates.GetRequest)
                return (Stream)m_asyncRequest.GetResponseOrStream();

            if (m_state != RequestStates.Created)
                throw new InvalidOperationException();

            m_asyncRequest = new AsyncWrapper(this, true);
            m_state = RequestStates.GetRequest;

            var s = (Stream)m_asyncRequest.GetResponseOrStream();

            try { s.WriteTimeout = m_activity_timeout; }
            catch { }

            return s;
        }
Exemple #10
0
        public async Task <IActionResult> PutRequest([FromRoute] string id, [FromBody] Request request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != request.RequestId)
            {
                return(BadRequest());
            }

            if (!UsertExists(request.Username))
            {
                return(BadRequest("User does not exist."));
            }

            if (!RequestTypeExists(request.RequestTypeId))
            {
                return(BadRequest("Request Type does not exist."));
            }

            if (!RequestStates.IsValidState(request.State))
            {
                return(BadRequest("Request State does not exist."));
            }

            _context.Entry(request).State = EntityState.Modified;

            try
            {
                if (request.Attachment != null)
                {
                    try
                    {
                        string folderName = "forms";
                        if (string.IsNullOrWhiteSpace(_hostingEnvironment.WebRootPath))
                        {
                            _hostingEnvironment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                        }
                        string webRootPath   = _hostingEnvironment.WebRootPath;
                        string newPath       = Path.Combine(webRootPath, folderName);
                        string attachmentDir = Path.Combine(newPath, request.RequestId);

                        if (!Directory.Exists(attachmentDir))
                        {
                            Directory.CreateDirectory(attachmentDir);
                        }

                        string fileName = ContentDispositionHeaderValue.Parse(request.Attachment.ContentDisposition).FileName.Trim('"');
                        string fullPath = Path.Combine(attachmentDir, fileName);
                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            request.Attachment.CopyTo(stream);
                        }
                        request.AttachmentPath = string.Format("{0}://{1}/{2}/{3}/{4}", Request.Scheme, Request.Host.Value, folderName, request.RequestId, fileName);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return(BadRequest(e));
                        //throw e;
                    }
                }
                await _context.SaveChangesAsync();

                if (request.State != RequestStates.DB_REQUESTED)
                {
                    var smtpClient = new SmtpClient
                    {
                        Host        = "smtp.gmail.com", // set your SMTP server name here
                        Port        = 587,              // Port
                        EnableSsl   = true,
                        Credentials = new NetworkCredential("*****@*****.**", "noy3k.mail_svc")
                    };

                    using (var message = new MailMessage("*****@*****.**", _context.Users.Find(request.Username).Email)
                    {
                        Subject = string.Format("Status of the Request {0} has changed to {1}", request.RequestId, request.State),
                        Body = string.Format("Status of the Request {0} has changed to {1}", request.RequestId, request.State)
                    })
                    {
                        smtpClient.Send(message);
                    }
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RequestExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
            return(NoContent());
        }
 /// <summary>
 /// Reset the <see cref="State"/> to its default value.
 /// Should be executed between each call to <see cref="Start"/>.
 /// </summary>
 /// <returns>The resulting state</returns>
 public RequestStates Reset()
 {
     // Set start up state and return it
     return(State = RequestStates.NoData);
 }