private XDocument CreateIdentify(DateTime date, ArgumentContainer arguments) { OaiVerb verb = OaiVerb.Identify; OaiArgument allowedArguments = OaiArgument.None; if (!OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement)) { return(CreateErrorDocument(date, verb, arguments, errorElement)); } IList <XElement> root = new List <XElement>(); root.Add(CreateRequest(verb, arguments)); XElement content = new XElement(verb.ToString()); root.Add(content); TryAddXElement(content, "repositoryName", _configuration.RepositoryName); TryAddXElement(content, "baseURL", _configuration.BaseUrl()); TryAddXElement(content, "protocolVersion", _configuration.ProtocolVersion); if (_configuration.AdminEmails != null) { foreach (var adminEmail in _configuration.AdminEmails) { TryAddXElement(content, "adminEmail", adminEmail); } } TryAddXElement(content, "earliestDatestamp", _configuration.EarliestDatestamp); TryAddXElement(content, "deletedRecord", _configuration.DeletedRecord); TryAddXElement(content, "granularity", GetDisplayGranularity()); if (_configuration.Compressions != null) { foreach (var compression in _configuration.Compressions) { TryAddXElement(content, "compression", compression); } } if (_configuration.Descriptions != null) { foreach (var description in _configuration.Descriptions) { TryAddXElement(content, "description", description); } } return(CreateXml(date, root.ToArray())); }
public static bool ValidateArguments(ArgumentContainer arguments, OaiArgument allowedArguments, out XElement errorElement) { errorElement = null; if (!allowedArguments.HasFlag(OaiArgument.MetadataPrefix) && !string.IsNullOrWhiteSpace(arguments.MetadataPrefix)) { errorElement = BadArgumentMetadataPrefixNotAllowed; return(false); } if (!allowedArguments.HasFlag(OaiArgument.ResumptionToken) && !string.IsNullOrWhiteSpace(arguments.ResumptionToken)) { errorElement = BadArgumentResumptionTokenNotAllowed; return(false); } if (!allowedArguments.HasFlag(OaiArgument.Identifier) && !string.IsNullOrWhiteSpace(arguments.Identifier)) { errorElement = BadArgumentIdentifierNotAllowed; return(false); } if (!allowedArguments.HasFlag(OaiArgument.From) && !string.IsNullOrWhiteSpace(arguments.From)) { errorElement = BadArgumentFromNotAllowed; return(false); } if (!allowedArguments.HasFlag(OaiArgument.Until) && !string.IsNullOrWhiteSpace(arguments.Until)) { errorElement = BadArgumentUntilNotAllowed; return(false); } if (!allowedArguments.HasFlag(OaiArgument.Set) && !string.IsNullOrWhiteSpace(arguments.Set)) { errorElement = BadArgumentSetNotAllowed; return(false); } return(true); }
private XDocument CreateListMetadataFormats(DateTime date, ArgumentContainer arguments) { OaiVerb verb = OaiVerb.ListMetadataFormats; OaiArgument allowedArguments = OaiArgument.Identifier; if (!OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement)) { return(CreateErrorDocument(date, verb, arguments, errorElement)); } if (!string.IsNullOrWhiteSpace(arguments.Identifier) && _recordRepository.GetRecord(arguments.Identifier, arguments.MetadataPrefix) == null) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.IdDoesNotExist)); } var formats = _metadataFormatRepository.GetMetadataFormats().OrderBy(e => e.Prefix); if (formats.Count() <= 0) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.NoMetadataFormats)); } IList <XElement> root = new List <XElement>(); root.Add(CreateRequest(verb, arguments)); XElement content = new XElement(verb.ToString()); root.Add(content); foreach (var format in formats) { XElement formatElement = new XElement("metadataFormat"); content.Add(formatElement); TryAddXElement(formatElement, "metadataPrefix", format.Prefix); TryAddXElement(formatElement, "schema", format.Schema?.ToString()); TryAddXElement(formatElement, "metadataNamespace", format.Namespace?.ToString()); } return(CreateXml(date, root.ToArray())); }
private XDocument CreateListSets(DateTime date, ArgumentContainer arguments, IResumptionToken resumptionToken = null) { OaiVerb verb = OaiVerb.ListSets; OaiArgument allowedArguments = OaiArgument.ResumptionToken; if (!OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement)) { return(CreateErrorDocument(date, verb, arguments, errorElement)); } // Set if (!_configuration.SupportSets) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.NoSetHierarchy)); } // Decode ResumptionToken if (resumptionToken == null && !string.IsNullOrWhiteSpace(arguments.ResumptionToken)) { if (!OaiErrors.ValidateArguments(arguments, OaiArgument.ResumptionToken)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadArgumentExclusiveResumptionToken)); } IResumptionToken decodedResumptionToken = _resumptionTokenConverter.Decode(arguments.ResumptionToken); if (decodedResumptionToken.ExpirationDate >= DateTime.UtcNow) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadResumptionToken)); } ArgumentContainer resumptionTokenArguments = new ArgumentContainer( verb.ToString(), decodedResumptionToken.MetadataPrefix, arguments.ResumptionToken, null, _dateConverter.Encode(_configuration.Granularity, decodedResumptionToken.From), _dateConverter.Encode(_configuration.Granularity, decodedResumptionToken.Until), decodedResumptionToken.Set); return(CreateListSets(date, resumptionTokenArguments, decodedResumptionToken)); } var setContainer = _setRepository.GetSets(arguments, resumptionToken); IList <XElement> root = new List <XElement>(); root.Add(CreateRequest(verb, arguments)); XElement content = new XElement(verb.ToString()); root.Add(content); if (setContainer != null) { foreach (var set in setContainer.Sets) { XElement setElement = new XElement("set"); content.Add(setElement); TryAddXElement(setElement, "setSpec", set.Spec); TryAddXElement(setElement, "setName", set.Name); TryAddXElement(setElement, "setDescription", set.Description); foreach (var additionalDescription in set.AdditionalDescriptions) { setElement.Add(new XElement("setDescription", additionalDescription)); } } if (setContainer.ResumptionToken != null) { content.Add(_resumptionTokenConverter.ToXElement(setContainer.ResumptionToken)); } } return(CreateXml(date, root.ToArray())); }
private XDocument CreateListIdentifiersOrRecords(DateTime date, ArgumentContainer arguments, OaiVerb verb, IResumptionToken resumptionToken = null) { OaiArgument allowedArguments = OaiArgument.MetadataPrefix | OaiArgument.ResumptionToken | OaiArgument.From | OaiArgument.Until | OaiArgument.Set; if (!OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement)) { return(CreateErrorDocument(date, verb, arguments, errorElement)); } // Set if (!string.IsNullOrWhiteSpace(arguments.Set) && !_configuration.SupportSets) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.NoSetHierarchy)); } // From DateTime fromDate = DateTime.MinValue; if (!string.IsNullOrWhiteSpace(arguments.From) && !_dateConverter.TryDecode(arguments.From, out fromDate)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadFromArgument)); } // Until DateTime untilDate = DateTime.MinValue; if (!string.IsNullOrWhiteSpace(arguments.Until) && !_dateConverter.TryDecode(arguments.Until, out untilDate)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadUntilArgument)); } // The from argument must be less than or equal to the until argument. if (fromDate > untilDate) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadFromUntilCombinationArgument)); } // Decode ResumptionToken if (resumptionToken == null && !string.IsNullOrWhiteSpace(arguments.ResumptionToken)) { if (!OaiErrors.ValidateArguments(arguments, OaiArgument.ResumptionToken)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadArgumentExclusiveResumptionToken)); } try { IResumptionToken decodedResumptionToken = _resumptionTokenConverter.Decode(arguments.ResumptionToken); if (decodedResumptionToken.ExpirationDate >= DateTime.UtcNow) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadResumptionToken)); } ArgumentContainer resumptionTokenArguments = new ArgumentContainer( verb.ToString(), decodedResumptionToken.MetadataPrefix, arguments.ResumptionToken, null, _dateConverter.Encode(_configuration.Granularity, decodedResumptionToken.From), _dateConverter.Encode(_configuration.Granularity, decodedResumptionToken.Until), decodedResumptionToken.Set); return(CreateListIdentifiersOrRecords(date, resumptionTokenArguments, verb, decodedResumptionToken)); } catch (Exception) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadResumptionToken)); } } // Check if required metadata prefix is included in the request if (string.IsNullOrWhiteSpace(arguments.MetadataPrefix)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadMetadataArgument)); } // Check if metadata prefix is supported var metadataPrefix = _metadataFormatRepository.GetMetadataFormat(arguments.MetadataPrefix); if (metadataPrefix == null) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.CannotDisseminateFormat)); } var recordContainer = new RecordContainer(); if (verb == OaiVerb.ListRecords) { recordContainer = _recordRepository.GetRecords(arguments, resumptionToken); } else { recordContainer = _recordRepository.GetIdentifiers(arguments, resumptionToken); } if (recordContainer == null || recordContainer.Records.Count() <= 0) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.NoRecordsMatch)); } IList <XElement> root = new List <XElement>(); root.Add(CreateRequest(verb, arguments)); XElement content = new XElement(verb.ToString()); root.Add(content); foreach (var record in recordContainer.Records) { XElement recordElement = content; if (verb == OaiVerb.ListRecords) { recordElement = new XElement("record"); content.Add(recordElement); } // Header if (record.Header != null) { recordElement.Add(CreateHeaderXElement(record.Header)); } // Metadata if (record.Metadata != null && verb == OaiVerb.ListRecords) { recordElement.Add(CreateMetadataXElement(record.Metadata)); } } if (recordContainer.ResumptionToken != null) { content.Add(_resumptionTokenConverter.ToXElement(recordContainer.ResumptionToken)); } return(CreateXml(date, root.ToArray())); }
private XDocument CreateGetRecord(DateTime date, ArgumentContainer arguments) { OaiVerb verb = OaiVerb.GetRecord; OaiArgument allowedArguments = OaiArgument.Identifier | OaiArgument.MetadataPrefix; if (!OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement)) { return(CreateErrorDocument(date, verb, arguments, errorElement)); } // Check if required identifier is included in the request if (string.IsNullOrWhiteSpace(arguments.Identifier)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadIdentifierArgument)); } // Check if required metadata prefix is included in the request if (string.IsNullOrWhiteSpace(arguments.MetadataPrefix)) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.BadMetadataArgument)); } // Check if metadata prefix is supported var metadataPrefix = _metadataFormatRepository.GetMetadataFormat(arguments.MetadataPrefix); if (metadataPrefix == null) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.CannotDisseminateFormat)); } Record record = _recordRepository.GetRecord(arguments.Identifier, arguments.MetadataPrefix); if (record == null) { return(CreateErrorDocument(date, verb, arguments, OaiErrors.IdDoesNotExist)); } IList <XElement> root = new List <XElement>(); root.Add(CreateRequest(verb, arguments)); XElement content = new XElement(verb.ToString()); root.Add(content); XElement recordElement = new XElement("record"); content.Add(recordElement); // Header if (record.Header != null) { recordElement.Add(CreateHeaderXElement(record.Header)); } // Metadata if (record.Metadata != null) { recordElement.Add(CreateMetadataXElement(record.Metadata)); } return(CreateXml(date, root.ToArray())); }
public void OaiErrors_ValidateArguments(bool expected, string expectedElement, OaiArgument allowedArguments, string metadataPrefix = null, string resumptionToken = null, string identifier = null, string from = null, string until = null, string set = null) { var arguments = new ArgumentContainer(OaiVerb.None.ToString(), metadataPrefix, resumptionToken, identifier, from, until, set); var actual = OaiErrors.ValidateArguments(arguments, allowedArguments, out XElement errorElement); Assert.AreEqual(expected, actual); Assert.AreEqual(expectedElement, errorElement?.ToString()); }
public static bool ValidateArguments(ArgumentContainer arguments, OaiArgument allowedArguments) { return(ValidateArguments(arguments, allowedArguments, out XElement errorElement)); }