Example #1
0
        public static Header Read(StandardBusinessDocumentHeader sbdh) // throws SbdhException
        {
            Header header = Header.NewInstance();

            // Sender
            PartnerIdentification senderIdentifier = sbdh.Sender[0].Identifier;

            header = header.SetSender(
                ParticipantIdentifier.Of(senderIdentifier.Value, Scheme.Of(senderIdentifier.Authority)));

            // Receiver
            PartnerIdentification receiverIdentifier = sbdh.Receiver[0].Identifier;

            header = header.SetReceiver(
                ParticipantIdentifier.Of(receiverIdentifier.Value, Scheme.Of(receiverIdentifier.Authority)));

            // Identifier
            header = header.SetIdentifier(InstanceIdentifier.Of(sbdh.DocumentIdentification.InstanceIdentifier));

            // InstanceType
            header = header.SetInstanceType(
                InstanceType.Of(
                    sbdh.DocumentIdentification.Standard,
                    sbdh.DocumentIdentification.Type,
                    sbdh.DocumentIdentification.TypeVersion));

            // CreationTimestamp
            if (sbdh.DocumentIdentification.CreationDateAndTime == null)
            {
                throw new SbdhException("Element 'CreationDateAndTime' is not set or contains invalid value.");
            }

            header = header.SetCreationTimestamp(sbdh.DocumentIdentification.CreationDateAndTime);

            // Scope
            foreach (Scope scope in sbdh.BusinessScope)
            {
                if (scope.Type.Equals("DOCUMENTID"))
                {
                    Scheme scheme = scope.Identifier != null
                                        ? Scheme.Of(scope.Identifier)
                                        : DocumentTypeIdentifier.DefaultScheme;

                    header = header.SetDocumentType(DocumentTypeIdentifier.Of(scope.InstanceIdentifier, scheme));
                }
                else if (scope.Type.Equals("PROCESSID"))
                {
                    Scheme scheme = scope.Identifier != null
                                        ? Scheme.Of(scope.Identifier)
                                        : ProcessIdentifier.DefaultScheme;

                    header = header.SetProcess(ProcessIdentifier.Of(scope.InstanceIdentifier, scheme));
                }
            }

            return(header);
        }
Example #2
0
        public IPotentiallySigned <ServiceMetadata> ParseServiceMetadata(FetcherResponse fetcherResponse)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(fetcherResponse.InputStream);
                var o = ClassFactory.FromXmlStream(fetcherResponse.InputStream);

                X509Certificate signer = null;
                if (o is SignedServiceMetadata)
                {
                    signer = XmldsigVerifier.Verify(doc);
                }

                ServiceInformationType serviceInformation = ((tns.ServiceMetadata)o).ServiceInformation;

                List <ProcessMetadata <Endpoint> > processMetadatas = new List <ProcessMetadata <Endpoint> >();
                foreach (ProcessType processType in serviceInformation.ProcessList.Process)
                {
                    List <Endpoint> endpoints = new List <Endpoint>();
                    foreach (EndpointType endpointType in processType.ServiceEndpointList.Endpoint)
                    {
                        endpoints.Add(
                            Endpoint.Of(
                                TransportProfile.Of(endpointType.TransportProfile),
                                new Uri(endpointType.EndpointURI),
                                this.CertificateInstance(endpointType.Certificate.Data)));
                    }

                    processMetadatas.Add(
                        ProcessMetadata <Endpoint> .Of(
                            ProcessIdentifier.Of(
                                processType.ProcessIdentifier.PrimitiveValue,
                                Scheme.Of(processType.ProcessIdentifier.Scheme)),
                            endpoints));
                }

                return(Signed <ServiceMetadata> .Of(
                           ServiceMetadata.Of(

                               ParticipantIdentifier.Of(
                                   serviceInformation.ParticipantIdentifier.PrimitiveValue,
                                   Scheme.Of(serviceInformation.ParticipantIdentifier.Scheme)),

                               DocumentTypeIdentifier.Of(
                                   serviceInformation.DocumentIdentifier.PrimitiveValue,
                                   Scheme.Of(serviceInformation.DocumentIdentifier.Scheme)),

                               processMetadatas),
                           signer));
            }
            catch (Exception e) when(e is CertificateException | e is IOException)
            {
                throw new Exception(e.Message, e);
            }
        }
