Updated REST API URLs following discussion last week with Roy

- To get a form definition we now POST to /api/formdefinitions
 - To persist a form we now POST to /api/item_kind/item_id/formprocessor

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2009-04-20 09:37:44 +00:00
parent c379051889
commit 67020f42c2
5 changed files with 77 additions and 46 deletions

View File

@@ -1,7 +1,19 @@
<webscript>
<shortname>Form Definition</shortname>
<description>Returns a form definition for the requested item</description>
<url>/api/form/definition/{item_kind}/{item_id}</url>
<shortname>Form Definitions</shortname>
<description><![CDATA[
Returns a form definition for the requested item.<br/>
The body of the post should be in the form<br/>
{<br/>
"itemKind" : item kind,<br/>
"itemId" : item id,<br/>
"fields" : [fields],<br/>
"force" : [force]<br/>
}<br/>
<br/>
Returns the form model.
]]>
</description>
<url>/api/formdefinitions</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>

View File

@@ -1,17 +1,28 @@
function main()
{
// Extract template args
var itemKind = url.templateArgs['item_kind'];
var itemId = url.templateArgs['item_id'];
// check that required data is present in request body
if (json.has("itemKind") === false)
{
status.setCode(status.STATUS_BAD_REQUEST, "itemKind parameter is not present");
return;
}
if (json.has("itemId") === false)
{
status.setCode(status.STATUS_BAD_REQUEST, "itemId parameter is not present");
return;
}
// extract required data from request body
var itemKind = json.get("itemKind");
var itemId = json.get("itemId");
if (logger.isLoggingEnabled())
{
logger.log("itemKind = " + itemKind);
logger.log("itemId = " + itemId);
}
// TODO: Return error if item kind and/or id is missing?
// extract optional data from request body (if present)
var count = 0;
var fields = null;
@@ -84,9 +95,9 @@ function main()
formModel.data = {};
// TODO: retrieve the item URL from the response?
formModel.data.item = '/api/form/definition/' + itemKind + '/' + itemId;
formModel.data.item = '/api/formdefinitions';
// TODO: look for overridden submission url
formModel.data.submissionUrl = '/api/form/' + itemKind + '/' + itemId;
formModel.data.submissionUrl = '/api/' + itemKind + '/' + itemId + '/formprocessor';
formModel.data.type = formScriptObj.type;
formModel.data.definition = {};

View File

@@ -1,7 +1,7 @@
<webscript>
<shortname>Form</shortname>
<shortname>Form Persistence</shortname>
<description>Handles the submission of a form</description>
<url>/api/form/{item_kind}/{item_id}</url>
<url>/api/{item_kind}/{item_id}/formprocessor</url>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>internal</lifecycle>

View File

@@ -52,9 +52,8 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
protected static final String APPLICATION_JSON = "application/json";
protected static final String TEST_FORM_DESCRIPTION = "Test form description";
protected static final String TEST_FORM_TITLE = "Test form title";
protected String referencingNodeDefUrl;
protected static final String FORM_DEF_URL = "/api/formdefinitions";
protected String referencingNodeUpdateUrl;
protected String containingNodeDefUrl;
protected String containingNodeUpdateUrl;
protected String containingNodeUrl;
protected NodeRef referencingDocNodeRef;
@@ -154,24 +153,13 @@ public abstract class AbstractTestFormRestApi extends BaseWebScriptTest
// Create and store the urls to the referencingNode
StringBuilder builder = new StringBuilder();
builder.append("/api/form/definition/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId());
this.referencingNodeDefUrl = builder.toString();
builder = new StringBuilder();
builder.append("/api/form/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId());
builder.append("/api/node/workspace/").append(referencingDocNodeRef.getStoreRef().getIdentifier())
.append("/").append(referencingDocNodeRef.getId()).append("/formprocessor");
this.referencingNodeUpdateUrl = builder.toString();
// Create and store the urls to the containing node
builder = new StringBuilder();
builder.append("/api/form/definition/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId());
this.containingNodeDefUrl = builder.toString();
builder = new StringBuilder();
builder.append("/api/form/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId());
builder.append("/api/node/workspace/").append(containerNodeRef.getStoreRef().getIdentifier())
.append("/").append(containerNodeRef.getId()).append("/formprocessor");
this.containingNodeUpdateUrl = builder.toString();
// Store the original properties of this node

View File

@@ -29,6 +29,7 @@ import java.util.Iterator;
import java.util.List;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.web.scripts.TestWebScriptServer.PostRequest;
import org.alfresco.web.scripts.TestWebScriptServer.Response;
import org.alfresco.web.scripts.json.JSONUtils;
@@ -38,30 +39,49 @@ import org.json.JSONTokener;
public class FormRestApiGet_Test extends AbstractTestFormRestApi
{
public void testResponseContentType() throws Exception
protected JSONObject createItemJSON(NodeRef nodeRef) throws Exception
{
JSONObject jsonPostData = new JSONObject();
jsonPostData.put("itemKind", "node");
StringBuilder builder = new StringBuilder();
builder.append(nodeRef.getStoreRef().getProtocol()).append("/").append(
nodeRef.getStoreRef().getIdentifier()).append("/").append(nodeRef.getId());
jsonPostData.put("itemId", builder.toString());
return jsonPostData;
}
public void testResponseContentType() throws Exception
{
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
assertEquals("application/json;charset=UTF-8", rsp.getContentType());
}
public void testGetFormForNonExistentNode() throws Exception
{
// Replace all digits with an 'x' char - this should make for a non-existent node.
JSONObject jsonPostData = new JSONObject();
// Create a NodeRef with all digits changed to an 'x' char -
// this should make for a non-existent node.
String missingId = this.referencingDocNodeRef.getId().replaceAll("\\d", "x");
NodeRef missingNodeRef = new NodeRef(this.referencingDocNodeRef.getStoreRef(),
missingId);
JSONObject jsonPostData = createItemJSON(missingNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl.replaceAll("\\d", "x"),
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 404);
assertEquals("application/json;charset=UTF-8", rsp.getContentType());
}
public void testJsonContentParsesCorrectly() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
@@ -71,9 +91,9 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
public void testJsonUpperStructure() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
@@ -101,9 +121,9 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
@SuppressWarnings("unchecked")
public void testJsonFormData() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
// At this point the formData names have underscores
@@ -135,9 +155,9 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
@SuppressWarnings("unchecked")
public void testJsonDefinitionFields() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL,
jsonPostString, APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
@@ -172,7 +192,7 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
public void testJsonSelectedFields() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
JSONArray jsonFields = new JSONArray();
jsonFields.put("cm:name");
jsonFields.put("cm:title");
@@ -181,7 +201,7 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl, jsonPostString,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL, jsonPostString,
APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();
@@ -202,7 +222,7 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
public void testJsonForcedFields() throws Exception
{
JSONObject jsonPostData = new JSONObject();
JSONObject jsonPostData = createItemJSON(this.referencingDocNodeRef);
JSONArray jsonFields = new JSONArray();
jsonFields.put("cm:name");
@@ -218,7 +238,7 @@ public class FormRestApiGet_Test extends AbstractTestFormRestApi
// Submit the JSON request.
String jsonPostString = jsonPostData.toString();
Response rsp = sendRequest(new PostRequest(referencingNodeDefUrl, jsonPostString,
Response rsp = sendRequest(new PostRequest(FORM_DEF_URL, jsonPostString,
APPLICATION_JSON), 200);
String jsonResponseString = rsp.getContentAsString();