Ejemplo n.º 1
0
        /// <summary>
        /// Submits the request and returns the response.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override IOResponse Submit(IORequest request)
        {
            // write request
            var webRequest = WriteWebRequest(request);

            if (webRequest == null)
            {
                throw new InvalidOperationException();
            }

            // submit request and retrieve response
            var webResponse = webRequest.GetResponse();

            if (webResponse == null)
            {
                throw new InvalidOperationException();
            }

            // read response
            var response = ReadResponse(webResponse, request);

            if (response == null)
            {
                throw new InvalidOperationException();
            }

            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="ioService"></param>
        /// <param name="uri"></param>
        /// <param name="accept"></param>
        public static TextReader Create(IIOService ioService, Uri uri, MediaRangeList accept, Encoding encoding)
        {
            Contract.Requires <ArgumentNullException>(ioService != null);
            Contract.Requires <ArgumentNullException>(uri != null);

            var request = new IORequest(uri, IOMethod.Get)
            {
                Accept = accept,
            };

            var response = ioService.Send(request);

            if (response.Status != IOStatus.Success)
            {
                throw new IOException();
            }

            if (!response.ContentType.Matches(accept))
            {
                throw new IOException();
            }

            if (encoding != null)
            {
                return(new IOTextReader(uri, response.Content, encoding));
            }
            else
            {
                return(new IOTextReader(uri, response.Content));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves a <see cref="WebRequest"/> for the given <see cref="IORequest"/>.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual WebRequest WriteWebRequest(IORequest request)
        {
            Contract.Requires <ArgumentNullException>(request != null);

            // generate new web request
            var webRequest = WebRequest.Create(request.ResourceUri);

            webRequest.Method      = request.Method;
            webRequest.ContentType = request.ContentType;

            if (request.Content != null &&
                request.Content.CanRead &&
                request.Content.Length > 0)
            {
                request.Content.CopyTo(webRequest.GetRequestStream());
            }

            // populate headers
            foreach (var pair in request.Headers)
            {
                webRequest.Headers.Add(pair.Key, pair.Value);
            }

            return(webRequest);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Returns <c>true</c> if the processor can handle this request.
 /// </summary>
 /// <param name="submit"></param>
 /// <returns></returns>
 public override Priority CanSend(IORequest submit)
 {
     if (submit.ResourceUri.Scheme == Uri.UriSchemeFile)
         return Priority.Default;
     else
         return Priority.Ignore;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="status"></param>
        public IOResponse(IORequest request, IOStatus status)
        {
            Contract.Requires<ArgumentNullException>(request != null);

            this.request = request;
            this.status = status;
            this.headers = new Headers();
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Submits the given request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IOResponse Send(IORequest request)
        {
            var transport = GetTransport(request);
            if (transport == null)
                return new IOResponse(request, IOStatus.NoTransport);

            return transport.Submit(request);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="status"></param>
        public IOResponse(IORequest request, IOStatus status)
        {
            Contract.Requires <ArgumentNullException>(request != null);

            this.request = request;
            this.status  = status;
            this.headers = new Headers();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="status"></param>
        /// <param name="content"></param>
        /// <param name="contentType"></param>
        public IOResponse(IORequest request, IOStatus status, Stream content, MediaRange contentType)
            : this(request, status)
        {
            Contract.Requires <ArgumentNullException>(request != null);
            Contract.Requires <ArgumentNullException>(content != null);

            this.content     = content;
            this.contentType = contentType;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="status"></param>
        /// <param name="content"></param>
        /// <param name="contentType"></param>
        public IOResponse(IORequest request, IOStatus status, Stream content, MediaRange contentType)
            : this(request, status)
        {
            Contract.Requires<ArgumentNullException>(request != null);
            Contract.Requires<ArgumentNullException>(content != null);

            this.content = content;
            this.contentType = contentType;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Submits the given request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IOResponse Send(IORequest request)
        {
            var transport = GetTransport(request);

            if (transport == null)
            {
                return(new IOResponse(request, IOStatus.NoTransport));
            }

            return(transport.Submit(request));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets the <see cref="IIOTransport"/> to handle the given request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        IIOTransport GetTransport(IORequest request)
        {
            Contract.Requires <ArgumentNullException>(request != null);

            return(transports
                   .Select(i => new { Priority = i.CanSend(request), Processor = i })
                   .Where(i => i.Priority != Priority.Ignore)
                   .OrderByDescending(i => i.Priority)
                   .Select(i => i.Processor)
                   .FirstOrDefault());
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets the <see cref="IIOTransport"/> to handle the given request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        IIOTransport GetTransport(IORequest request)
        {
            Contract.Requires<ArgumentNullException>(request != null);

            return transports
                .Select(i => new { Priority = i.CanSend(request), Processor = i })
                .Where(i => i.Priority != Priority.Ignore)
                .OrderByDescending(i => i.Priority)
                .Select(i => i.Processor)
                .FirstOrDefault();
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Returns <c>true</c> if the processor can handle this request.
 /// </summary>
 /// <param name="submit"></param>
 /// <returns></returns>
 public override Priority CanSend(IORequest submit)
 {
     if (submit.ResourceUri.Scheme == Uri.UriSchemeFile)
     {
         return(Priority.Default);
     }
     else
     {
         return(Priority.Ignore);
     }
 }
Ejemplo n.º 14
0
        public override IOResponse Submit(IORequest request)
        {
            var stm = typeof(Ref).Assembly
                .GetManifestResourceNames()
                .Where(i => i.EndsWith("." + request.ResourceUri.LocalPath.Substring(1)))
                .Select(i => typeof(Ref).Assembly.GetManifestResourceStream(i))
                .FirstOrDefault();
            if (stm == null)
                return null;

            return new IOResponse(request, IOStatus.Success, stm, "application/xml");
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Deserializes the <see cref="WebResponse"/> into a <see cref="IOResponse"/>.
        /// </summary>
        /// <param name="webResponse"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual IOResponse ReadResponse(WebResponse webResponse, IORequest request)
        {
            Contract.Requires<ArgumentNullException>(webResponse != null);
            Contract.Requires<ArgumentNullException>(request != null);

            // generate new response
            var response = new IOResponse(
                request,
                ReadResponseStatus(webResponse),
                webResponse.GetResponseStream(),
                webResponse.ContentType);

            // populate headers
            foreach (var name in webResponse.Headers.AllKeys)
                response.Headers[name] = webResponse.Headers[name];

            return response;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Submits the request and returns the response.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override IOResponse Submit(IORequest request)
        {
            // write request
            var webRequest = WriteWebRequest(request);
            if (webRequest == null)
                throw new InvalidOperationException();

            // submit request and retrieve response
            var webResponse = webRequest.GetResponse();
            if (webResponse == null)
                throw new InvalidOperationException();

            // read response
            var response = ReadResponse(webResponse, request);
            if (response == null)
                throw new InvalidOperationException();

            return response;
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Deserializes the <see cref="WebResponse"/> into a <see cref="IOResponse"/>.
        /// </summary>
        /// <param name="webResponse"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual IOResponse ReadResponse(WebResponse webResponse, IORequest request)
        {
            Contract.Requires <ArgumentNullException>(webResponse != null);
            Contract.Requires <ArgumentNullException>(request != null);

            // generate new response
            var response = new IOResponse(
                request,
                ReadResponseStatus(webResponse),
                webResponse.GetResponseStream(),
                webResponse.ContentType);

            // populate headers
            foreach (var name in webResponse.Headers.AllKeys)
            {
                response.Headers[name] = webResponse.Headers[name];
            }

            return(response);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="ioService"></param>
        /// <param name="uri"></param>
        /// <param name="accept"></param>
        public static TextReader Create(IIOService ioService, Uri uri, MediaRangeList accept, Encoding encoding)
        {
            Contract.Requires<ArgumentNullException>(ioService != null);
            Contract.Requires<ArgumentNullException>(uri != null);

            var request = new IORequest(uri, IOMethod.Get)
            {
                Accept = accept,
            };

            var response = ioService.Send(request);
            if (response.Status != IOStatus.Success)
                throw new IOException();

            if (!response.ContentType.Matches(accept))
                throw new IOException();

            if (encoding != null)
                return new IOTextReader(uri, response.Content, encoding);
            else
                return new IOTextReader(uri, response.Content);
        }
Ejemplo n.º 19
0
 public override Priority CanSend(IORequest request)
 {
     return request.ResourceUri.Scheme == "nx-example" ? Priority.High : Priority.Ignore;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Handles a submitted request.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public abstract IOResponse Submit(IORequest request);
Ejemplo n.º 21
0
 /// <summary>
 /// Return <c>true</c> if your <see cref="IIOTransport"/> supports the given <see cref="IORequest"/>.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public abstract Priority CanSend(IORequest request);
Ejemplo n.º 22
0
 public Priority CanSend(IORequest request)
 {
     Contract.Requires<ArgumentNullException>(request != null);
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
 public Priority CanSend(IORequest request)
 {
     Contract.Requires <ArgumentNullException>(request != null);
     throw new NotImplementedException();
 }
Ejemplo n.º 24
0
 public IOResponse Send(IORequest request)
 {
     Contract.Requires <ArgumentNullException>(request != null);
     Contract.Ensures(Contract.Result <IOResponse>() != null);
     throw new NotImplementedException();
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Retrieves a <see cref="WebRequest"/> for the given <see cref="IORequest"/>.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual WebRequest WriteWebRequest(IORequest request)
        {
            Contract.Requires<ArgumentNullException>(request != null);

            // generate new web request
            var webRequest = WebRequest.Create(request.ResourceUri);
            webRequest.Method = request.Method;
            webRequest.ContentType = request.ContentType;

            if (request.Content != null &&
                request.Content.CanRead &&
                request.Content.Length > 0)
                request.Content.CopyTo(webRequest.GetRequestStream());

            // populate headers
            foreach (var pair in request.Headers)
                webRequest.Headers.Add(pair.Key, pair.Value);

            return webRequest;
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Return <c>true</c> if your <see cref="IIOTransport"/> supports the given <see cref="IORequest"/>.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public abstract Priority CanSend(IORequest request);
Ejemplo n.º 27
0
 public override Priority CanSend(IORequest request)
 {
     return Priority.Low;
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Handles a submitted request.
 /// </summary>
 /// <param name="request"></param>
 /// <returns></returns>
 public abstract IOResponse Submit(IORequest request);
Ejemplo n.º 29
0
        /// <summary>
        /// Retrieves a <see cref="WebRequest"/> for the given <see cref="ModelRequest"/>.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual IORequest WriteIORequest(ModelRequest request)
        {
            Contract.Requires<ArgumentNullException>(request != null);

            var mediaType = GetMediaType(request);
            if (mediaType == null)
                return null;

            // obtain and build request parts
            var cnt = (string)null;
            var stm = new MemoryStream();
            var uri = request.ResourceUri;
            if (IsQuery(request) && request.Body != null)
            {
                // serialize body to string
                var u = new UriBuilder(uri);
                var s = new StringBuilder(u.Query);
                using (var w = new StringWriter(s))
                    Serialize(w, request.Body, mediaType);
                u.Query = s.ToString();

                // replace uri
                uri = u.Uri;
            }
            else if (request.Body != null)
            {
                // serialize data to body
                using (var w = new StreamWriter(stm, request.Encoding, 1024, true))
                    Serialize(w, request.Body, mediaType);

                // configure body
                cnt = mediaType;
                stm.Position = 0;
            }

            // generate new web request
            var ioRequest = new IORequest(uri, GetMethod(request));
            ioRequest.ContentType = cnt;
            ioRequest.Headers.Add(request.Headers);
            ioRequest.Content = stm;

            return ioRequest;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="ioService"></param>
        /// <param name="uri"></param>
        /// <param name="accept"></param>
        public static XmlReader Create(IIOService ioService, Uri uri, MediaRangeList accept)
        {
            Contract.Requires<ArgumentNullException>(ioService != null);
            Contract.Requires<ArgumentNullException>(uri != null);

            // default acceptable media types
            accept = !accept.IsEmpty ? accept : XML_MEDIA_RANGES;

            // format request
            var request = new IORequest(uri, IOMethod.Get)
            {
                Accept = accept,
            };

            // get response
            var response = ioService.Send(request);
            if (response.Status != IOStatus.Success)
                throw new XmlException();

            // acceptable response?
            if (!response.ContentType.Matches(accept))
                throw new XmlException();

            return new IOXmlReader(uri, response.Content);
        }
Ejemplo n.º 31
0
 public override Priority CanSend(IORequest request)
 {
     return(Priority.Low);
 }
Ejemplo n.º 32
0
 public IOResponse Submit(IORequest request)
 {
     Contract.Requires<ArgumentNullException>(request != null);
     Contract.Ensures(Contract.Result<IOResponse>() != null);
     throw new NotImplementedException();
 }