public async Task<UploadStatus> Upload(ActivityType activityType, DataType dataType, System.IO.Stream input, string fileName, string name = null, string description = null,
            bool @private = false, bool commute = false)
        {
            var request = new RestRequest("/api/v3/uploads?data_type={data_type}&activity_type={activity_type}&private={private}&commute={commute}", Method.POST);
            if (name != null)
            {
                request.Resource += "&name={name}";
                request.AddParameter("name", name, ParameterType.UrlSegment);
            }
            if (description != null)
            {
                request.Resource += "&description={description}";
                request.AddParameter("description", description, ParameterType.UrlSegment);
            }

            request.ContentCollectionMode = ContentCollectionMode.MultiPart;
            request.AddParameter("data_type", dataType, ParameterType.UrlSegment);
            request.AddParameter("activity_type", EnumHelper.ToString(activityType), ParameterType.UrlSegment);
            request.AddParameter("private", @private ? 1 : 0, ParameterType.UrlSegment);
            request.AddParameter("commute", commute ? 1 : 0, ParameterType.UrlSegment);
            request.AddFile("file", input, Uri.EscapeDataString(fileName));
            var response = await _client.RestClient.Execute<UploadStatus>(request);
            return response.Data;
        }
Example #2
0
        public RestRequest CreateUploadFileRequest(string path, string filename, Stream fileStream, string root, bool overwrite, string parent_revision)
        {
            var request = new RestRequest("{version}/files/{root}{path}", HttpMethod.Post);
            request.AddParameter("version", _version, ParameterType.UrlSegment);
            request.AddParameter("path", path, ParameterType.UrlSegment);
            request.AddParameter("root", root, ParameterType.UrlSegment);
            //Need to add the "file" parameter with the file name
            // This isn't needed. Dropbox is particular about the ordering,
            // but the oauth sig only needs the filename, which we have in the OTHER parameter
            //request.AddParameter("file", filename);

            request.AddParameter("overwrite", overwrite);
            if (!String.IsNullOrEmpty(parent_revision))
            {
                request.AddParameter("parent_rev", parent_revision);
            }

            request.AddFile("file", fileStream, filename);

            return request;
        }
        /// <summary>
        /// Posts an image to the api.
        /// </summary>
        /// <returns></returns>
        private async Task<int?> AddImageAsync()
        {
            using (var client = new RestClient("http://localhost:55298/api/"))
            {
                var request = new RestRequest("images", Method.POST);
                var data = await ReadFileAsync(this.imageFile);
                request.AddFile(FileParameter.Create("file", data, this.imageFile.Name));

                try
                {
                    var image = await client.Execute<Image>(request);
                    return image.Data.Id;
                }
                catch
                {
                    return null;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Antrag versenden
        /// </summary>
        /// <param name="application">Antrag</param>
        /// <param name="p10Data">PKCS#10-Daten</param>
        /// <param name="certStore">Zertifikat-Speicher für die Bildung der Zertifikatskette und die Abfrage des Empfänger-Zertifikats</param>
        /// <param name="pfx">Zertifikat für die Verschlüsselung - wenn nicht gesetzt, dann wird ein Erstantrag erstellt</param>
        /// <param name="validator">The validator for the OSTC certificate request document</param>
        /// <returns>Das Ergebnis der Antragstellung</returns>
        public async Task<OstcApplicationResult> SendApplicationAsync([NotNull] OstcAntrag application, [NotNull] Pkcs10Data p10Data, [CanBeNull] IOstcCertificateStore certStore, [CanBeNull] Pkcs12Store pfx, [CanBeNull] IValidator validator)
        {
            var senderId = SenderId.FromBnrOrIk(application.Antragsteller.IK_BN);

            var applicationData = OstcUtils.Serialize(application, Iso88591);
            string mimeType;
            if (pfx == null)
            {
                mimeType = "text/xml";
            }
            else
            {
                var alias = pfx.Aliases.Cast<string>().FirstOrDefault(pfx.IsKeyEntry);
                if (alias != null)
                {
                    var certEntry = pfx.GetCertificate(alias);
                    var keyEntry = pfx.GetKey(alias);
                    var certificate = certEntry.Certificate;
                    var key = keyEntry.Key;
                    if (certStore == null)
                        throw new ArgumentNullException(nameof(certStore));
                    var certChain = certStore.GetChain(certificate).ToList();
                    System.Diagnostics.Debug.Assert(certChain[0].SubjectDN.Equivalent(certificate.SubjectDN));
                    certChain.RemoveAt(0);
                    applicationData = OstcUtils.SignData(applicationData, key, certificate, certChain);
                    var receiverCert = certStore.GetCertificate(senderId.CommunicationServerReceiver);
                    applicationData = OstcUtils.EncryptData(applicationData, receiverCert);
                    mimeType = "application/octet-stream";
                }
                else
                {
                    mimeType = "text/xml";
                }
            }

            var date = DateTime.ParseExact(application.Antragsinfo.Datum, "dd.MM.yyyy", CultureDe);
            var p10FileName = $"{senderId.Id}.p10";
            var xmlFileName = $"{senderId}_{date:ddMMyyyy}.xml";

            var requestSendApp = new RestRequest(Network.Requests.Upload);
            requestSendApp.AddParameter("MAX_FILE_SIZE_XML", "4000");
            requestSendApp.AddFile("xml_Datei", applicationData, xmlFileName, mimeType);
            requestSendApp.AddParameter("MAX_FILE_SIZE_P10", "4000");
            requestSendApp.AddFile("p10_Datei", p10Data.CertRequestDer, p10FileName);

            var responseSendApp = await Client.Execute(requestSendApp);
            var responseHtml = DecodeResponse(new MemoryStream(responseSendApp.RawBytes));
            var responseFileUrl = GetResponseFileUrl(responseHtml);
            if (responseFileUrl == null)
                throw new OstcException("Von der ITSG wurde kein Pfad zu einer Rückmeldungs-Datei geliefert.");

            var downloadRequest = new RestRequest(responseFileUrl, Method.GET);
            var downloadResponse = await Client.Execute(downloadRequest);
            var resultData = downloadResponse.RawBytes;

            var resultXml = XDocument.Load(new MemoryStream(resultData));

            var trustCenterNode = resultXml
                .Elements("OSTCAntrag")
                .Elements("Trustcenter")
                .FirstOrDefault();
            if (trustCenterNode == null)
                throw new InvalidOperationException("Die von der ITSG zurückgelieferte Antwort lag in einem unbekannten Format vor.");
            var returnCodeNode = trustCenterNode
                .Elements("Returncode")
                .FirstOrDefault();
            if (returnCodeNode == null)
                throw new InvalidOperationException("Die von der ITSG zurückgelieferte Antwort lag in einem unbekannten Format vor.");
            var returnCode = Convert.ToInt32(returnCodeNode.Value.Trim(), 10);
            var errorCodeNode = trustCenterNode.Elements("Fehlercode").FirstOrDefault();
            if (errorCodeNode == null)
                throw new InvalidOperationException("Die von der ITSG zurückgelieferte Antwort lag in einem unbekannten Format vor.");
            var errorCodes = errorCodeNode.Value.Trim()
                .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => Convert.ToInt32(x.Trim(), 10))
                .ToList();

            var inputNumberNode = trustCenterNode.Elements("Eingangsnummer").FirstOrDefault();
            if (inputNumberNode == null)
                throw new InvalidOperationException("Die von der ITSG zurückgelieferte Antwort lag in einem unbekannten Format vor.");
            var inputNumber = inputNumberNode.Value.Trim();
            var result = new OstcApplicationResult()
            {
                OrderId = inputNumber,
                ReturnCode = returnCode,
                ErrorCodes = errorCodes,
            };

            return result;
        }