Skip to content

Commit

Permalink
Add support for OpenSearch description in AtomRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Mar 20, 2024
1 parent ce472ee commit 7c33ac1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Stars.Services/Model/Atom/AtomRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
using Terradue.Stars.Services.Supplier;
using System.IO;
using System.Threading;
using Terradue.OpenSearch;
using System.Xml.Serialization;
using Terradue.OpenSearch.Schema;

namespace Terradue.Stars.Services.Model.Atom
{
Expand All @@ -22,6 +25,8 @@ public class AtomRouter : IRouter

private static string[] supportedTypes = new string[] { "application/atom+xml", "application/xml", "text/xml" };

private static string opensearchDescriptionType = "application/opensearchdescription+xml";

private readonly IResourceServiceProvider resourceServiceProvider;

public AtomRouter(IResourceServiceProvider resourceServiceProvider)
Expand Down Expand Up @@ -63,6 +68,17 @@ private async Task<IResource> AffineRouteAsync(IResource route, CancellationToke
{
return route;
}
if (route.ContentType.MediaType == opensearchDescriptionType && route is IStreamResource streamResource)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(OpenSearchDescription));
var openSearchDescription = (OpenSearchDescription)xmlSerializer.Deserialize(XmlReader.Create(await streamResource.GetStreamAsync(ct)));
var url = openSearchDescription.Url.FirstOrDefault(u => supportedTypes.Contains(u.Type));
if (url != null)
{
OpenSearchUrl searchUri = OpenSearchFactory.BuildRequestUrlFromTemplate(url, new System.Collections.Specialized.NameValueCollection(), new QuerySettings(url.Type, null));
return await resourceServiceProvider.CreateStreamResourceAsync(new GenericResource(searchUri), ct);
}
}
IResource newRoute = await resourceServiceProvider.CreateStreamResourceAsync(new GenericResource(new Uri(route.Uri.ToString())), ct);
return newRoute;
}
Expand Down Expand Up @@ -105,7 +121,7 @@ public async Task<IStreamResource> FetchResourceAsync(IResource node, Cancellati

public async Task<IResource> RouteLinkAsync(IResource resource, IResourceLink childLink, CancellationToken ct)
{
if (!(resource is AtomFeedCatalog)
if (!(resource is AtomFeedCatalog)
&& !(resource is AtomItemNode))
{
throw new Exception("Cannot route link from non-atom resource");
Expand Down

0 comments on commit 7c33ac1

Please sign in to comment.