Example #1
0
        /// <summary>
        /// Archives a DE Message
        /// </summary>
        /// <param name="de">DE message to archive</param>
        /// <param name="sourceip">Source IP Address of the message</param>
        public bool ArchiveDE(DEv1_0 de, string sourceip)
        {
            bool              wasSuccessful     = false;
            NpgsqlCommand     command           = null;
            NpgsqlTransaction sqlTrans          = null;
            NpgsqlConnection  currentConnection = null;

            try
            {
                currentConnection = GetDatabaseConnection();
                if (OpenConnection(currentConnection) == false)
                {
                    return(wasSuccessful);
                }
                sqlTrans            = currentConnection.BeginTransaction();
                command             = currentConnection.CreateCommand();
                command.Transaction = sqlTrans;

                //add DE first
                //assumes calling function will wrap this in a try catch block
                //allows the error to be thrown to the calling function
                int iHash = DEUtilities.ComputeDELookupID(de);

                string sTable   = QualifiedTableName(TableNames.MessageArchive);
                string sColumns = MessageArchiveColumns.DELookupID + "," + MessageArchiveColumns.DistributionID + "," + MessageArchiveColumns.SenderID + "," +
                                  MessageArchiveColumns.DateTimeSent + "," + MessageArchiveColumns.SenderIP + "," + MessageArchiveColumns.DateTimeLogged + "," + MessageArchiveColumns.DE;
                command.CommandText = "INSERT INTO " + sTable + " (" + sColumns + ") VALUES (@DEHash, @DistributionID, @SenderID, @DateTimeSent, @SourceIP, @DateTimeLogged, @DEv1_0)";
                command.Parameters.Clear();
                AddParameter(command, NpgsqlDbType.Integer, "DEHash", iHash);
                AddParameter(command, NpgsqlDbType.Text, "DistributionID", de.DistributionID);
                AddParameter(command, NpgsqlDbType.Text, "SenderID", de.SenderID);
                AddParameter(command, NpgsqlDbType.TimestampTZ, "DateTimeSent", de.DateTimeSent);
                AddParameter(command, NpgsqlDbType.Text, "SourceIP", sourceip);
                AddParameter(command, NpgsqlDbType.TimestampTZ, "DateTimeLogged", DateTime.UtcNow);
                AddParameter(command, NpgsqlDbType.Xml, "DEv1_0", de.ToString());
                Log.Debug(command.CommandText);
                command.ExecuteNonQuery();
                sqlTrans.Commit();
                wasSuccessful = true;
            }
            catch (Exception Ex)
            {
                Log.Error("General Error in AddedDEToCache()", Ex);
                this.WasRolledBackTransaction(sqlTrans);
            }
            finally
            {
                CloseConnection(currentConnection);
            }

            return(wasSuccessful);
        }
Example #2
0
        public HttpResponseMessage Validate([FromBody] DEv1_0 value)
        {
            List <string> errorList = null;

            try
            {
                Log.Debug("Checking if DE Message is valid");
                string xml = value.ToString();   // Validates DE portion of message and writes to xml

                bool isValid = Fresh.Global.DEUtilities.ValidateNiemSchema(xml, out errorList);

                if (isValid)
                {
                    DEUtilities.LogMessage("The message is valid", DEUtilities.LogLevel.Info);
                    return(Request.CreateResponse(HttpStatusCode.OK, "Message is valid"));
                }
                else
                {
                    DEUtilities.LogMessage("The message was not valid", DEUtilities.LogLevel.Info);
                    string schemaErrorString = "";

                    foreach (string er in errorList)
                    {
                        schemaErrorString = schemaErrorString + er + "\n";
                    }

                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The DE was invalid: " + schemaErrorString));
                }
            }
            catch (IOException Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The schema files could not be read"));
            }
            catch (FormatException Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The schema files could not be parsed"));
            }
            catch (Exception Ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "The message could not be validated"));
            }
        }
Example #3
0
        /// <summary>
        /// Forwards the DE to another webserver.
        /// </summary>
        /// <param name="requesturi">The location to where the DE should be forwarded.</param>
        /// <param name="distributionElement">The DE to be forwarded</param>
        private void DoPost(Uri requesturi, DEv1_0 distributionElement)
        {
            int  numRetries     = 0;
            bool deliveryFailed = false;

            do
            {
                logger.Debug(string.Format("Attempting to federate the message to {0}.  This is the attempt #{1}", requesturi, numRetries + 1));

                if (deliveryFailed)
                {
                    Thread.Sleep(1000 * 5);
                    deliveryFailed = false;
                }

                try
                {
                    HttpWebRequest request;

                    string s = distributionElement.ToString();
                    s       = s.Replace("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"no\"?>\r\n", String.Empty);
                    request = (HttpWebRequest)WebRequest.Create(requesturi);
                    //request.Timeout = 7000;
                    request.KeepAlive         = true;
                    request.Method            = "POST";
                    request.ContentType       = "text/xml";
                    request.AllowAutoRedirect = true;
                    request.ContentLength     = Encoding.UTF8.GetByteCount(s);
                    logger.Debug("The Post requesturi is: " + requesturi.ToString());
                    // HACK HACKITY HACK HACK HACK
                    if (requesturi.ToString() == "https://c2crouter.nics.ll.mit.edu/api/de")
                    {
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                        X509Certificate2 clientCert = new X509Certificate2("C:\\public\\ArdentAWS.pfx", "ArdentAWS");
                        request.ClientCertificates.Add(clientCert);
                        request.PreAuthenticate = true;
                        request.Credentials     = CredentialCache.DefaultCredentials;
                        request.CachePolicy     = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                    }
                    this.SetBody(request, s);

                    HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
                    resp.Close();
                }
                catch (WebException e)
                {
                    deliveryFailed = true;

                    logger.Info("TLS Info: " + System.Net.ServicePointManager.SecurityProtocol.ToString());
                    logger.Error("Error POSTing to " + requesturi + ": " + e.Message);
                    logger.Error("Stacktrace: " + e.StackTrace);
                    logger.Error("Err: " + e.ToString());
                    if (e.InnerException != null)
                    {
                        logger.Error("Inner: " + e.InnerException.Message);
                    }

                    logger.Error("NumRetries: " + numRetries);
                }
            }while (deliveryFailed && (numRetries++ < federationConnectionRetryAttempts));

            // If the message was never able to be delivered successfully, add it to the unreachable URI map
            if (deliveryFailed)
            {
                //TODO: Change this to add hour 1 after testing
                unreachableURI.Add(requesturi.ToString(), DateTime.Now.AddHours(1));
                logger.Error("Failed to federate message to " + requesturi.ToString());
            }
            else
            {
                logger.Debug("Message was federated successfully to " + requesturi.ToString());
            }
        }