Ejemplo n.º 1
0
        /// <summary>
        /// this is a helper function for to send binary data to a resource
        /// it is not worth running the other insert/saves through here, as this would involve
        /// double buffering/copying of the bytes
        /// </summary>
        /// <param name="targetUri"></param>
        /// <param name="inputStream"></param>
        /// <param name="type"></param>
        /// <param name="contentType">the contenttype to use in the request, if NULL is passed, factory default is used</param>
        /// <param name="slugHeader">the slugHeader to use in the request, if NULL is passed, factory default is used</param>
        /// <param name="etag">The http etag to pass into the request</param>
        /// <param name="data">The async data needed for notifications</param>
        /// <returns>Stream from the server response. You should close this stream explicitly.</returns>
        private Stream StreamSend(Uri targetUri,
                                  Stream inputStream,
                                  GDataRequestType type,
                                  string contentType,
                                  string slugHeader,
                                  string etag,
                                  AsyncSendData data)
        {
            Tracing.Assert(targetUri != null, "targetUri should not be null");
            if (targetUri == null)
            {
                throw new ArgumentNullException("targetUri");
            }

            if (inputStream == null)
            {
                Tracing.Assert(inputStream != null, "payload should not be null");
                throw new ArgumentNullException("inputStream");
            }

            if (type != GDataRequestType.Insert && type != GDataRequestType.Update)
            {
                Tracing.Assert(type != GDataRequestType.Insert && type != GDataRequestType.Update, "type needs to be insert or update");
                throw new ArgumentNullException("type");
            }

            IGDataRequest request = this.RequestFactory.CreateRequest(type, targetUri);

            request.Credentials = this.Credentials;

            if (data != null)
            {
                GDataGAuthRequest gr = request as GDataGAuthRequest;
                if (gr != null)
                {
                    gr.AsyncData = data;
                }
            }

            // set the contenttype of the request
            if (contentType != null)
            {
                GDataRequest r = request as GDataRequest;
                if (r != null)
                {
                    r.ContentType = contentType;
                }
            }

            if (slugHeader != null)
            {
                GDataRequest r = request as GDataRequest;
                if (r != null)
                {
                    r.Slug = slugHeader;
                }
            }

            if (etag != null)
            {
                ISupportsEtag ise = request as ISupportsEtag;
                if (ise != null)
                {
                    ise.Etag = etag;
                }
            }

            Stream outputStream = request.GetRequestStream();

            WriteInputStreamToRequest(inputStream, outputStream);

            request.Execute();
            outputStream.Close();
            return(new GDataReturnStream(request));
        }
Ejemplo n.º 2
0
        /////////////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        /// <summary>Inserts an AtomBase entry against a Uri. The overloaded
        /// version here will check if this is an AbstractEntry and if it has
        /// a media property set. If so, it will create a mime multipart envelope</summary>
        /// <param name="feedUri">the uri for the feed this object should be posted against</param>
        /// <param name="baseEntry">the entry to be inserted</param>
        /// <param name="type">the type of request to create</param>
        /// <param name="data">the async data payload</param>
        /// <returns> the response as a stream</returns>
        //////////////////////////////////////////////////////////////////////
        internal override Stream EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
        {
            if (feedUri == null)
            {
                throw new ArgumentNullException("feedUri");
            }
            Tracing.Assert(baseEntry != null, "baseEntry should not be null");
            if (baseEntry == null)
            {
                throw new ArgumentNullException("baseEntry");
            }

            AbstractEntry entry = baseEntry as AbstractEntry;

            // if the entry is not an abstractentry or if no media is set, do the default
            if (entry == null || entry.MediaSource == null)
            {
                return(base.EntrySend(feedUri, baseEntry, type, data));
            }

            Stream outputStream = null;
            Stream inputStream  = null;

            try
            {
                IGDataRequest request = this.RequestFactory.CreateRequest(type, feedUri);
                request.Credentials = this.Credentials;

                GDataRequest r = request as GDataRequest;

                if (r != null)
                {
                    r.ContentType = MediaService.MimeContentType;
                    r.Slug        = entry.MediaSource.Name;

                    GDataRequestFactory f = this.RequestFactory as GDataRequestFactory;
                    if (f != null)
                    {
                        f.CustomHeaders.Add("MIME-version: 1.0");
                    }
                }

                if (data != null)
                {
                    GDataGAuthRequest gr = request as GDataGAuthRequest;
                    if (gr != null)
                    {
                        gr.AsyncData = data;
                    }
                }


                outputStream = request.GetRequestStream();
                inputStream  = entry.MediaSource.GetDataStream();
                StreamWriter w = new StreamWriter(outputStream);

                w.WriteLine("Media multipart posting");
                CreateBoundary(w, GDataRequestFactory.DefaultContentType);
                baseEntry.SaveToXml(outputStream);
                w.WriteLine();
                CreateBoundary(w, entry.MediaSource.ContentType);
                WriteInputStreamToRequest(inputStream, outputStream);
                w.WriteLine();
                w.WriteLine("--" + MediaService.MimeBoundary + "--");
                w.Flush();
                request.Execute();
                outputStream.Close();
                outputStream = null;
                return(request.GetResponseStream());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (outputStream != null)
                {
                    outputStream.Close();
                }
                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }
        }