From 67020f42c20b10513e17ca589ed4682a2ddda3ac Mon Sep 17 00:00:00 2001 From: Gavin Cornwell Date: Mon, 20 Apr 2009 09:37:44 +0000 Subject: [PATCH] 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 --- .../repository/forms/definition.post.desc.xml | 18 +++++-- .../repository/forms/definition.post.json.js | 25 ++++++--- .../repository/forms/form.post.desc.xml | 4 +- .../forms/AbstractTestFormRestApi.java | 22 ++------ .../scripts/forms/FormRestApiGet_Test.java | 54 +++++++++++++------ 5 files changed, 77 insertions(+), 46 deletions(-) diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.desc.xml index 16ec75a29c..146c56277a 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.desc.xml @@ -1,7 +1,19 @@ - Form Definition - Returns a form definition for the requested item - /api/form/definition/{item_kind}/{item_id} + Form Definitions + + The body of the post should be in the form
+ {
+ "itemKind" : item kind,
+ "itemId" : item id,
+ "fields" : [fields],
+ "force" : [force]
+ }
+
+ Returns the form model. + ]]> +
+ /api/formdefinitions user required diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.json.js b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.json.js index a83d75c8ef..09a70d2eec 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.json.js +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/definition.post.json.js @@ -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 = {}; diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/form.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/form.post.desc.xml index 6d8e89fcff..bbdd827d24 100644 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/forms/form.post.desc.xml +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/forms/form.post.desc.xml @@ -1,7 +1,7 @@ - Form + Form Persistence Handles the submission of a form - /api/form/{item_kind}/{item_id} + /api/{item_kind}/{item_id}/formprocessor user required internal diff --git a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java index 53bc850592..73d5526030 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/AbstractTestFormRestApi.java @@ -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 diff --git a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiGet_Test.java b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiGet_Test.java index ef84ebe803..fd9809f0a5 100644 --- a/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiGet_Test.java +++ b/source/java/org/alfresco/repo/web/scripts/forms/FormRestApiGet_Test.java @@ -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();