public IPromise<Stream> Upload(UploadCommand upload, bool async) { // Files need to be uploaded, so build the vault request // Compile the headers and AML query into the appropriate content var multiWriter = new MultiPartFormWriter(async, _conn.AmlContext.LocalizationContext); multiWriter.AddFiles(upload); _conn.SetDefaultHeaders(multiWriter.WriteFormField); multiWriter.WriteFormField("SOAPACTION", upload.Action.ToString()); multiWriter.WriteFormField("VAULTID", upload.Vault.Id); multiWriter.WriteFormField("XMLdata", "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:i18n=\"http://www.aras.com/I18N\"><SOAP-ENV:Body><ApplyItem>" + upload.ToNormalizedAml(_conn.AmlContext.LocalizationContext) + "</ApplyItem></SOAP-ENV:Body></SOAP-ENV:Envelope>"); // Transform the vault URL (as necessary) var urlPromise = upload.Vault.Url.IndexOf("$[") < 0 ? Promises.Resolved(upload.Vault.Url) : _conn.Process(new Command("<url>@0</url>", upload.Vault.Url) .WithAction(CommandAction.TransformVaultServerURL), async) .Convert(s => s.AsString()); //GetResult("TransformVaultServerURL", "<url>" + upload.Vault.Url + "</url>", async); return urlPromise.Continue(u => { // Determine the authentication used by the vault if (u != upload.Vault.Url) upload.Vault.Url = u; return CheckAuthentication(upload.Vault, async); }).Continue(a => { // Build the request to perform the upload var hReq = (HttpWebRequest)System.Net.WebRequest.Create(upload.Vault.Url); hReq.AllowWriteStreamBuffering = false; hReq.Timeout = -1; hReq.ReadWriteTimeout = 300000; hReq.SendChunked = true; hReq.Method = "POST"; hReq.KeepAlive = true; hReq.ProtocolVersion = HttpVersion.Version11; hReq.ContentType = multiWriter.ContentType; hReq.ContentLength = multiWriter.GetLength(); hReq.CookieContainer = upload.Vault.Cookies; switch (a) { case AuthenticationSchemes.Negotiate: hReq.PreAuthenticate = true; hReq.UnsafeAuthenticatedConnectionSharing = true; break; case AuthenticationSchemes.Ntlm: hReq.UnsafeAuthenticatedConnectionSharing = true; break; case AuthenticationSchemes.Basic: hReq.PreAuthenticate = true; break; } var req = new WebRequest(hReq, _conn.Compression); foreach (var ac in _conn.DefaultSettings) { ac.Invoke(req); } if (upload.Settings != null) upload.Settings.Invoke(req); //hReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2; .NET CLR 1.1.4322)"; req.SetContent(multiWriter.WriteToRequest); return req.Execute(async); }).Convert(r => r.AsStream); }
public IPromise<Stream> Process(Command request, bool async) { var upload = request as UploadCommand; if (upload == null) { if (request.Action == CommandAction.DownloadFile) return DownloadFile(request, async); return Query(_sessionToken, request.Action.ToString(), request.ToNormalizedAml(_factory.LocalizationContext) , this.SessionPolicy, null, async) .Convert(r => r.AsStream); } else if (request.Action == CommandAction.DownloadFile) { throw new ArgumentException("Cannot download a file with an upload request."); } var multiWriter = new MultiPartFormWriter(async, _factory.LocalizationContext); multiWriter.AddFiles(upload); multiWriter.WriteFormField("SOAPACTION", request.Action.ToString()); multiWriter.WriteFormField("XMLdata", "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:i18n=\"http://www.aras.com/I18N\"><SOAP-ENV:Body><ApplyItem>" + upload.ToNormalizedAml(_factory.LocalizationContext) + "</ApplyItem></SOAP-ENV:Body></SOAP-ENV:Envelope>"); return _service.Execute("POST", new Uri(_endpoints.Base, _endpoints.Query.First()).ToString() , null, CredentialCache.DefaultCredentials, async, req => { req.ConfigureForFileUpload(); foreach (var a in _defaults) { a.Invoke(req); } req.Timeout = -1; req.SetHeader("Content-Length", multiWriter.GetLength().ToString()); req.SetHeader("Accept", "text/xml"); if (!string.IsNullOrEmpty(_sessionToken)) req.SetHeader("Authorization", "Bearer " + _sessionToken); req.SetHeader("SOAPACTION", request.Action.ToString()); req.SetContent(multiWriter.WriteToRequest, multiWriter.ContentType); }).Convert(r => r.AsStream); }