public Action <Stream> ProcessCobalt(IWopiFile file, ClaimsPrincipal principal, byte[] newContent) { // Refactoring tip: there are more ways of initializing Atom AtomFromByteArray atomRequest = new AtomFromByteArray(newContent); RequestBatch requestBatch = new RequestBatch(); object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); var cobaltFile = GetCobaltFile(file, principal); cobaltFile.CobaltEndpoint.ExecuteRequestBatch(requestBatch); if (requestBatch.Requests.Any(request => request is PutChangesRequest && request.PartitionId == FilePartitionId.Content)) { using (var stream = file.GetWriteStream()) { new GenericFda(cobaltFile.CobaltEndpoint).GetContentStream().CopyTo(stream); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); Action <Stream> copyToAction = s => { response.CopyTo(s); }; return(copyToAction); }
public Atom ProcessRequest(int accessTokenId, Stream requestStream) { var token = _tokenManager.GetToken(accessTokenId); var atomRequest = new AtomFromStream(requestStream); var requestBatch = new RequestBatch(); try { var cobaltFile = _fileManager.Get(token.FilePath) ?? _fileManager.Create(accessTokenId); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); cobaltFile.CobaltEndpoint.ExecuteRequestBatch(requestBatch); foreach (var request in requestBatch.Requests) { if (request.GetType() == typeof (PutChangesRequest) && request.PartitionId == FilePartitionId.Content && request.CompletedSuccessfully) { using (var saveStream = new MemoryStream()) { CopyStream(cobaltFile, saveStream); _webDavManager.UploadFile(token.FilePath, saveStream.ToArray()); } } } return requestBatch.SerializeOutputToProtocol(protocolVersion); } catch (Exception e) { Log.Error("Cobalt manager Process request", e); throw; } }
public Atom ProcessRequest(int accessTokenId, Stream requestStream) { var token = _tokenManager.GetToken(accessTokenId); var atomRequest = new AtomFromStream(requestStream); var requestBatch = new RequestBatch(); try { var cobaltFile = _fileManager.Get(token.FilePath) ?? _fileManager.Create(accessTokenId); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); cobaltFile.CobaltEndpoint.ExecuteRequestBatch(requestBatch); foreach (var request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content && request.CompletedSuccessfully) { using (var saveStream = new MemoryStream()) { CopyStream(cobaltFile, saveStream); _webDavManager.UploadFile(token.FilePath, saveStream.ToArray()); } } } return(requestBatch.SerializeOutputToProtocol(protocolVersion)); } catch (Exception e) { Server.Utils.Log.WriteError("Cobalt manager Process request", e); throw; } }
public override Action <Stream> SetFileContent(byte[] newContent) { // Refactoring tip: there are more ways of initializing Atom AtomFromByteArray atomRequest = new AtomFromByteArray(newContent); RequestBatch requestBatch = new RequestBatch(); object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); ExecuteRequestBatch(requestBatch); if (requestBatch.Requests.Any(request => request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content)) { Save(); } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); Action <Stream> copyToAction = s => { response.CopyTo(s); }; return(copyToAction); }
private void ProcessRequest(IAsyncResult result) { try { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); try { Console.WriteLine(context.Request.HttpMethod + @" " + context.Request.Url.AbsolutePath); var stringarr = context.Request.Url.AbsolutePath.Split('/'); var access_token = context.Request.QueryString["access_token"]; if (stringarr.Length < 3 || access_token == null) { Console.WriteLine(@"Invalid request"); ErrorResponse(context, @"Invalid request parameter"); m_listener.BeginGetContext(ProcessRequest, m_listener); return; } var filename = stringarr[3]; //use filename as session id just test, recommend use file id and lock id as session id EditSession editSession = CobaltSessionManager.Instance.GetSession(filename); if (editSession == null) { var fileExt = filename.Substring(filename.LastIndexOf('.') + 1); if (fileExt.ToLower().Equals(@"xlsx")) { editSession = new FileSession(filename, m_docsPath + "/" + filename, @"yonggui.yu", @"yuyg", @"*****@*****.**", false); } else { editSession = new CobaltSession(filename, m_docsPath + "/" + filename, @"yonggui.yu", @"yuyg", @"*****@*****.**", false); } CobaltSessionManager.Instance.AddSession(editSession); } if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET")) { //request of checkfileinfo, will be called first var memoryStream = new MemoryStream(); var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, editSession.GetCheckFileInfo()); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.Close(); } else if (stringarr.Length == 5 && stringarr[4].Equals(@"contents")) { // get and put file's content, only for xlsx and pptx if (context.Request.HttpMethod.Equals(@"POST")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); editSession.Save(ms.ToArray()); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; } else { var content = editSession.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; context.Response.OutputStream.Write(content, 0, content.Length); } context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && context.Request.Headers["X-WOPI-Override"].Equals("COBALT")) { //cobalt, for docx and pptx var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray()); RequestBatch requestBatch = new RequestBatch(); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); editSession.ExecuteRequestBatch(requestBatch); foreach (Request request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content) { //upload file to hdfs editSession.Save(); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); context.Response.Headers.Add("X-WOPI-CorellationID", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.Headers.Add("request-id", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = response.Length; response.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && (context.Request.Headers["X-WOPI-Override"].Equals("LOCK") || context.Request.Headers["X-WOPI-Override"].Equals("UNLOCK") || context.Request.Headers["X-WOPI-Override"].Equals("REFRESH_LOCK")) ) { //lock, for xlsx and pptx context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else { Console.WriteLine(@"Invalid request parameters"); ErrorResponse(context, @"Invalid request cobalt parameter"); } } catch (Exception ex) { Console.WriteLine(@"process request exception:" + ex.Message); } m_listener.BeginGetContext(ProcessRequest, m_listener); } catch (Exception ex) { Console.WriteLine(@"get request context:" + ex.Message); return; } }
private void ProcessRequest(IAsyncResult result) { try { Console.WriteLine("start..."); HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); try { //Console.WriteLine("1111..."); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " " + context.Request.HttpMethod + @" " + context.Request.Url.AbsolutePath); var stringarr = context.Request.Url.AbsolutePath.Split('/'); var access_token = context.Request.QueryString["access_token"]; if (stringarr.Length < 3 || access_token == null) { Console.WriteLine(@"Invalid request"); ErrorResponse(context, @"Invalid request parameter"); m_listener.BeginGetContext(ProcessRequest, m_listener); return; } //todo: documentUtil = new DocumentUtil(); //string fileId = stringarr[3]; var docInfo = documentUtil.GetInfo(stringarr[3]); var filename = docInfo.fileName; //Stream gridfsStream = GetFileById(fileId); //StreamToFile(gridfsStream, filename); //use filename as session id just test, recommend use file id and lock id as session id EditSession editSession = CobaltSessionManager.Instance.GetSession(filename); if (editSession == null) { //Console.WriteLine("2222..."); var fileExt = filename.Substring(filename.LastIndexOf('.') + 1); if (fileExt.ToLower().Contains("xlsx")) { editSession = new FileSession(filename, docInfo.filePath, docInfo.author, docInfo.loginUser, docInfo.mail, false); } else { editSession = new CobaltSession(filename, docInfo.filePath, docInfo.author, docInfo.loginUser, docInfo.mail, false); } CobaltSessionManager.Instance.AddSession(editSession); } if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET")) { //Console.WriteLine("4444..."); //request of checkfileinfo, will be called first using (var memoryStream = new MemoryStream()) { var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, editSession.GetCheckFileInfo(access_token)); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.AddHeader("Cache-Control", "no-cache"); context.Response.AddHeader("Pragma", "no-cache"); context.Response.AddHeader("Expires", "-1"); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.OutputStream.Flush(); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } } else if (stringarr.Length == 5 && stringarr[4].Equals(@"contents")) { //Console.WriteLine("5555..."); // get and put file's content, only for xlsx and pptx if (context.Request.HttpMethod.Equals(@"POST")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); editSession.Save(ms.ToArray()); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; } else { var content = editSession.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; context.Response.OutputStream.Write(content, 0, content.Length); } context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && context.Request.Headers["X-WOPI-Override"].Equals("COBALT")) { //Console.WriteLine("6666..."); //cobalt, for docx and pptx var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray()); RequestBatch requestBatch = new RequestBatch(); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); editSession.ExecuteRequestBatch(requestBatch); foreach (Request request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content) { editSession.Save(); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); context.Response.Headers.Add("X-WOPI-CorellationID", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.Headers.Add("request-id", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = response.Length; context.Response.StatusCode = (int)HttpStatusCode.OK; response.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && (context.Request.Headers["X-WOPI-Override"].Equals("LOCK") || context.Request.Headers["X-WOPI-Override"].Equals("UNLOCK") || context.Request.Headers["X-WOPI-Override"].Equals("REFRESH_LOCK")) ) { //Console.WriteLine("7777..."); //lock, for xlsx and pptx context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else { Console.WriteLine(@"Invalid request parameters"); ErrorResponse(context, @"Invalid request cobalt parameter"); } Console.WriteLine("ok..."); } catch (Exception ex) { Console.WriteLine(@"process request exception:" + ex.Message); } m_listener.BeginGetContext(ProcessRequest, m_listener); } catch (Exception ex) { Console.WriteLine(@"get request context:" + ex.Message); return; } }
private void ProcessRequest(IAsyncResult result) { try { Console.WriteLine("start..."); HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); try { Console.WriteLine("1111..."); Console.WriteLine(context.Request.HttpMethod + @" " + context.Request.Url.AbsolutePath); var stringarr = context.Request.Url.AbsolutePath.Split('/'); var access_token = context.Request.QueryString["access_token"]; if (stringarr.Length < 3 || access_token == null) { Console.WriteLine(@"Invalid request"); ErrorResponse(context, @"Invalid request parameter"); m_listener.BeginGetContext(ProcessRequest, m_listener); return; } //todo: //string fileId = stringarr[3]; var filename = stringarr[3]; //Stream gridfsStream = GetFileById(fileId); //StreamToFile(gridfsStream, filename); //use filename as session id just test, recommend use file id and lock id as session id EditSession editSession = CobaltSessionManager.Instance.GetSession(filename); if (editSession == null) { Console.WriteLine("2222..."); var fileExt = filename.Substring(filename.LastIndexOf('.') + 1); if (fileExt.ToLower().Equals(@"xlsx")) editSession = new FileSession(filename, m_docsPath + "/" + filename, @"yonggui.yu", @"yuyg", @"*****@*****.**", false); else editSession = new CobaltSession(filename, m_docsPath + "/" + filename, @"yonggui.yu", @"yuyg", @"*****@*****.**", false); CobaltSessionManager.Instance.AddSession(editSession); } if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET")) { Console.WriteLine("4444..."); //request of checkfileinfo, will be called first var memoryStream = new MemoryStream(); var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, editSession.GetCheckFileInfo()); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.Close(); } else if (stringarr.Length == 5 && stringarr[4].Equals(@"contents")) { Console.WriteLine("5555..."); // get and put file's content, only for xlsx and pptx if (context.Request.HttpMethod.Equals(@"POST")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); editSession.Save(ms.ToArray()); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; } else { var content = editSession.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; context.Response.OutputStream.Write(content, 0, content.Length); } context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && context.Request.Headers["X-WOPI-Override"].Equals("COBALT")) { Console.WriteLine("6666..."); //cobalt, for docx and pptx var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray()); RequestBatch requestBatch = new RequestBatch(); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); editSession.ExecuteRequestBatch(requestBatch); foreach (Request request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content) { editSession.Save(); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); context.Response.Headers.Add("X-WOPI-CorellationID", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.Headers.Add("request-id", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = response.Length; response.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && (context.Request.Headers["X-WOPI-Override"].Equals("LOCK") || context.Request.Headers["X-WOPI-Override"].Equals("UNLOCK") || context.Request.Headers["X-WOPI-Override"].Equals("REFRESH_LOCK")) ) { Console.WriteLine("7777..."); //lock, for xlsx and pptx context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else { Console.WriteLine(@"Invalid request parameters"); ErrorResponse(context, @"Invalid request cobalt parameter"); } Console.WriteLine("ok..."); } catch (Exception ex) { Console.WriteLine(@"process request exception:" + ex.Message); } m_listener.BeginGetContext(ProcessRequest, m_listener); } catch (Exception ex) { Console.WriteLine(@"get request context:" + ex.Message); return; } }
public override Action<Stream> SetFileContent(byte[] newContent) { // Refactoring tip: there are more ways of initializing Atom AtomFromByteArray atomRequest = new AtomFromByteArray(newContent); RequestBatch requestBatch = new RequestBatch(); object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); ExecuteRequestBatch(requestBatch); if (requestBatch.Requests.Any(request => request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content)) { Save(); } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); Action<Stream> copyToAction = s => { response.CopyTo(s); }; return copyToAction; }
private void ProcessRequest(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); var stringarr = context.Request.Url.AbsolutePath.Split('/'); var access_token = context.Request.QueryString["access_token"]; if (stringarr.Length < 3 || access_token == null) { Console.WriteLine(@"Invalid request"); SendError(context); return; } var filename = stringarr[3]; //filename = "test2.docx"; //overrride WopiSession cf = WopiSessionManager.Instance.GetSession(access_token); if (cf == null) { cf = new WopiSession(access_token, _docs + "/" + filename); WopiSessionManager.Instance.AddSession(cf); } if (stringarr.Length == 5 && context.Request.HttpMethod.Equals(@"GET")) { // get file's content var content = cf.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; content.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && context.Request.Headers["X-WOPI-Override"].Equals("COBALT")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray()); RequestBatch requestBatch = new RequestBatch(); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); cf.ExecuteRequestBatch(requestBatch); foreach (Request request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content) { cf.Save(); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); context.Response.Headers.Add("X-WOPI-CorellationID", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.Headers.Add("request-id", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = response.Length; response.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET")) { // encode json var memoryStream = new MemoryStream(); var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, cf.GetCheckFileInfo()); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.Close(); } else { Console.WriteLine(@"Invalid request parameters"); SendError(context); } _listener.BeginGetContext(ProcessRequest, _listener); }
private void ProcessRequest(IAsyncResult result) { try { logger.Info($"监听中......"); var listener = (HttpListener)result.AsyncState; var context = listener.EndGetContext(result); try { var listStr = new List <string>(); foreach (var item in context.Request.Headers.AllKeys) { listStr.Add(context.Request.Headers[item]); } logger.Info($"监听内容:HTTP请求方法为【{context.Request.HttpMethod}】,HTTP请求绝对路径为【{context.Request.Url.AbsolutePath}】,HTTP请求头为【{JsonConvert.SerializeObject(context.Request.Headers.AllKeys)}】值为【{JsonConvert.SerializeObject(listStr)}】"); var stringarr = context.Request.Url.AbsolutePath.Split('/'); var access_token = context.Request.QueryString["access_token"]; if (stringarr.Length < 3 || access_token == null) { logger.Info($"请求参数无效,参数为:{stringarr},token参数为:{access_token}"); ErrorResponse(context, @"无效的请求参数"); m_listener.BeginGetContext(new AsyncCallback(ProcessRequest), m_listener); return; } documentUtil = new DocumentUtil(); //string fileId = stringarr[3]; var docInfo = documentUtil.GetInfo(stringarr[3]); logger.Info($"文件信息为:{JsonConvert.SerializeObject(docInfo)}"); var filename = docInfo.fileName; EditSession editSession = CobaltSessionManager.Instance.GetSession(docInfo.uuid); if (editSession == null) { var fileExt = filename.Substring(filename.LastIndexOf('.') + 1); if (fileExt.ToLower().Contains("xlsx")) { editSession = new FileSession(docInfo.uuid, docInfo.filePath, docInfo.loginUser, docInfo.author, false); } else { editSession = new CobaltSession(docInfo.uuid, docInfo.filePath, docInfo.loginUser, docInfo.author, false); } CobaltSessionManager.Instance.AddSession(editSession); } if (stringarr.Length == 4 && context.Request.HttpMethod.Equals(@"GET")) { using (var memoryStream = new MemoryStream()) { var json = new DataContractJsonSerializer(typeof(WopiCheckFileInfo)); json.WriteObject(memoryStream, editSession.GetCheckFileInfo(access_token)); memoryStream.Flush(); memoryStream.Position = 0; StreamReader streamReader = new StreamReader(memoryStream); var jsonResponse = Encoding.UTF8.GetBytes(streamReader.ReadToEnd()); context.Response.AddHeader("Cache-Control", "no-cache"); context.Response.AddHeader("Pragma", "no-cache"); context.Response.AddHeader("Expires", "-1"); context.Response.ContentType = @"application/json"; context.Response.ContentLength64 = jsonResponse.Length; context.Response.OutputStream.Write(jsonResponse, 0, jsonResponse.Length); context.Response.OutputStream.Flush(); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } } else if (stringarr.Length == 5 && stringarr[4].Equals(@"contents")) { if (context.Request.HttpMethod.Equals(@"POST")) { logger.Info($"进入contents方法,调用了保存。"); var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); editSession.Save(ms.ToArray()); context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; } else { var content = editSession.GetFileContent(); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = content.Length; context.Response.OutputStream.Write(content, 0, content.Length); context.Response.OutputStream.Flush(); } //context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && context.Request.Headers["X-WOPI-Override"].Equals("COBALT")) { var ms = new MemoryStream(); context.Request.InputStream.CopyTo(ms); AtomFromByteArray atomRequest = new AtomFromByteArray(ms.ToArray()); RequestBatch requestBatch = new RequestBatch(); Object ctx; ProtocolVersion protocolVersion; requestBatch.DeserializeInputFromProtocol(atomRequest, out ctx, out protocolVersion); editSession.ExecuteRequestBatch(requestBatch); foreach (Request request in requestBatch.Requests) { if (request.GetType() == typeof(PutChangesRequest) && request.PartitionId == FilePartitionId.Content) { editSession.Save(); editSession.fileinfo = new FileInfo(docInfo.filePath); } } var response = requestBatch.SerializeOutputToProtocol(protocolVersion); context.Response.Headers.Add("X-WOPI-CorellationID", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.Headers.Add("request-id", context.Request.Headers["X-WOPI-CorrelationID"]); context.Response.ContentType = @"application/octet-stream"; context.Response.ContentLength64 = response.Length; context.Response.StatusCode = (int)HttpStatusCode.OK; response.CopyTo(context.Response.OutputStream); context.Response.Close(); } else if (context.Request.HttpMethod.Equals(@"POST") && (context.Request.Headers["X-WOPI-Override"].Equals("LOCK") || context.Request.Headers["X-WOPI-Override"].Equals("UNLOCK") || context.Request.Headers["X-WOPI-Override"].Equals("REFRESH_LOCK")) ) { context.Response.ContentLength64 = 0; context.Response.ContentType = @"text/html"; context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Close(); } else { logger.Info($"请求参数无效,参数为:{stringarr},token参数为:{access_token}"); ErrorResponse(context, @"无效的请求参数"); } logger.Info($"当前请求处理完成..."); } catch (Exception ex) { logger.Error($"请求处理发生异常:{ex.Message}"); } m_listener.BeginGetContext(new AsyncCallback(ProcessRequest), m_listener); } catch (Exception ex) { logger.Error($"获取请求时发生异常:{ex.Message}"); return; } }