logrequest.cs :  » Network-Clients » RemoteCalendars » Google » GData » Client » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » RemoteCalendars 
RemoteCalendars » Google » GData » Client » logrequest.cs
/* Copyright (c) 2006 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
#region Using directives

#define USE_TRACING

using System;
using System.IO;
using System.Net;

#endregion

/////////////////////////////////////////////////////////////////////
// <summary>contains GDataLogingRequest
//  </summary>
////////////////////////////////////////////////////////////////////
namespace Google.GData.Client{

    //////////////////////////////////////////////////////////////////////
    /// <summary>base GDataRequestFactory implmentation</summary> 
    //////////////////////////////////////////////////////////////////////
    public class GDataLoggingRequestFactory : GDataGAuthRequestFactory
    {
        /// <summary>holds the filename for the input request</summary> 
        private string strInput;
        /// <summary>holds the filename for the output response</summary> 
        private string strOutput; 
        /// <summary>holds the filename for the combined logger</summary> 
        private string strCombined; 

        //////////////////////////////////////////////////////////////////////
        /// <summary>accessor method public string RequestFileName</summary> 
        /// <returns> </returns>
        //////////////////////////////////////////////////////////////////////
        public string RequestFileName
        {
            get {return this.strInput;}
            set {this.strInput = value;}
        }
        /////////////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        /// <summary>accessor method public string ResponseFileName</summary> 
        /// <returns> </returns>
        //////////////////////////////////////////////////////////////////////
        public string ResponseFileName
        {
            get {return this.strOutput;}
            set {this.strOutput = value;}
        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>accessor method public string CombinedLogFileName</summary> 
        /// <returns> </returns>
        //////////////////////////////////////////////////////////////////////
        public string CombinedLogFileName
        {
            get {return this.strCombined;}
            set {this.strCombined = value;}
        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>default constructor</summary> 
        //////////////////////////////////////////////////////////////////////
        public GDataLoggingRequestFactory(string service, string applicationName) : base(service, applicationName)
        {
            this.strInput = "GDatarequest.xml";
            this.strOutput = "GDataresponse.xml"; 
            this.strCombined = "GDatatraffic.log"; 

        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>default constructor</summary> 
        //////////////////////////////////////////////////////////////////////
        public GDataLoggingRequestFactory(string service, string applicationName, string library) : base(service, applicationName, library)
        {
            this.strInput = "GDatarequest.xml";
            this.strOutput = "GDataresponse.xml"; 
            this.strCombined = "GDatatraffic.log"; 

        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>default constructor</summary> 
        //////////////////////////////////////////////////////////////////////
        public override IGDataRequest CreateRequest(GDataRequestType type, Uri uriTarget)
        {
            return new GDataLoggingRequest(type, uriTarget, this, this.strInput, this.strOutput, this.strCombined);
        }
        /////////////////////////////////////////////////////////////////////////////



    }
    /////////////////////////////////////////////////////////////////////////////


    //////////////////////////////////////////////////////////////////////
    /// <summary>base GDataRequest implementation</summary> 
    //////////////////////////////////////////////////////////////////////
    public class GDataLoggingRequest : GDataGAuthRequest, IDisposable
    {
        /// <summary>holds the filename for the input request</summary> 
        private string strInput;
        /// <summary>holds the filename for the output response</summary> 
        private string strOutput; 
        /// <summary>holds the filename for the combined logger</summary> 
        private string strCombined; 
        /// <summary>holds carriage return/linefeed </summary>
        private static byte [] arrCRLF = { 13,10,13,10 }; 


        //////////////////////////////////////////////////////////////////////
        /// <summary>default constructor</summary> 
        //////////////////////////////////////////////////////////////////////
        internal GDataLoggingRequest(GDataRequestType type, Uri uriTarget, GDataGAuthRequestFactory factory, string strInputFileName, string strOutputFileName, string strCombinedLogFileName) : base(type, uriTarget, factory)
        {
            this.strInput = strInputFileName;
            this.strOutput = strOutputFileName;
            this.strCombined = strCombinedLogFileName;
        }
        /////////////////////////////////////////////////////////////////////////////

        
        //////////////////////////////////////////////////////////////////////
        /// <summary>does the real disposition</summary> 
        /// <param name="disposing">indicates if dispose called it or finalize</param>
        //////////////////////////////////////////////////////////////////////
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing); 
        }



        //////////////////////////////////////////////////////////////////////
        /// <summary>Executes the request and prepares the response stream. Also 
        /// does error checking</summary> 
        //////////////////////////////////////////////////////////////////////
        public override void Execute()
        {
            if (this.RequestCopy != null)
            {
                this.RequestCopy.Seek(0, SeekOrigin.Begin); 
                SaveStream(this.RequestCopy, this.strInput); 
                this.RequestCopy.Seek(0, SeekOrigin.Begin); 

            }
            try
            {
                base.Execute(); 

            } catch (GDataRequestException re)
            {
                Tracing.TraceMsg("Got into exception handling for base.execute"); 
                HttpWebResponse response = re.Response as HttpWebResponse;
            
                if (response != null)
                {
                    Stream   req = response.GetResponseStream(); 
                    SaveStream(req, this.strOutput); 
                }
                throw re; 
            }
        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>private void SaveStream()</summary> 
        /// <param name="stream">the stream to save </param>
        /// <param name="targetFile">the targetFilename  to save into </param>
        //////////////////////////////////////////////////////////////////////
        private void SaveStream(Stream stream, String targetFile)
        {
            if (stream != null)
            {
                // save the response to the log
                FileStream w = new FileStream(targetFile, FileMode.Create);
                FileStream x = new FileStream(this.strCombined, FileMode.Append); 

                const int size = 4096;
                byte[] bytes = new byte[4096];
                int numBytes;

                x.Write(GDataLoggingRequest.arrCRLF, 0, 4); 


                while((numBytes = stream.Read(bytes, 0, size)) > 0)
                {
                    w.Write(bytes, 0, numBytes);
                    x.Write(bytes, 0, numBytes);
                }
                w.Close();
                x.Write(GDataLoggingRequest.arrCRLF, 0, 4); 
                x.Close(); 
            }
        }
        /////////////////////////////////////////////////////////////////////////////


        //////////////////////////////////////////////////////////////////////
        /// <summary>gets the readable response stream</summary> 
        /// <returns> the response stream</returns>
        //////////////////////////////////////////////////////////////////////
        public override Stream GetResponseStream()
        {
            // get the real response stream, stream it to disk and return a filestream

            FileStream w = new FileStream(this.strOutput, FileMode.Create);
            FileStream x = new FileStream(this.strCombined, FileMode.Append); 

            MemoryStream mem = new MemoryStream(); 
            Stream   req = base.GetResponseStream(); 


            const int size = 4096;
            byte[] bytes = new byte[4096];
            int numBytes;

            x.Write(GDataLoggingRequest.arrCRLF, 0, 4); 

            while((numBytes = req.Read(bytes, 0, size)) > 0)
            {
                w.Write(bytes, 0, numBytes);
                x.Write(bytes, 0, numBytes);
                mem.Write(bytes, 0, numBytes); 
            }
            w.Close();
            mem.Seek(0, SeekOrigin.Begin); 
            x.Write(GDataLoggingRequest.arrCRLF, 0, 4); 
            x.Close(); 
            return mem; 
        }
        /////////////////////////////////////////////////////////////////////////////

    }
    /////////////////////////////////////////////////////////////////////////////
} 
/////////////////////////////////////////////////////////////////////////////
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.