In this tutorial we will zoom in on the Atom Protocol's link element and the way it's being used in the RAWS APIs. More specifically, we will look at the different kinds of atom:link elements that may be present inside of an atom:feed or atom:entry element. In our next tutorial, you will learn how to handle these links using the RAWS PHP client libraries.
The atom:link element
According to the atom protocol, the 'atom:link' element defines a reference from an entry or feed to a Web resource. The reference itself is contained inside the "href" attribute. The "rel" attribute is used to give meaning to the reference: it specifies the type of relation that exists between the entry or feed that contains the link and the web resource that is referenced by the link. The "type" attribute contains a hint about the type of the representation that you can expect when following the link. The protocol specifies a few more attributes, but they are currently not being used by RAWS.
For example, a feed or entry that discusses the performance of the search engine at "http://search.example.com" might contain the following element:
<link href="http://search.example.com/" rel="related" type="text/html" />
The link references a web resource located at "http://search.example.com/", which is 'related' to the current feed or entry and will (in all probability) return an HTML page.
The following relation types are currently being used by RAWS: 'self', 'alternate', 'edit', 'enclosure', 'first', 'next' and 'last'.
Self, Alternate and Edit links
These types of relations allow for some introspection regarding the current feed or entry. Below are some examples of how they are being used (taken from the RATS example - get output list on our wiki).
<link href="http://rats.enc01.rambla.be/output/75/" rel="self" type="application/atom+xml"/>
The value 'self' signifies that the IRI in the value of the "href" attribute identifies a resource equivalent to the containing element (= the entry or feed). Basically, it refers to the location where the current entry or feed can be retrieved. In this case, the entry represents a RATS output profile.
<link href="http://rats.enc01.rambla.be/output/75/?alt=html" rel="alternate" type="text/html"/>
The value alternate signifies that the IRI in the value of the "href" attribute identifies an alternate version of the resource described by the containing element. In this case, the output profile in the atom entry can also be rendered as a web page, which can be inferred from the "type" attribute.
<link href="http://rats.enc01.rambla.be/output/75/" rel="edit" type="application/atom+xml"/>
The value edit signifies that the IRI in the value of the "href" attribute identifies a resource that is editable. In most cases, this means that you can change the resource - represented by the entry - by sending a PUT request to the edit link.
Enclosure links
The value enclosure signifies that the IRI in the value of the "href" attribute identifies a related resource that is potentially large in size and might require special handling. In the RAWS APIs, enclosure is used in entries that contain metadata about a file. The type attribute can be used as an indication of the file's mimetype. Depending on the specific service and/or resource, this file may be located on the CDN or may be accessible through a web service.
<link href="http://rats.enc01.rambla.be/transc/media/monty/test.flv/" rel="enclosure" type="video/x-flv"/>
An enclosure link inside an entry which is part of a RATS response always refers to a file that is available from RATS. This means that only the same, authenticated user is able to access the file (= in this case the result of a transcoding operation).
<link href="http://monty.cdn03.rambla.be/tutorial5/snake.mp4" rel="enclosure" type="video/mp4"/>
An enclosure link that is returned by the RASS 'item', 'dir' or 'meta' resource refers to the public location of a file on the CDN.
First, Next and Last links
These types of links are used for pagination; for collections that have too many entries to be returned in a single feed, the atom publishing protocol's defines a mechanism that allows the server to return the entries in consecutive, partial feeds. To have control over the pagination, a client application can check for "atom:link" elements in the feed with a "first", "next" or "last" relation type.
<link href="http://rass.cdn03.rambla.be/dir/tutorial/?kind=file;paginate_by=50;page=2" rel="next"
type="application/atom+xml"/>
If a feed contains an atom:link element with a "next" relation type, this means that the response contains a partial feed and more entries can be retrieved by sending an HTTP GET request to the URL in the "href" attribute. The client may keep doing this as long as the returned feeds contain a "next" link. When the last partial feed is being returned by the server (= all entries have been returned), there will be no more "next" link in the feed.
<link href="http://rass.cdn03.rambla.be/dir/tutorial/?kind=file;paginate_by=50;page=2" rel="last"
type="application/atom+xml"/>
If a feed contains an atom:link element with a "last" relation type, the client can jump to the last (partial) feed by sending an HTTP GET request to the URL in the "href" attribute.
<link href="http://rass.cdn03.rambla.be/dir/tutorial/?kind=file;paginate_by=50;page=1" rel="first"
type="application/atom+xml"/>
If a feed contains an atom:link element with a "first" relation type, the client can jump to the first (partial) feed by sending an HTTP GET request to the URL in the "href" attribute.
Currently, pagination support within RAWS is limited to resources that return large feeds. These include the RASS dir and meta resources and most of the RAMS resources. All other RAWS resources will return their collections in one single feed. For more details about RAWS and pagination, see the dedicated wiki page.