Example #3
0
        public IPotentiallySigned <ServiceMetadata> ParseServiceMetadata(FetcherResponse fetcherResponse)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.PreserveWhitespace = true;
                doc.Load(fetcherResponse.InputStream);
                var o = ClassFactory.FromXmlElement(doc.DocumentElement);

                Xml.Busdox.tns.ServiceMetadata serviceMetadata = o as Xml.Busdox.tns.ServiceMetadata;
                X509Certificate signer = null;
                if (o is SignedServiceMetadata)
                {
                    signer          = XmldsigVerifier.Verify(doc);
                    serviceMetadata = ((SignedServiceMetadata)o).ServiceMetadata;
                }

                if (serviceMetadata == null)
                {
                    throw new LookupException("ServiceMetadata element not found");
                }


                ServiceInformationType serviceInformation = serviceMetadata.ServiceInformation;

                List <ProcessMetadata <Endpoint> > processMetadatas = new List <ProcessMetadata <Endpoint> >();
                foreach (ProcessType processType in serviceInformation.ProcessList.Process)
                {
                    List <Endpoint> endpoints = new List <Endpoint>();
                    foreach (EndpointType endpointType in processType.ServiceEndpointList.Endpoint)
                    {
                        var certificate = this.CertificateInstance(Convert.FromBase64String(endpointType.Certificate));
                        var endpointUri = new Uri(endpointType.EndpointReference.Address.PrimitiveValue);
                        var profile     = TransportProfile.Of(endpointType.TransportProfile);
                        endpoints.Add(Endpoint.Of(profile, endpointUri, certificate));
                    }

                    processMetadatas.Add(
                        ProcessMetadata <Endpoint> .Of(
                            ProcessIdentifier.Of(
                                processType.ProcessIdentifier.PrimitiveValue,
                                Scheme.Of(processType.ProcessIdentifier.Scheme)),
                            endpoints));
                }

                return(Signed <ServiceMetadata> .Of(
                           ServiceMetadata.Of(

                               ParticipantIdentifier.Of(
                                   serviceInformation.ParticipantIdentifier.PrimitiveValue,
                                   Scheme.Of(serviceInformation.ParticipantIdentifier.Scheme)),

                               DocumentTypeIdentifier.Of(
                                   serviceInformation.DocumentIdentifier.PrimitiveValue,
                                   Scheme.Of(serviceInformation.DocumentIdentifier.Scheme)),

                               processMetadatas),
                           signer));
            }
            catch (Exception e) when(e is CertificateException | e is IOException)
            {
                throw new Exception(e.Message, e);
            }
        }
Example #4
0
        public ProcessIdentifier FetchProcessTypeId()
        {
            String value = this.RetriveValueForXpath("//cbc:ProfileID");

            return(ProcessIdentifier.Of(value));
        }
