Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PublicNodes.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using System.Web;
6using Waher.Content;
13
15{
17 {
18 public PublicNodes()
19 : base("/PubSub")
20 {
21 }
22
23 public override bool HandlesSubPaths => true;
24 public override bool UserSessions => true;
25 public bool AllowsGET => true;
26
27 public async Task GET(HttpRequest Request, HttpResponse Response)
28 {
29 if (string.IsNullOrEmpty(Request.SubPath))
30 throw new BadRequestException("Sub-path must be specified for this request.");
31
32 int i = Request.SubPath.IndexOf('/', 1);
33 string NodeName = HttpUtility.UrlDecode(i < 0 ? Request.SubPath[1..] : Request.SubPath[1..i]);
34 CaseInsensitiveString ItemId = i < 0 ? string.Empty : HttpUtility.UrlDecode(Request.SubPath[i..]);
35
36 PubSubNode Node = await XmppServerModule.GetPubSubNodeAsync(NodeName)
37 ?? throw new NotFoundException("PubSub node `" + NodeName + "` not found.");
38
39 if (Node.AccessModel != NodeAccessModel.open)
40 throw new ForbiddenException("Node is not configured for open public access.");
41
42 string Xml;
43
45 {
46 StringBuilder sb = new StringBuilder();
47 string Url = Request.Header.GetURL();
48 bool First;
49
50 sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
51 sb.AppendLine("<rss version=\"2.0\">");
52 sb.AppendLine("<channel>");
53 sb.Append("<title>");
54 sb.Append(XML.Encode(string.IsNullOrEmpty(Node.Title) ? NodeName : Node.Title));
55 sb.AppendLine("</title>");
56 sb.Append("<link>");
57 sb.Append(XML.Encode(Url));
58 sb.AppendLine("</link>");
59 sb.Append("<description>");
60
61 if (!(Node.Description is null))
62 {
63 First = true;
64 foreach (string s in Node.Description)
65 {
66 if (First)
67 First = false;
68 else
69 sb.AppendLine();
70
71 sb.Append(s);
72 }
73 }
74
75 sb.AppendLine("</description>");
76
77 if (!string.IsNullOrEmpty(Node.Language))
78 {
79 sb.Append("<language>");
80 sb.Append(XML.Encode(Node.Language));
81 sb.AppendLine("</language>");
82 }
83
84 IEnumerable<PubSubItem> Items = await Database.Find<PubSubItem>(0, 20,
85 new FilterAnd(
87 new FilterFieldEqualTo("Node", NodeName)),
88 "-Service", "-Node", "-Created");
89
90 First = true;
91
92 foreach (PubSubItem Item in Items)
93 {
94 if (First)
95 {
96 First = false;
97 sb.Append("<pubDate>");
98 sb.Append(CommonTypes.EncodeRfc822(Item.Updated == DateTime.MinValue ? Item.Created : Item.Updated));
99 sb.AppendLine("</pubDate>");
100 }
101
102 sb.AppendLine("<item>");
103 sb.Append("<title>");
104 sb.Append(XML.Encode(Item.ItemId));
105 sb.AppendLine("</title>");
106 sb.Append("<author>");
107 sb.Append(Item.Publisher.Value);
108 sb.AppendLine("</author>");
109 sb.Append("<pubDate>");
110 sb.Append(CommonTypes.EncodeRfc822(Item.Updated == DateTime.MinValue ? Item.Created : Item.Updated));
111 sb.AppendLine("</pubDate>");
112 sb.AppendLine("<description>Published item</description>");
113 sb.Append("<link>");
114 sb.Append(XML.Encode(Url));
115 if (!Item.ItemId.StartsWith("/"))
116 sb.Append('/');
117 sb.Append(XML.Encode(Item.ItemId));
118 sb.AppendLine("</link>");
119 sb.Append("<guid isPermaLink=\"false\">");
120 sb.Append(XML.Encode(Item.ObjectId));
121 sb.AppendLine("</guid>");
122 sb.AppendLine("</item>");
123 }
124
125 sb.AppendLine("</channel>");
126 sb.AppendLine("</rss>");
127
128 Xml = sb.ToString();
129 }
130 else
131 {
132 PubSubItem Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, ItemId);
133
134 if (Item is null && ItemId.StartsWith("/"))
135 Item = await XmppServerModule.PubSub.LoadItem(CaseInsensitiveString.Empty, NodeName, ItemId.Substring(1));
136
137 if (Item is null)
138 throw new NotFoundException("PubSub item `" + ItemId + "` on node `" + NodeName + "` not found.");
139
140 Xml = Item.Payload;
141 }
142
143 Response.ContentType = XmlCodec.DefaultContentType;
144 await Response.Write(Xml);
145 }
146 }
147}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static string EncodeRfc822(DateTime Timestamp)
Encodes a date and time, according to RFC 822 §5.
Definition: CommonTypes.cs:669
XML encoder/decoder.
Definition: XmlCodec.cs:19
const string DefaultContentType
Default content type for XML documents.
Definition: XmlCodec.cs:30
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
string GetURL()
Gets an absolute URL for the request.
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
Represents a case-insensitive string.
string Value
String-representation of the case-insensitive string. (Representation is case sensitive....
static readonly CaseInsensitiveString Empty
Empty case-insensitive string
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
bool StartsWith(CaseInsensitiveString value)
Determines whether the beginning of this string instance matches the specified string.
CaseInsensitiveString Substring(int startIndex, int length)
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
CaseInsensitiveString Publisher
Publisher of content.
Definition: PubSubItem.cs:92
override string ToString()
object.ToString
Definition: PubSubItem.cs:153
DateTime Created
When item was created.
Definition: PubSubItem.cs:101
DateTime Updated
When item was updated.
Definition: PubSubItem.cs:111
CaseInsensitiveString ItemId
Item ID.
Definition: PubSubItem.cs:50
Defines a node on which items can be published.
Definition: PubSubNode.cs:19
NodeAccessModel AccessModel
Who may subscribe and retrieve items
Definition: PubSubNode.cs:202
string[] Description
A description of the node
Definition: PubSubNode.cs:492
string Language
Default language of the node
Definition: PubSubNode.cs:502
string Title
A friendly name for the node
Definition: PubSubNode.cs:482
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
Definition: PublicNodes.cs:27
bool AllowsGET
If the GET method is allowed.
Definition: PublicNodes.cs:25
Service Module hosting the XMPP broker and its components.
GET Interface for HTTP resources.
NodeAccessModel
Node access model.
Definition: Enumerations.cs:9