public XPathServiceOperationNavigator ( HttpContext httpContext, Context context, Transform.Context transformContext, XPathNavigator navigator, TransformRequest request, TransformResponse response, XsltTransformationManager transformationManager ) {
     m_navigator = navigator;
     m_httpContext = httpContext;
     m_context = context;
     m_transformContext = transformContext;
     m_request = request;
     m_response = response;
     m_xslTransformationManager = transformationManager;
     m_transform = transformationManager.Transform;
     m_encoding = new UTF8Encoding();
 }
 public XmlServiceOperationReader(HttpContext httpContext, Context context, Transform.Context transformContext, XmlReader reader, TransformRequest request, TransformResponse response, XsltTransformationManager transformationManager) {
     m_reader = reader;
     m_httpContext = httpContext;
     m_context = context;
     m_transformContext = transformContext;
     m_request = request;
     m_response = response;
     m_xslTransformationManager = transformationManager;
     m_transform = transformationManager.Transform;
     m_encoding = new UTF8Encoding();
 }
        public IAsyncResult BeginProcessRequest ( HttpContext context, AsyncCallback cb, object extraData ) {

            m_stopwatch.Start();

            FileInfo fileInfo = new FileInfo(context.Request.MapPath(context.Request.CurrentExecutionFilePath));
            this.LogDebug("File Date: {0}; File Length: {1}", fileInfo.LastWriteTimeUtc, fileInfo.Length);
            m_CONTENT_IS_MEMCACHED = false;
            m_USE_MEMCACHED = false;
            m_httpContext = context;
            m_returnOutput = true;
            m_httpMethod = m_httpContext.Request.HttpMethod;
            m_memcachedClient = (Client)context.Application["memcached"];
            m_encoding = (UTF8Encoding)context.Application["encoding"];
            m_queueClient = (QueueClient)context.Application["queueclient"];
            m_hashkey = (string)context.Application["hashkey"];
            m_xmlServiceOperationManager = (XPathServiceOperationManager)context.Application["xmlServiceOperationManager"];
            m_xslTransformationManager = (XsltTransformationManager)context.Application["xslTransformationManager"];
            m_transform = m_xslTransformationManager.Transform;
            m_xsltParams = (Hashtable)context.Application["globalXsltParams"];
            m_transformContext = new Transform.Context(context, m_hashAlgorithm, (string)context.Application["hashkey"], fileInfo, (Hashtable)m_xsltParams.Clone(), fileInfo.LastWriteTimeUtc, fileInfo.Length);
            m_namedXsltHashtable = (Hashtable)context.Application["namedXsltHashtable"];
            m_xmlSourceETagDictionary = m_xmlServiceOperationManager.XmlSourceETagDictionary;
            m_xmlReaderDictionary = m_xmlServiceOperationManager.XmlReaderDictionary;
            m_context = new Context(context, m_hashAlgorithm, m_hashkey, fileInfo, fileInfo.LastWriteTimeUtc, fileInfo.Length);
            this.LogDebug("File Date: {0}; File Length: {1}", m_context.RequestXmlFileInfo.LastWriteTimeUtc, m_context.RequestXmlFileInfo.Length);
            m_nuxleusAsyncResult = new Nuxleus.Core.NuxleusAsyncResult(cb, extraData);
            m_callback = cb;
            m_nuxleusAsyncResult.m_context = context;
            m_builder = new StringBuilder();
            m_CONTENT_IS_MEMCACHED = false;
            m_USE_MEMCACHED = (bool)context.Application["usememcached"];
            Uri requestUri = new Uri(m_context.RequestUri);
            m_requestHashcode = m_context.GetRequestHashcode(true).ToString();
            m_lastModifiedKey = String.Format("LastModified:{0}", m_context.RequestUri.GetHashCode());
            m_lastModifiedDate = String.Empty;
            m_request = new TransformRequest();
            m_response = new TransformResponse();
            Guid requestGuid = Guid.NewGuid();
            m_request.ID = requestGuid;

            
            context.Response.ContentType = "text/xml";

            IEnumerator headers = context.Request.Headers.GetEnumerator();
            for (int i = 0; headers.MoveNext(); i++) {
                string local = context.Request.Headers.AllKeys[i].ToString();
                this.LogDebug("KeyName: {0}, KeyValue: {1}", local, context.Request.Headers[local]);
            }
            bool hasXmlSourceChanged = m_xmlServiceOperationManager.HasXmlSourceChanged(m_context.RequestXmlETag, requestUri);

            //if (m_USE_MEMCACHED) {

            //    string obj = (string)m_memcachedClient.Get(m_context.GetRequestHashcode(true).ToString());

            //    if (obj != null && !hasXmlSourceChanged) {
            //        m_response.TransformResult = (string)obj;
            //        m_CONTENT_IS_MEMCACHED = true;
            //        if ((bool)context.Application["debug"]) {
            //            context.Response.ContentType = "text";
            //        }
            //    } else {
            //        //m_writer = new StringWriter(m_builder);
            //        m_CONTENT_IS_MEMCACHED = false;
            //    }
            //} else {
            //    m_writer = new StringWriter(m_builder);
            //}

            m_writer = new StringWriter(m_builder);

            try {

                switch (m_httpMethod) {
                    case "GET":
                    case "HEAD":
                    case "POST": {                     
                            string name = String.Format("Name: {0}", context.Request.QueryString["name"]);
                            this.LogDebug("QueryString Length: {0}", context.Request.QueryString.Count);
                            this.LogDebug(name);
                            this.LogDebug("If-None-Match: {0}, RequestHashCode: {1}", context.Request.Headers["If-None-Match"], m_requestHashcode);
                            this.LogDebug(context.Request.Path);
                            if (context.Request.Headers["If-None-Match"] == m_requestHashcode) {
                                object lastModified;
                                this.LogDebug("They matched.");
                                this.LogDebug("Use memcached: {0}, KeyExists: {1}, XmlSource Changed: {2}", m_USE_MEMCACHED, m_memcachedClient.TryGet(m_lastModifiedKey, out lastModified), hasXmlSourceChanged);
                                this.LogDebug("Last Modified Key Value: {0}", m_lastModifiedKey);
                                if (m_USE_MEMCACHED && m_memcachedClient.TryGet(m_lastModifiedKey, out lastModified) && !hasXmlSourceChanged)
                                {
                                    m_lastModifiedDate = (string)m_memcachedClient.Get(m_lastModifiedKey);
                                    this.LogDebug("Last Modified Date: {0}", m_lastModifiedDate);
                                    if (context.Request.Headers["If-Modified-Since"] == m_lastModifiedDate) {

                                        context.Response.StatusCode = 304;
                                        m_returnOutput = false;
                                        goto CompleteCall;
                                    } else {
                                        goto Process;
                                    }
                                } else if (m_CONTENT_IS_MEMCACHED) {
                                    goto CompleteCall;
                                } else {
                                    goto Process;
                                }
                            } else {
                                this.LogDebug("Headers do not match.  Beginning transformation process...");
                                m_returnOutput = true;
                                goto Process;
                            }
                        }
                    case "PUT": {
                            goto CompleteCall;
                        }
                    case "DELETE": {
                            goto CompleteCall;
                        }
                    default: {
                            goto CompleteCall;
                        }
                }

            } catch (Exception ex) {
                m_exception = ex;
                WriteError();
                goto CompleteCall;
            }
        Process:
            try {
                this.LogDebug("Processing Transformation");
                this.LogDebug("Request XML ETag Value: {0}, Request URI: {1}", m_context.RequestXmlETag, requestUri);

                XPathNavigator navigator = m_xmlServiceOperationManager.GetXPathNavigator(m_context.RequestXmlETag, requestUri);
                //if (initialReader == null) {
                //    initialReader = reader;
                //} else {
                //    this.LogDebug("XmlReaders are the same object: {0}", initialReader.Equals(reader));
                //}
                //this.LogDebug("XML Reader Value: {0}", reader.ReadOuterXml());
                //this.LogDebug("XML Reader Hash: {0}", reader.GetHashCode());
                XPathServiceOperationNavigator serviceOperationReader = new XPathServiceOperationNavigator(context, m_context, m_transformContext, navigator, m_request, m_response, m_xslTransformationManager);
                m_response = serviceOperationReader.Process();

            } catch (Exception e) {
                this.LogDebug("Error: {0} in transformation.", e.Message);
                m_exception = e;
                WriteError();
            }

            goto CompleteCall;

        CompleteCall:
            this.LogDebug("CompleteCall reached");
            if (m_lastModifiedDate == String.Empty) {
                m_lastModifiedDate = DateTime.UtcNow.ToString("r");
            }
            context.Response.AppendHeader("Cache-Control", "max-age=86400");
            context.Response.AddHeader("Last-Modified", m_lastModifiedDate);
            context.Response.AddHeader("ETag", String.Format("\"{0}\"", m_requestHashcode));
            m_nuxleusAsyncResult.CompleteCall();
            return m_nuxleusAsyncResult;
        }
 public NuxleusTransformAsyncResult (AsyncCallback cb, TransformResponse transformResponse) {
     this.m_cb = cb;
     m_transformResponse = transformResponse;
     m_isCompleted = false;
 }
        public TransformResponse Process () {

            XmlReader reader = m_navigator.ReadSubtree();

            string xmlStylesheetHref = String.Empty;
            bool processWithEmbeddedPIStylsheet = false;

            do {
                switch (reader.NodeType) {
                    case XmlNodeType.ProcessingInstruction:
                        switch (reader.Name) {
                            case "xml-stylesheet":
                                string piValue = reader.Value;
                                if (piValue.Contains("type=\"text/xsl\"") && piValue.Contains("href=")) {
                                    processWithEmbeddedPIStylsheet = true;
                                    xmlStylesheetHref = piValue.SubstringAfter("href=\"").SubstringBefore("\"");
                                }
                                Console.WriteLine("Stylesheet Href = {0}", xmlStylesheetHref);
                                break;
                            default:
                                break;
                        }
                        break;
                    case XmlNodeType.Element:
                        if (reader.IsStartElement()) {
                            switch (reader.Name) {
                                case "service:compile":
                                    if (reader.HasAttributes) {
                                        bool USEMEMCACHE = false;
                                        bool SUCCESS = bool.TryParse(reader.GetAttribute("cache-result"), out USEMEMCACHE);
                                        Console.WriteLine(String.Format("Operation succeeded: {0}, Use memcached: {1}", SUCCESS, USEMEMCACHE));
                                    }
                                    break;
                                case "service:session":
                                    Console.WriteLine("service:session reached");
                                    break;
                                case "service:operation":
                                case "my:page":
                                    Console.WriteLine("service:operation or my:page reached");
                                    Uri baseXsltUri = new Uri(m_httpContext.Request.MapPath(xmlStylesheetHref));
                                    m_xslTransformationManager.BaseXsltUri = baseXsltUri;
                                    string baseXslt = baseXsltUri.GetHashCode().ToString();

                                    if (!m_xslTransformationManager.NamedXsltHashtable.ContainsKey(baseXslt)) {
                                        m_xslTransformationManager.AddTransformer(baseXslt, baseXsltUri);
                                        m_xslTransformationManager.NamedXsltHashtable.Add(baseXslt, baseXsltUri);
                                    }
                                    if (!m_xslTransformationManager.XmlSourceHashtable.ContainsKey(m_context.RequestXmlETag)) {
                                        Console.WriteLine(reader.ReadOuterXml());
                                        using (MemoryStream stream = new MemoryStream(m_encoding.GetBytes(reader.ReadOuterXml().ToCharArray()))) {
                                            m_xslTransformationManager.AddXmlSource(m_context.RequestXmlETag.ToString(), (Stream)stream);
                                        }
                                    }

                                    TransformContext transformContext = new TransformContext();
                                    transformContext.Context = m_transformContext;
                                    transformContext.HttpContext = m_httpContext;
                                    transformContext.XsltTransformationManager = m_xslTransformationManager;
                                    transformContext.XsltName = baseXslt;
                                    transformContext.Response = m_response;
                                    m_request.TransformContext = transformContext;
                                    m_response = m_transform.BeginTransformProcess(m_request);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case XmlNodeType.EndElement:

                        continue;
                    case XmlNodeType.Text:
                    case XmlNodeType.SignificantWhitespace:
                    case XmlNodeType.Whitespace:

                        break;
                    case XmlNodeType.CDATA:

                        break;
                    case XmlNodeType.Comment:
                        break;
                    case XmlNodeType.DocumentType:
                        break;
                    case XmlNodeType.EntityReference:
                        reader.ResolveEntity();
                        continue;

                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.EndEntity:
                        continue;
                    default:
                        break;
                    //throw new InvalidOperationException();

                }
            } while (reader.Read());

            return m_response;
        }