/// <summary> /// Returns a list of matching extended uris templates of a given resource proxy, by matching the regular expression of PID URLs and target URLs. /// </summary> /// <param name="resourceProxy">Resource Proxy to match the extended URI templates to.</param> /// <param name="extendedUris">List of all extended URI templates.</param> /// <returns>All templates matching the current location URL and target URL.></returns> private IList <ExtendedUriTemplateResultDTO> GetMatchedExtendedUriTemplate(ResourceProxyDTO resourceProxy, IList <ExtendedUriTemplateResultDTO> extendedUris) { return(extendedUris.Where(t => { string pidUrlRegex = t.Properties.GetValueOrNull(Common.Constants.ExtendedUriTemplate.HasPidUriSearchRegex, true); string targetUrlRegex = t.Properties.GetValueOrNull(Common.Constants.ExtendedUriTemplate.HasTargetUriMatchRegex, true); if (string.IsNullOrWhiteSpace(pidUrlRegex) || string.IsNullOrWhiteSpace(targetUrlRegex) || string.IsNullOrWhiteSpace(resourceProxy.TargetUrl)) { return false; } return Regex.IsMatch(resourceProxy.PidUrl, pidUrlRegex) && Regex.IsMatch(resourceProxy.TargetUrl, targetUrlRegex); }).ToList()); }
private ResourceProxyDTO ConvertResourceToProxyDto(ResourceRequestDTO resource) { var hasPid = resource.Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.hasPID, true); string pidUri = hasPid != null ? hasPid.Id : ""; var hasBaseUri = resource.Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.BaseUri, true); string baseUri = hasBaseUri != null ? hasBaseUri.Id : null; ResourceProxyDTO resourceProxyDto = new ResourceProxyDTO { PidUrl = pidUri, TargetUrl = null, ResourceVersion = null, NestedProxies = new List <ResourceProxyDTO>() }; // Get distribution if (resource.Properties.ContainsKey(Graph.Metadata.Constants.Resource.Distribution)) { List <dynamic> distList = resource.Properties[Graph.Metadata.Constants.Resource.Distribution]; foreach (dynamic dist in distList) { string distributionPidUri = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties[Graph.Metadata.Constants.Resource.hasPID][0].Id; string distributionNetworkAddress = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.HasNetworkAddress, true); bool isDistributionEndpointDeprecated = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.DistributionEndpointLifecycleStatus, true) == Common.Constants.DistributionEndpoint.LifeCycleStatus.Deprecated; //string distBaseUri = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.HasNetworkAddress, true); resourceProxyDto.NestedProxies.Add( new ResourceProxyDTO { PidUrl = distributionPidUri, TargetUrl = isDistributionEndpointDeprecated ? pidUri : distributionNetworkAddress, ResourceVersion = null, BaseUrl = baseUri }); } } //Get Main distribution string baseUriDistTargetUrl = ""; if (resource.Properties.ContainsKey(Graph.Metadata.Constants.Resource.MainDistribution)) { List <dynamic> distList = resource.Properties[Graph.Metadata.Constants.Resource.MainDistribution]; foreach (dynamic dist in distList) { string mainDistributionPidUri = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties[Graph.Metadata.Constants.Resource.hasPID][0].Id; string mainDistributionNetworkAddress = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.HasNetworkAddress, true); bool isDistributionEndpointDeprecated = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.DistributionEndpointLifecycleStatus, true) == Common.Constants.DistributionEndpoint.LifeCycleStatus.Deprecated; //string distBaseUri = ((COLID.Graph.TripleStore.DataModels.Base.EntityBase)dist).Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.DistributionEndpoints.HasNetworkAddress, true); baseUriDistTargetUrl = isDistributionEndpointDeprecated ? pidUri : mainDistributionNetworkAddress; resourceProxyDto.NestedProxies.Add( new ResourceProxyDTO { PidUrl = mainDistributionPidUri, TargetUrl = baseUriDistTargetUrl, ResourceVersion = null, BaseUrl = baseUri }); } } // Proxy for base URI if (!string.IsNullOrWhiteSpace(baseUri)) { string resourceVersion = resource.Properties.GetValueOrNull(Graph.Metadata.Constants.Resource.HasVersion, true); // distribution target uri is null -> base uri have to redirect to resourcePidUri if (!string.IsNullOrWhiteSpace(baseUriDistTargetUrl)) { if (Uri.TryCreate(baseUriDistTargetUrl, UriKind.Absolute, out _)) { resourceProxyDto.NestedProxies.Add(new ResourceProxyDTO { PidUrl = baseUri, TargetUrl = string.IsNullOrWhiteSpace(baseUriDistTargetUrl) ? pidUri : baseUriDistTargetUrl, ResourceVersion = resourceVersion, BaseUrl = baseUri }); } else { resourceProxyDto.NestedProxies.Add(new ResourceProxyDTO { PidUrl = baseUri, TargetUrl = pidUri, ResourceVersion = resourceVersion, BaseUrl = baseUri }); } } else { resourceProxyDto.NestedProxies.Add(new ResourceProxyDTO { PidUrl = baseUri, TargetUrl = pidUri, ResourceVersion = resourceVersion }); } } return(resourceProxyDto); }
/// <summary> /// Creates NGINX specific configuration sections for the given resource proxy and its matching extended URI templates. /// </summary> /// <param name="resourceProxy">Resource Proxy to match the extended URI templates to.</param> /// <param name="extendedUris">List of all extended URI templates.</param> /// <returns>List of NGINX configuration sections matching to the regular expressions of the extended URI templates.</returns> private IList <NginxConfigSection> CreateNginxConfigSectionForExtendedUriTemplates(ResourceProxyDTO resourceProxy, IList <ExtendedUriTemplateResultDTO> extendedUris) { var nginxConfigSections = new List <NginxConfigSection>(); foreach (var extendedUri in extendedUris) { try { string replacementString = extendedUri.Properties.GetValueOrNull(COLID.Graph.Metadata.Constants.ExtendedUriTemplate.HasReplacementString, true); string pidUriSearchRegex = extendedUri.Properties.GetValueOrNull(COLID.Graph.Metadata.Constants.ExtendedUriTemplate.HasPidUriSearchRegex, true); replacementString = replacementString .Replace("{targetUri}", resourceProxy.TargetUrl, StringComparison.Ordinal) .Replace("{encodedPidUri}", TransformAndEncodePidUrl(resourceProxy.PidUrl, extendedUri), StringComparison.Ordinal) .Replace("{base64PidUri}", TransformAndBase64EncodePidUrl(resourceProxy.PidUrl, extendedUri), StringComparison.Ordinal) .Replace("{base64BaseUri}", resourceProxy.BaseUrl == null ? string.Empty : TransformAndBase64EncodePidUrl(resourceProxy.BaseUrl, extendedUri), StringComparison.Ordinal) .Replace(" ", string.Empty, StringComparison.Ordinal); var parameters = GenerateNginxRegexLocation(pidUriSearchRegex, resourceProxy.PidUrl); if (!string.IsNullOrWhiteSpace(parameters)) { var nginxAttributes = new List <NginxAttribute>(); var nginxAttr = new NginxAttribute($"rewrite {parameters}", replacementString); var hasBaseUriReplacement = extendedUri.Properties.GetValueOrNull(COLID.Graph.Metadata.Constants.ExtendedUriTemplate.HasReplacementString, true); if (!string.IsNullOrWhiteSpace(resourceProxy.BaseUrl) && resourceProxy.PidUrl == resourceProxy.BaseUrl & hasBaseUriReplacement.Contains("{base64BaseUri}")) { var base64Attributes = new List <NginxAttribute> { new NginxAttribute("set $extendeduri", $"{resourceProxy.BaseUrl}$1"), new NginxAttribute("set_encode_base64", "$accurid $extendeduri"), }; nginxAttr = new NginxAttribute($"rewrite {parameters}", $"{resourceProxy.TargetUrl}$accurid"); nginxAttributes.AddRange(base64Attributes); } nginxAttributes.Add(nginxAttr); nginxConfigSections.Add(new NginxConfigSection { Name = "location", Parameters = $"~ {parameters}", Order = extendedUri.Properties.GetValueOrNull(COLID.Graph.Metadata.Constants.ExtendedUriTemplate.HasOrder, true), Attributes = nginxAttributes, }); } } catch (System.Exception exception) { _logger.LogError(exception, Messages.Proxy.ExtendedUri, extendedUri, resourceProxy); } } return(nginxConfigSections); }
/// <summary> /// Creates NGINX specific configuration sections with locations and rewrite attributes. /// Additionally, appends regex locations for all matching extended PID uri templates. /// </summary> /// <param name="resourceProxy">List of resources to generate the NGINX proxy configuration from.</param> /// <param name="extendedUris">List of extended URI templates.</param> /// <param name="nginxConfigSections">List of NGINX configuration sections to append.</param> private void ConvertToNginxConfigSectionsAndAppendTemplates(ResourceProxyDTO resourceProxy, IList <ExtendedUriTemplateResultDTO> extendedUris, IList <NginxConfigSection> nginxConfigSections = null) { if (nginxConfigSections == null) { nginxConfigSections = new List <NginxConfigSection>(); } var urlPath = GetUrlPath(resourceProxy.PidUrl); // If this given PID URL does not start with the PID internal prefix (e.g. an external base URL), this does not need be further processed. if (urlPath != null && !string.IsNullOrWhiteSpace(urlPath)) { var matchedExtendedUris = GetMatchedExtendedUriTemplate(resourceProxy, extendedUris); var pidUri = urlPath; var newParamater = $"= {pidUri}"; var targetUrl = resourceProxy.TargetUrl ?? $"{_dmpFrontEndUrl}/resource-detail?pidUri={WebUtility.UrlEncode(resourceProxy.PidUrl)}"; var nginxAttr = new NginxAttribute("rewrite ^.*", targetUrl); // Base URIs should proxy to the latest version of the version list of a PID entry / resource // If this Base URI is already set in the proxy config list, compare the resource version and // override if to the later version. if (nginxConfigSections.FirstOrDefault(section => section.Parameters == newParamater) == null) { if (Uri.TryCreate(targetUrl, UriKind.Absolute, out _)) { nginxConfigSections.Add(new NginxConfigSection { Name = "location", Parameters = newParamater, Version = resourceProxy.ResourceVersion, Attributes = new List <NginxAttribute> { nginxAttr } }); nginxConfigSections.AddRange(CreateNginxConfigSectionForExtendedUriTemplates(resourceProxy, matchedExtendedUris)); } } else { var config = nginxConfigSections.FirstOrDefault(section => section.Parameters == newParamater); if (resourceProxy.ResourceVersion != null && config.Version.CompareVersionTo(resourceProxy.ResourceVersion) < 0) { config.Version = resourceProxy.ResourceVersion; config.Attributes = new List <NginxAttribute> { nginxAttr }; if (Uri.TryCreate(targetUrl, UriKind.Absolute, out _)) { nginxConfigSections.AddRange(CreateNginxConfigSectionForExtendedUriTemplates(resourceProxy, matchedExtendedUris)); } } } } if (resourceProxy.NestedProxies != null) { foreach (var rp in resourceProxy.NestedProxies) { try { ConvertToNginxConfigSectionsAndAppendTemplates(rp, extendedUris, nginxConfigSections); } catch (System.Exception exception) { _logger.LogError(exception, Messages.Proxy.NestedProxy, rp); } } } }
/// <summary> /// Creates NGINX specific configuration sections for the given resource proxy and its matching extended URI templates. /// </summary> /// <param name="resourceProxy">Resource Proxy to match the extended URI templates to.</param> /// <param name="extendedUris">List of all extended URI templates.</param> /// <returns>List of NGINX configuration sections matching to the regular expressions of the extended URI templates.</returns> private IList <NginxConfigSection> CreateNginxConfigSectionForExtendedUriTemplates(ResourceProxyDTO resourceProxy, IList <ExtendedUriTemplateResultDTO> extendedUris) { var nginxConfigSections = new List <NginxConfigSection>(); foreach (var extendedUri in extendedUris) { try { string replacementString = extendedUri.Properties.GetValueOrNull(Common.Constants.ExtendedUriTemplate.HasReplacementString, true); string pidUriSearchRegex = extendedUri.Properties.GetValueOrNull(Common.Constants.ExtendedUriTemplate.HasPidUriSearchRegex, true); replacementString = replacementString .Replace("{targetUri}", resourceProxy.TargetUrl, StringComparison.Ordinal) .Replace("{encodedPidUri}", TransformAndEncodePidUrl(resourceProxy.PidUrl, extendedUri), StringComparison.Ordinal) .Replace(" ", string.Empty, StringComparison.Ordinal); var parameters = GenerateNginxRegexLocation(pidUriSearchRegex, resourceProxy.PidUrl); if (!string.IsNullOrWhiteSpace(parameters)) { var nginxAttr = new NginxAttribute($"rewrite {parameters}", replacementString); nginxConfigSections.Add(new NginxConfigSection { Name = "location", Parameters = $"~ {parameters}", Order = extendedUri.Properties.GetValueOrNull(Common.Constants.ExtendedUriTemplate.HasOrder, true), Attributes = new List <NginxAttribute> { nginxAttr } }); } } catch (System.Exception exception) { _logger.LogError(exception, Messages.Proxy.ExtendedUri, extendedUri, resourceProxy); } } return(nginxConfigSections); }