<webscript> <shortname>Hello User</shortname> <description>Personalized greeting</description> <url>/hellouser</url> <authentication>user</authentication> <negotiate accept="text/html">html</negotiate> <negotiate accept="application/json">json</negotiate> </webscript>
A message indicates there is one additional web script.
A Hello admin message displays indicating your web script is working.
A web script can offer multiple response formats where each format is supported by its own response template. Clients that invoke the web script either rely on the default response format or can explicitly ask for a specific response format.
Add another response format to the Hello User web script that returns the greeting in JSON format.
{greeting: "hello", user: "${person.properties.userName}"}
A message indicates there is no additional web script (just a response format has been added).
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.json"
The message {greeting: "hello", user: "admin"} displays indicating your web script is working.
Sometimes, a web script URL cannot support the format extension approach as the URL might naturally end with an extension anyway. For example, web script URL paths that refer to folder and file names in the content repository already have the extension inherited from the file name. For these scenarios, it is possible to explicitly select the response format by using the URL query parameter, such as <webscript url>?format=<format>.
Each format actually maps to a MIME type, which is set on the HTTP response allowing a client to process or render the response appropriately. The Web Script Framework provides a registry of formats where the commonly used MIME types are mapped as follows:
Another approach to selecting a response format is to use the HTTP Accept header, as defined by RFC 2616 section 14. A client uses an Accept header to specify a prioritized list of preferred MIME types for the response. When the Web Script Framework accepts an HTTP request with an Accept header, it responds with the response format that most closely matches the highest priority preference.
Web browsers typically provide an Accept header on all their HTTP requests, but most HTTP clients offer some way of specifying an Accept header.
If a client does not explicitly request a specific response format, the web script uses its predefined default response format.
Refer to the following instructions to explicitly select a response format. Each option uses the Hello User sample web script.
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.html"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser?format=json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser?format=html"
curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/hellouser"
By default, the Web Script Framework initiates the login process through HTTP Basic authentication, which informs the web browser to display a login box for the user to enter their user name and password. Upon successful authentication, which is performed by the Alfresco content application server, the web script is invoked. Otherwise, the process stops and the invocation of the web script fails.
Having found the hellouser.get.html.ftl response template, the Web Script Framework renders its result back to the web browser. The template, which is now running as an authenticated user, has access to special Alfresco content application server objects. In this case, the template renders the name of the authenticated user through the object ${person.properties.userName}.
HTTP Basic authentication allows you to specify your user name and password within an HTTP request. A request to a web script can include the user name and password of the Alfresco user to authenticate as, meaning the client does not have to ask for them. The cURL client supports this feature.
You can specify an Alfresco Ticket instead of an explicit user name and password. A ticket represents a pre-authenticated user who has already performed the login process. Tickets can be programmatically established by using the pre-built Login web script.
A final option enables you to specify that a web script be executed as an Alfresco guest. Guests are not named users, so do not need to log in; however, they might be restricted in what they can see or do in the Alfresco content repository.
Refer to the following instructions to specify user identity. Each option uses the Hello User sample web script [10].
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser"
This informs cURL to invoke the URL defined by your Hello User web script as the user admin, which returns Hello admin.
When comparing this to invoking the Hello User web script through the web browser, you can see that the cURL client did not subsequently ask for the user name and password, whereas the web browser did.
curl "http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin"
This informs cURL to invoke the URL defined by the Login web script, which returns XML similar to the following:
<?xml version="1.0" encoding="URF-8"?> <ticket>TICKET_0a748bc2543f2b271dc4cb9955c11a042cad72cd</ticket>
curl "http://localhost:8080/alfresco/service/hellouser?alf_ticket=TICKET_0a748bc2543f2b271dc4cb9955c11a042cad72cd"
Remember, guests can only invoke web scripts that require Guest authentication; they cannot invoke User or Admin required web scripts. To invoke the Hello User web script as guest, type the following in your command line:
curl "http://localhost:8080/alfresco/service/hellouser?guest=true"
You might expect this to respond with a polite greeting, but instead you will receive a 401 error message stating that the Hello User web script requires user authentication and a guest has attempted access.
<bean id="webscripts.container" class="org.alfresco.repo.web.scripts.TenantRepositoryContainer" parent="baseAlfrescoRepositoryContainer" init-method="setup"> <property name="configService" ref="webscripts.config" /> <property name="name"><value>Repository</value></property> <property name="allowCallbacks"><value>true</value></property> <property name="scriptObjects"> <map merge="true"> <entry key="paging"> <ref bean="webscripts.js.paging"/> </entry> </map> </property> <property name="webScriptsRegistryCache" ref="webScriptsRegistryCache"/> <!-- Use the time-limited transaction helper to keep request times to an acceptable duration --> <property name="transactionService" ref="transactionService" /> <!-- The transaction helper used to generate error responses must be unlimited --> <property name="fallbackTransactionHelper" ref="retryingTransactionHelper" /> <property name="authorityService" ref="AuthorityService" /> <property name="repository" ref="repositoryHelper" /> <property name="repositoryImageResolver" ref="webscripts.repo.imageresolver" /> <property name="templateProcessorRegistry" ref="webscripts.repo.registry.templateprocessor" /> <property name="scriptProcessorRegistry" ref="webscripts.repo.registry.scriptprocessor" /> <property name="descriptorService" ref="DescriptorService" /> <property name="tenantAdminService" ref="tenantAdminService" /> <property name="encryptTempFiles" value="${webscripts.encryptTempFiles}"/> <property name="tempDirectoryName" value="${webscripts.tempDirectoryName}"/> <property name="memoryThreshold" value="${webscripts.memoryThreshold}"/> <property name="maxContentSize" value="${webscripts.setMaxContentSize}"/> </bean>
<html> <body> <script> // callback function to display greeting function showGreeting(res) {alert(res.greeting + ' ' + res.user);} // invoke web script hello user web script var script = document.createElement('SCRIPT'); script.type = 'text/javascript'; script.src = 'http://localhost:8080/alfresco/s/hellouser.json?alf_callback=showGreeting'; document.body.appendChild(script); </script> </body> </html>
An alert box displaying the message hello admin indicates your callback is working.
This mimics the web script invocation made in the callback.html file.
The response is:
showGreeting({greeting: "hello", user: "admin"})
This is simply the vanilla Hello User web script response passed as an argument to the function named showGreeting as defined by the alf_callback query parameter. The full response is treated as JavaScript by the web browser, which executes it.
Links:
[1] https://docs.alfresco.com/../tasks/ws-response-format.html
[2] https://docs.alfresco.com/../tasks/ws-response-format-select.html
[3] https://docs.alfresco.com/../concepts/ws-hello-user-explain.html
[4] https://docs.alfresco.com/../tasks/ws-specify-user-identity.html
[5] https://docs.alfresco.com/../tasks/ws-json-callbacks-using.html
[6] https://docs.alfresco.com/../tasks/ws-json-callbacks-explain.html
[7] https://docs.alfresco.com/../tasks/ws-tutorials.html
[8] https://docs.alfresco.com/../tasks/ws-hello-user-create.html
[9] https://docs.alfresco.com/ws-hello-world-create.html
[10] https://docs.alfresco.com/ws-hello-user-create.html
[11] http://www.ietf.org/rfc/rfc2616.txt