A client uses an Accept header to specify a prioritized list of preferred MIME types for the response. When the Web Script Framework receives an HTTP request with an Accept header, it responds with the web script response format that most closely matches the highest-priority MIME type preference.
By default, content negotiation is disabled; however, each web script can enable content negotiation by declaring its requirements in its descriptor document. This involves mapping an incoming Accept header MIME type preference to one of its response formats.
-
Log in to SkyVault Share:
- Open a web browser and enter the URL: http://localhost:8080/share
- If prompted, log in with the user name admin and password admin.
- Click the Repository link in the Share header.
- Navigate to Data Dictionary > Web Scripts Extensions.
-
Create a folder to represent the top-level package structure (skip this step if the
org space already exists):
- In the Create menu, select Create Folder.
- Enter the name for the folder in the Name field: org
- Click Save.
- Navigate to the freshly created org folder.
-
Create a sub-package (skip this step if the example space
already exists):
- In the Create menu, select Create Folder.
- Enter the name for the folder in the Name field: example
- Click Save.
- Navigate to Data Dictionary > Web Scripts Extensions > org > example.
-
Create a web script description document for your content negotiation sample:
- In the Create menu, select XML.
- Enter the name for the web script in the Name field as: negotiate.get.desc.xml
-
Type the following in the content box:
<webscript> <shortname>Negotiation Sample</shortname> <description>Response format driven by content negotiation</description> <url>/negotiate</url> <negotiate accept="text/html">html</negotiate> <negotiate accept="application/json">json</negotiate> </webscript>
- Click Create.
- Navigate back to the org/example folder using the breadcrumb trail.
-
Create an HTML response template for your content negotiation sample:
- In the Create menu, select Plain Text.
- Enter the name in the Name field: negotiate.get.html.ftl
-
Type the following in the content box:
<html> <body>HTML response.</body> </html>
- Click Create.
- Navigate back to the org/example folder
-
Create a JSON response template for your content negotiation sample:
- In the Create menu, select Plain Text.
- Enter the name in the Name field: negotiate.get.json.ftl
-
Type the following in the content box:
{"response": "json"}
- Click Create.
-
Register the web script with SkyVault Content Services.
- Open a new tab in the web browser.
- In the new tab, enter the URL: http://localhost:8080/alfresco/service/index
- If prompted, log in with the user name admin and password admin.
- Click Refresh Web Scripts. (The number of web scripts will increase.)
Content negotiation is declared by listing the mappings between an incoming preferred MIME type and a web script response format. In your sample, the HTML and JSON response formats are mapped to the text/html and application/json MIME types, respectively:
... <negotiate accept="text/html">html</negotiate> <negotiate accept="application/json">json</negotiate> ...
-
Type the following in your command line to test that your sample web script responds
appropriately to content negotiation by explicitly requesting JSON:
curl -H "Accept: application/json" "http://localhost:8080/alfresco/service/negotiate"
The response is:
{"response": "json"}
-
Type the following in your command line to test that the best response format is
chosen:
curl -H "Accept: text/xml,text/*" "http://localhost:8080/alfresco/service/negotiate"
This time the response is:
<html><body>HTML response.</body></html>