Example #5
0
        private static void RunInScope(ILifetimeScope scope, Options options)
        {
            // bootstraps the outbound module
            HyperwayOutboundComponent hyperwayOutboundComponent = scope.Resolve <HyperwayOutboundComponent>();
            TransmissionParameters    parameters = new TransmissionParameters(hyperwayOutboundComponent);

            // Verifies the existence of a directory in which transmission evidence is stored.
            DirectoryInfo evidencePath = options.Evidence == null ? null : new DirectoryInfo(options.Evidence);

            if (evidencePath == null)
            {
                evidencePath = new DirectoryInfo("."); // Default is current directory
            }

            if (!evidencePath.Exists)
            {
                PrintErrorMessage(evidencePath + " does not exist or is not a directory");
            }

            parameters.EvidencePath = evidencePath;

            // --- Use Factory
            parameters.UseFactory = options.UseRequestFactory;

            // --- Recipient
            String recipientId = options.Recipient;

            if (recipientId != null)
            {
                parameters.Receiver = ParticipantIdentifier.Of(recipientId);
            }

            // --- Sender
            String senderId = options.Sender;

            if (senderId != null)
            {
                parameters.Sender = ParticipantIdentifier.Of(senderId);
            }

            // --- Document type
            var docType = options.DocumentType;

            if (docType != null)
            {
                String value = options.DocumentType;
                parameters.DocType = DocumentTypeIdentifier.Of(value);
            }

            // --- Process type
            var profileType = options.ProfileType;

            if (profileType != null)
            {
                String value = options.ProfileType; // profileType.value(optionSet);
                parameters.ProcessIdentifier = ProcessIdentifier.Of(value);
            }

            // --- Probe
            if (options.Probe)
            {
                WebClient c      = new WebClient();
                var       url    = options.DestinationUrl;
                var       output = c.DownloadData(url);
                Console.WriteLine(System.Text.Encoding.UTF8.GetString(output));
            }
            else
            {
                // --- Destination URL, protocl and system identifier
                if (!string.IsNullOrEmpty(options.DestinationUrl) && !options.Probe)
                {
                    String destinationString = options.DestinationUrl;

                    X509Certificate certificate;
                    using (Stream inputStream = File.Open(options.DestinationCertificate, FileMode.Open, FileAccess.Read)) {
                        certificate = Validator.GetCertificate(inputStream);
                    }

                    parameters.Endpoint = Endpoint.Of(
                        TransportProfile.AS2_1_0,
                        new Uri(destinationString),
                        certificate);
                }

                // Retrieves the name of the file to be transmitted
                IEnumerable <string> payloadFileSpec = options.InputFiles;

                List <FileInfo> files = LocateFiles(payloadFileSpec);

                Console.WriteLine("");
                Console.WriteLine("");

                int maxThreads = options.ThreadCount;
                Log.Info("Using " + maxThreads + " threads");

                int repeats = options.RepeatCount;
                int maximumTransmissions = options.MaxTransmissions;

                // ExecutorService exec = Executors.newFixedThreadPool(maxThreads);
                // ExecutorCompletionService<TransmissionResult> ecs = new ExecutorCompletionService<>(exec);

                // long start = DateTime.Now;
                int submittedTaskCount = 0;
                foreach (FileInfo file in files)
                {
                    //if (!file.isFile() || !file.canRead()) {
                    //    log.error("File " + file + " is not a file or can not be read, skipping...");
                    //    continue;
                    //}

                    for (int i = 0; i < repeats; i++)
                    {
                        TransmissionTask transmissionTask = new TransmissionTask(parameters, file);
                        transmissionTask.Call();
                        //Future<TransmissionResult> submit = ecs.submit(transmissionTask);
                        //submittedTaskCount++;
                        //if (submittedTaskCount > maximumTransmissions) {
                        //    log.Info("Stopped submitting tasks at {} " + submittedTaskCount);
                        //    break;
                        //}
                    }

                    if (submittedTaskCount > maximumTransmissions)
                    {
                        break;
                    }
                }

                // Wait for the results to be available
                //List<TransmissionResult> results = new List<TransmissionResult>();
                //int failed = 0;
                //for (int i = 0; i<submittedTaskCount; i++) {
                //    try {
                //        Future<TransmissionResult> future = ecs.take();
                //        TransmissionResult transmissionResult = future.get();
                //        results.add(transmissionResult);
                //    } catch (InterruptedException e) {
                //        System.err.println(e.getMessage());
                //    } catch (ExecutionException e) {
                //        log.error("Execution failed: {}", e.getMessage(), e);
                //        failed++;
                //    }
                //}

                //long elapsed = System.nanoTime() - start;
                //exec.shutdownNow(); // Shuts down the executor service

                //foreach (TransmissionResult transmissionResult in results) {
                //    TransmissionIdentifier transmissionIdentifier = transmissionResult.getTransmissionIdentifier();
                //    System.out.println(transmissionIdentifier + " transmission took " + transmissionResult.getDuration() + "ms");
                //}


                //OptionalDouble average = results.stream().mapToLong(TransmissionResult::getDuration).average();

                //if (average.isPresent()) {
                //    System.out.println("Average transmission time was " + average.getAsDouble() + "ms");
                //}
                //long elapsedInSeconds = TimeUnit.SECONDS.convert(elapsed, TimeUnit.NANOSECONDS);
                //Console.WriteLine("Total time spent: " + elapsedInSeconds + "s");
                //Console.WriteLine("Attempted to send " + results.size() + " files");
                //Console.WriteLine("Failed transmissions: " + failed);
                //if (results.size() > 0 && elapsedInSeconds > 0) {
                //    System.out.println("Transmission speed " + results.size() / elapsedInSeconds + " documents per second");
                //}

                //Thread.sleep(2000);
            }
        }