Ejemplo n.º 1
0
        public void RequestHandleTest()
        {
            var binding = CreateBasicHttpBinding();

            string[] ids = new string[]
            {
                "http://example.org/test1", "test2", "asdf asdf", "test2", "cb340d03-67a3-47c1-bec5-f25df1d6cd87"
            };

            //string username = Environment.GetEnvironmentVariable("Handle_Username");
            //string password = Environment.GetEnvironmentVariable("Handle_Password");

            bool isNew = true;

            if (isNew)
            {
                var address = new EndpointAddress("https://linktest.its.yale.edu/ypls/webservices");
                var client  = new YaleIsps.HandleService.YalePersistentLinkingService3.PersistentLinkingClient(binding, address);

                string username = "******";
                string password = "******";

                var result = client.createBatch(ids, username, username, password);
            }
            else
            {
                var address = new EndpointAddress("https://link.its.yale.edu/ypls-ws/PersistentLinking");
                var client  = new PersistentLinkingClient(binding, address);

                string username = "******";
                string password = "******";
                var    result   = client.createBatch(ids, username, username, password);
            }

            Console.WriteLine("test");
        }
Ejemplo n.º 2
0
        public CreatePersisitentIdentifiersResult CreatePersistentIdentifiers(CatalogRecord record, ApplicationUser user, ApplicationDbContext db)
        {
            var result = new CreatePersisitentIdentifiersResult();

            result.Successful = true;

            if (string.IsNullOrWhiteSpace(record.Number))
            {
                logger.Info("Skipping Handle request for catalog record without a number: " + record.Title);
                result.Skipped = true;
                return(result);
            }

            var  org   = record.Organization;
            bool isDev = org.HandleServerEndpoint != null && org.HandleServerEndpoint.Contains("linktest");

            if (string.IsNullOrWhiteSpace(org.HandleServerEndpoint))
            {
                result.Skipped = true;
                return(result);
            }

            // Determine which items need handles and make a list of their IDs.

            // Determine the Drupal URL to point to, based on whether this is a test environment or not.
            string drupalHostName = "isps.yale.edu";

            if (isDev)
            {
                drupalHostName = "dev.isps.yale.edu";
            }

            // Handle for the CatalogRecord.
            var handleRequests = new List <HandleRequestInformation>();

            if (string.IsNullOrWhiteSpace(record.PersistentId))
            {
                var req = new HandleRequestInformation
                {
                    Id  = record.Id,
                    Url = $"https://{drupalHostName}/research/data/{record.Number.ToLower()}"
                };
                handleRequests.Add(req);
            }

            // Handle for each ManagedFile.
            foreach (var file in record.Files)
            {
                // Don't request handles for
                //   * Removed files
                //   * Files that already have a persistent identifier
                //   * Non-public access files.
                if (file.Status == FileStatus.Removed ||
                    !string.IsNullOrWhiteSpace(file.PersistentLink) ||
                    !file.IsPublicAccess)
                {
                    continue;
                }

                var req = new HandleRequestInformation
                {
                    Id  = file.Id,
                    Url = $"http://{record.Organization.Hostname}/File/Download/{file.Id}"
                };
                handleRequests.Add(req);
            }

            // Handle for the (to-be-created) DDI file.
            result.DdiFileId = Guid.NewGuid();
            handleRequests.Add(new HandleRequestInformation
            {
                Id  = result.DdiFileId,
                Url = $"http://{record.Organization.Hostname}/File/Download/{result.DdiFileId}"
            });
            string[] valuesToRequest = handleRequests.Select(x => x.Url).ToArray();

            // Request the Handles.
            try
            {
                logger.Debug("Requesting Handles for " + handleRequests.Count + " items");
                logger.Debug("Handle request is for " + string.Join(", ", valuesToRequest));

                var binding = CreateBasicHttpBinding();
                var address = new EndpointAddress(org.HandleServerEndpoint);


                // Try the new service contract, first.
                List <KeyValuePair <string, string> > failMap    = null;
                List <KeyValuePair <string, string> > successMap = null;
                try
                {
                    var client = new YaleIsps.HandleService.YalePersistentLinkingService3.PersistentLinkingClient(binding, address);
                    var map    = client.createBatch(valuesToRequest, org.HandleGroupName, org.HandleUserName, org.HandlePassword);
                    failMap    = map.failMap.Select(x => new KeyValuePair <string, string>(x.key, x.value)).ToList();
                    successMap = map.successMap.Select(x => new KeyValuePair <string, string>(x.key, x.value)).ToList();
                    logger.Debug("First Handle request succeeded");
                }
                catch (Exception ex)
                {
                    // If this failed, try the old service contract.
                    logger.Warn("First Handle request failed. Trying the other service contract.", ex);
                    try
                    {
                        var client = new YaleIsps.HandleService.YalePersistentLinkingService.PersistentLinkingClient(binding, address);
                        var map    = client.createBatch(valuesToRequest, org.HandleGroupName, org.HandleUserName, org.HandlePassword);
                        failMap    = map.failMap.Select(x => new KeyValuePair <string, string>(x.key, x.value)).ToList();
                        successMap = map.successMap.Select(x => new KeyValuePair <string, string>(x.key, x.value)).ToList();
                        logger.Debug("Second Handle request succeeded");
                    }
                    catch (Exception ex2)
                    {
                        logger.Warn("Second Handle request failed. Trying the other service contract.", ex2);
                    }
                }



                // Handle any failures.
                if (failMap.Count > 0)
                {
                    result.Successful = false;
                    foreach (KeyValuePair <string, string> item in failMap)
                    {
                        string msg = $"{item.Key}: {item.Value}";
                        result.Messages.Add(msg);
                        logger.Warn(msg);
                    }
                }

                // Assign the Handles to the CatalogRecord and files.
                foreach (KeyValuePair <string, string> item in successMap)
                {
                    string handle = "http://hdl.handle.net/" + item.Key;
                    string url    = item.Value;

                    logger.Debug("Assigning Handle for " + handle + ", " + url);

                    Guid idForHandle = handleRequests
                                       .Where(x => x.Url == url)
                                       .Select(x => x.Id)
                                       .FirstOrDefault();

                    // Is this Handle for CatalogRecord?
                    if (record.Id == idForHandle)
                    {
                        record.PersistentId = handle;
                        logger.Debug("Assigning handle to record");
                    }
                    else if (result.DdiFileId == idForHandle)
                    {
                        // How about for the DDI file?
                        result.DdiFileHandle = handle;
                        logger.Debug("Assigning handle to DDI file");
                    }
                    else
                    {
                        // Find which file this Handle is for.
                        var file = record.Files.Where(x => x.Id == idForHandle).FirstOrDefault();
                        if (file != null)
                        {
                            file.PersistentLink     = handle;
                            file.PersistentLinkDate = DateTime.UtcNow;
                            logger.Debug("Assigning handle to file: " + file.Name);
                        }
                        else
                        {
                            logger.Warn($"Could not find object to assign Handle to: {handle} -> {url}");
                        }
                    }
                }

                EventService.LogEvent(record, user, db, EventTypes.CreatePersistentIdentifier, "Created Persistent Identifiers");
            }
            catch (Exception ex)
            {
                result.Successful = false;
                result.Messages.Add("Could not connect to handle server");
                logger.Warn("Could not connect to handle server", ex);
            }

            return(result);
        }