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 may 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 Explorer:
- Open a web browser and enter the URL: http://localhost:8080/SkyVault
- If prompted, log in with the user name admin and password admin.
- Navigate to Company Home > 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, click Create Space.
- Enter the name for the folder in the Name field, such as: org
- Click Create Space.
-
Create a sub-package (skip this step if the example space
already exists):
- Navigate to Company Home > Data Dictionary > Web Scripts Extensions > org.
- In the Create menu, click Create Space.
- Enter the name for the folder in the Name field, such as: example
- Navigate to Company Home > Data Dictionary > Web Scripts Extensions > org > example.
-
Create a web script description document for your content negotiation sample:
- In the Create menu, click Create Content.
- Enter the name for the web script in the Name field as: negotiate.get.desc.xml
- In the Content Type list, select XML.
- Click Next.
-
Type the following in the Enter 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 Next, click Finish, and then click OK.
-
Create an HTML response template for your content negotiation sample:
- In the Create menu, click Create Content.
- Enter the name in the Name field, such as: negotiate.get.html.ftl
- In the Content Type list, select Plain Text.
- Click Next.
-
Type the following in the Enter Content box:
<html> <body>HTML response.</body> </html>
- Click Next, click Finish, and then click OK.
-
Create a JSON response template for your content negotiation sample:
- In the Create menu, click Create Content.
- Enter the name in the Name field, such as: negotiate.get.json.ftl
- In the Content Type list, select Plain Text.
- Click Next.
-
Type the following in the Enter Content box:
{"response": "json"}
- Click Next, click Finish, and then click OK.
-
Register the web script with SkyVault.
- In a web browser, 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.
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>