RM-5837 Refactor POST API calls to return HTTP response.

By default throw an exception if the calls are not successful. This
allows us to remove a huge number of assertion statements from REST
API tests.

Returning the response allows us to make more detailed assertions if
we need to - for example checking exactly what the status code was.
This commit is contained in:
Tom Page
2017-11-07 10:15:40 +00:00
parent 3438e4c520
commit fb1a5f18c3
7 changed files with 202 additions and 261 deletions

View File

@@ -26,6 +26,8 @@
*/ */
package org.alfresco.rest.core.v0; package org.alfresco.rest.core.v0;
import static org.testng.AssertJUnit.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URI; import java.net.URI;
@@ -342,18 +344,20 @@ public abstract class BaseAPI
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful.
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected boolean doPostJsonRequest(String adminUser, protected HttpResponse doPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
String... urlTemplateParams) String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams); return doPostJsonRequest(adminUser, adminPassword, expectFailure, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams);
} }
/** /**
@@ -361,18 +365,20 @@ public abstract class BaseAPI
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful.
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected boolean doSlingshotPostJsonRequest(String adminUser, protected HttpResponse doSlingshotPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
String... urlTemplateParams) String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams); return doPostJsonRequest(adminUser, adminPassword, expectFailure, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams);
} }
/** /**
@@ -380,13 +386,15 @@ public abstract class BaseAPI
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful.
* @param urlStart the start of the URL (for example "alfresco/s/slingshot"). * @param urlStart the start of the URL (for example "alfresco/s/slingshot").
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
private boolean doPostJsonRequest(String adminUser, private HttpResponse doPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure,
String urlStart, String urlStart,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
@@ -399,7 +407,9 @@ public abstract class BaseAPI
urlTemplateParams); urlTemplateParams);
try try
{ {
return doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
assertEquals("POST request was not successful.", httpResponse.getStatusLine().getStatusCode(), 200);
return httpResponse;
} }
catch (InstantiationException | IllegalAccessException error) catch (InstantiationException | IllegalAccessException error)
{ {
@@ -501,7 +511,7 @@ public abstract class BaseAPI
return returnValues; return returnValues;
} }
private <T extends HttpRequestBase> boolean doRequestJson( private <T extends HttpRequestBase> HttpResponse doRequestJson(
Class<T> requestType, Class<T> requestType,
String requestUrl, String requestUrl,
String adminUser, String adminUser,
@@ -525,7 +535,7 @@ public abstract class BaseAPI
LOGGER.info("Request body: {}", requestParams); LOGGER.info("Request body: {}", requestParams);
HttpResponse httpResponse = client.execute(adminUser, adminPassword, request); HttpResponse httpResponse = client.execute(adminUser, adminPassword, request);
LOGGER.info("Response: {}", httpResponse.getStatusLine()); LOGGER.info("Response: {}", httpResponse.getStatusLine());
return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK; return httpResponse;
} }
catch (UnsupportedEncodingException | URISyntaxException error1) catch (UnsupportedEncodingException | URISyntaxException error1)
{ {
@@ -540,7 +550,7 @@ public abstract class BaseAPI
client.close(); client.close();
} }
return false; return null;
} }
/** /**

View File

@@ -30,8 +30,8 @@ import java.text.MessageFormat;
import java.util.List; import java.util.List;
import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.core.v0.BaseAPI;
import org.apache.http.HttpResponse;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -60,22 +60,32 @@ public class CopyToAPI extends BaseAPI
* "{site}/{container}/{path}", "{site}/{container}", "{store_type}/{store_id}/{id}/{path}", * "{site}/{container}/{path}", "{site}/{container}", "{store_type}/{store_id}/{id}/{path}",
* "{store_type}/{store_id}/{id}" or "{store_type}/{store_id}". * "{store_type}/{store_id}/{id}" or "{store_type}/{store_id}".
* @param nodeRefs The list of nodes to copy. * @param nodeRefs The list of nodes to copy.
* @return true if the request was successful. * @return The HTTP Response.
* @throws AssertionError If the API call didn't return a 200 response.
*/ */
public boolean copyTo(String user, String password, String targetContainerPath, List<String> nodeRefs) public HttpResponse copyTo(String user, String password, String targetContainerPath, List<String> nodeRefs)
{ {
try return copyToAndGetResponse(user, password, false, targetContainerPath, nodeRefs);
{ }
JSONObject requestParams = new JSONObject();
requestParams.put("nodeRefs", new JSONArray(nodeRefs));
return doSlingshotPostJsonRequest(user, password, requestParams, /**
MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath)); * Copy a list of nodes to the target container.
} *
catch (JSONException error) * @param user The username of the user to use.
{ * @param password The password of the user.
LOGGER.error("Unable to extract response parameter", error); * @param expectFailure If false then an exception will be thrown if the POST is not successful.
} * @param targetContainerPath The destination to copy the nodes to. This should be in the format
return false; * "{site}/{container}/{path}", "{site}/{container}", "{store_type}/{store_id}/{id}/{path}",
* "{store_type}/{store_id}/{id}" or "{store_type}/{store_id}".
* @param nodeRefs The list of nodes to copy.
* @return The HTTP Response.
*/
public HttpResponse copyToAndGetResponse(String user, String password, boolean expectFailure, String targetContainerPath, List<String> nodeRefs)
{
JSONObject requestParams = new JSONObject();
requestParams.put("nodeRefs", new JSONArray(nodeRefs));
return doSlingshotPostJsonRequest(user, password, expectFailure, requestParams,
MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath));
} }
} }

View File

@@ -27,6 +27,7 @@
package org.alfresco.rest.v0; package org.alfresco.rest.v0;
import static org.alfresco.dataprep.AlfrescoHttpClient.MIME_TYPE_JSON; import static org.alfresco.dataprep.AlfrescoHttpClient.MIME_TYPE_JSON;
import static org.testng.AssertJUnit.assertNotNull;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
@@ -206,9 +207,9 @@ public class RMRolesAndActionsAPI extends BaseAPI
* @param user the user executing the action * @param user the user executing the action
* @param password the user's password * @param password the user's password
* @param contentName the content name * @param contentName the content name
* @return true if the action completed successfully * @return The HTTP response.
*/ */
public boolean executeAction(String user, String password, String contentName, RM_ACTIONS rm_action) public HttpResponse executeAction(String user, String password, String contentName, RM_ACTIONS rm_action)
{ {
return executeAction(user, password, contentName, rm_action, null); return executeAction(user, password, contentName, rm_action, null);
} }
@@ -220,32 +221,24 @@ public class RMRolesAndActionsAPI extends BaseAPI
* @param password the user's password * @param password the user's password
* @param contentName the record folder name * @param contentName the record folder name
* @param date the date to be updated * @param date the date to be updated
* @return true if the action completed successfully * @return The HTTP response.
*/ */
public boolean executeAction(String user, String password, String contentName, RM_ACTIONS action, ZonedDateTime date) public HttpResponse executeAction(String user, String password, String contentName, RM_ACTIONS action, ZonedDateTime date)
{ {
String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, contentName); String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, contentName);
try JSONObject requestParams = new JSONObject();
requestParams.put("name", action.getAction());
requestParams.put("nodeRef", recNodeRef);
if (date != null)
{ {
JSONObject requestParams = new JSONObject(); String thisMoment = date.format(DateTimeFormatter.ISO_INSTANT);
requestParams.put("name", action.getAction()); requestParams.put("params", new JSONObject()
requestParams.put("nodeRef", recNodeRef); .put("asOfDate", new JSONObject()
if (date != null) .put("iso8601", thisMoment)
{ )
String thisMoment = date.format(DateTimeFormatter.ISO_INSTANT); );
requestParams.put("params", new JSONObject()
.put("asOfDate", new JSONObject()
.put("iso8601", thisMoment)
)
);
}
return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API);
} }
catch (JSONException error) return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API);
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -288,37 +281,32 @@ public class RMRolesAndActionsAPI extends BaseAPI
* @param holdName the hold name * @param holdName the hold name
* @param reason hold reason * @param reason hold reason
* @param description hold description * @param description hold description
* @return true if the hold creation has been successful * @return The HTTP response (or null if no POST call was needed).
*/ */
public boolean createHold(String user, String password, String holdName, String reason, String description) public HttpResponse createHold(String user, String password, String holdName, String reason, String description)
{ {
// if the hold already exists don't try to create it again // if the hold already exists don't try to create it again
String holdsContainerPath = getFilePlanPath() + "/Holds"; String holdsContainerPath = getFilePlanPath() + "/Holds";
String fullHoldPath = holdsContainerPath + "/" + holdName;
CmisObject hold = getObjectByPath(user, password, holdsContainerPath + "/" + holdName); CmisObject hold = getObjectByPath(user, password, fullHoldPath);
if (hold != null) if (hold != null)
{ {
return true; return null;
} }
// retrieve the Holds container nodeRef // retrieve the Holds container nodeRef
String parentNodeRef = getItemNodeRef(user, password, "/Holds"); String parentNodeRef = getItemNodeRef(user, password, "/Holds");
try JSONObject requestParams = new JSONObject();
{ requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef);
JSONObject requestParams = new JSONObject(); requestParams.put("prop_cm_name", holdName);
requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); requestParams.put("prop_cm_description", description);
requestParams.put("prop_cm_name", holdName); requestParams.put("prop_rma_holdReason", reason);
requestParams.put("prop_cm_description", description);
requestParams.put("prop_rma_holdReason", reason);
boolean requestSucceeded = doPostJsonRequest(user, password, requestParams, CREATE_HOLDS_API); // Make the POST request and throw an assertion error if it fails.
return requestSucceeded && getObjectByPath(user, password, holdsContainerPath + "/" + holdName) != null; HttpResponse httpResponse = doPostJsonRequest(user, password, false, requestParams, CREATE_HOLDS_API);
} assertNotNull("Expected object to have been created at " + fullHoldPath,
catch (JSONException error) getObjectByPath(user, password, fullHoldPath));
{ return httpResponse;
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -327,24 +315,16 @@ public class RMRolesAndActionsAPI extends BaseAPI
* @param username the user updating the item * @param username the user updating the item
* @param password the user's password * @param password the user's password
* @param itemNodeRef the item noderef * @param itemNodeRef the item noderef
* @return true if the update of the item properties has been successful * @return The HTTP response.
*/ */
public boolean updateMetadata(String username, String password, String itemNodeRef, Map<RMProperty, String> properties) public HttpResponse updateMetadata(String username, String password, String itemNodeRef, Map<RMProperty, String> properties)
{ {
try JSONObject requestParams = new JSONObject();
{ addPropertyToRequest(requestParams, "prop_cm_name", properties, RMProperty.NAME);
JSONObject requestParams = new JSONObject(); addPropertyToRequest(requestParams, "prop_cm_title", properties, RMProperty.TITLE);
addPropertyToRequest(requestParams, "prop_cm_name", properties, RMProperty.NAME); addPropertyToRequest(requestParams, "prop_cm_description", properties, RMProperty.DESCRIPTION);
addPropertyToRequest(requestParams, "prop_cm_title", properties, RMProperty.TITLE); addPropertyToRequest(requestParams, "prop_cm_author", properties, RMProperty.AUTHOR);
addPropertyToRequest(requestParams, "prop_cm_description", properties, RMProperty.DESCRIPTION);
addPropertyToRequest(requestParams, "prop_cm_author", properties, RMProperty.AUTHOR);
return doPostJsonRequest(username, password, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", itemNodeRef)); return doPostJsonRequest(username, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", itemNodeRef));
}
catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
} }

View File

@@ -31,7 +31,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.core.v0.BaseAPI;
import org.json.JSONException; import org.apache.http.HttpResponse;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -56,24 +56,17 @@ public class RecordCategoriesAPI extends BaseAPI
* @param user the user creating the disposition schedule * @param user the user creating the disposition schedule
* @param password the user's password * @param password the user's password
* @param categoryName the category name to create the retention schedule for * @param categoryName the category name to create the retention schedule for
* @return true if the creation completed successfully * @return The HTTP Response.
*/ */
public boolean createRetentionSchedule(String user, String password, String categoryName) public HttpResponse createRetentionSchedule(String user, String password, String categoryName)
{ {
String catNodeRef = getNodeRefSpacesStore() + getItemNodeRef(user, password, "/" + categoryName); String catNodeRef = getNodeRefSpacesStore() + getItemNodeRef(user, password, "/" + categoryName);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("name", "createDispositionSchedule");
JSONObject requestParams = new JSONObject(); requestParams.put("nodeRef", catNodeRef);
requestParams.put("name", "createDispositionSchedule");
requestParams.put("nodeRef", catNodeRef);
return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API);
} catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -82,24 +75,17 @@ public class RecordCategoriesAPI extends BaseAPI
* @param user the user creating the disposition schedule * @param user the user creating the disposition schedule
* @param password the user's password * @param password the user's password
* @param retentionNodeRef the retention nodeRef * @param retentionNodeRef the retention nodeRef
* @return true if the creation completed successfully * @return The HTTP Response.
*/ */
public boolean setRetentionScheduleGeneralFields(String user, String password, String retentionNodeRef, Map<RETENTION_SCHEDULE, String> retentionProperties, Boolean appliedToRecords) public HttpResponse setRetentionScheduleGeneralFields(String user, String password, String retentionNodeRef, Map<RETENTION_SCHEDULE, String> retentionProperties, Boolean appliedToRecords)
{ {
String dispRetentionNodeRef = NODE_PREFIX + retentionNodeRef; String dispRetentionNodeRef = NODE_PREFIX + retentionNodeRef;
try JSONObject requestParams = new JSONObject();
{ requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY));
JSONObject requestParams = new JSONObject(); requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS));
requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY)); requestParams.put("prop_rma_recordLevelDisposition", appliedToRecords.toString());
requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS)); return doPostJsonRequest(user, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef));
requestParams.put("prop_rma_recordLevelDisposition", appliedToRecords.toString());
return doPostJsonRequest(user, password, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef));
} catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -108,30 +94,22 @@ public class RecordCategoriesAPI extends BaseAPI
* @param user the user creating the disposition schedule * @param user the user creating the disposition schedule
* @param password the user's password * @param password the user's password
* @param categoryName the category name to create the retention schedule for * @param categoryName the category name to create the retention schedule for
* @return true if the creation completed successfully * @return The HTTP Response.
*/ */
public boolean addDispositionScheduleSteps(String user, String password, String categoryName, Map<RETENTION_SCHEDULE, String> properties) public HttpResponse addDispositionScheduleSteps(String user, String password, String categoryName, Map<RETENTION_SCHEDULE, String> properties)
{ {
String catNodeRef = NODE_PREFIX + getItemNodeRef(user, password, "/" + categoryName); String catNodeRef = NODE_PREFIX + getItemNodeRef(user, password, "/" + categoryName);
{
try
{
JSONObject requestParams = new JSONObject();
addPropertyToRequest(requestParams, "name", properties, RETENTION_SCHEDULE.NAME);
addPropertyToRequest(requestParams, "description", properties, RETENTION_SCHEDULE.DESCRIPTION);
addPropertyToRequest(requestParams, "period", properties, RETENTION_SCHEDULE.RETENTION_PERIOD);
addPropertyToRequest(requestParams, "ghostOnDestroy", properties, RETENTION_SCHEDULE.RETENTION_GHOST);
addPropertyToRequest(requestParams, "periodProperty", properties, RETENTION_SCHEDULE.RETENTION_PERIOD_PROPERTY);
addPropertyToRequest(requestParams, "events", properties, RETENTION_SCHEDULE.RETENTION_EVENTS);
addPropertyToRequest(requestParams, "eligibleOnFirstCompleteEvent", properties, RETENTION_SCHEDULE.RETENTION_ELIGIBLE_FIRST_EVENT);
return doPostJsonRequest(user, password, requestParams, MessageFormat.format(DISPOSITION_ACTIONS_API, "{0}", catNodeRef)); JSONObject requestParams = new JSONObject();
} catch (JSONException error) addPropertyToRequest(requestParams, "name", properties, RETENTION_SCHEDULE.NAME);
{ addPropertyToRequest(requestParams, "description", properties, RETENTION_SCHEDULE.DESCRIPTION);
LOGGER.error("Unable to extract response parameter", error); addPropertyToRequest(requestParams, "period", properties, RETENTION_SCHEDULE.RETENTION_PERIOD);
} addPropertyToRequest(requestParams, "ghostOnDestroy", properties, RETENTION_SCHEDULE.RETENTION_GHOST);
return false; addPropertyToRequest(requestParams, "periodProperty", properties, RETENTION_SCHEDULE.RETENTION_PERIOD_PROPERTY);
} addPropertyToRequest(requestParams, "events", properties, RETENTION_SCHEDULE.RETENTION_EVENTS);
addPropertyToRequest(requestParams, "eligibleOnFirstCompleteEvent", properties, RETENTION_SCHEDULE.RETENTION_ELIGIBLE_FIRST_EVENT);
return doPostJsonRequest(user, password, false, requestParams, MessageFormat.format(DISPOSITION_ACTIONS_API, "{0}", catNodeRef));
} }
/** /**

View File

@@ -28,6 +28,7 @@ package org.alfresco.rest.v0;
import org.alfresco.dataprep.ContentService; import org.alfresco.dataprep.ContentService;
import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.core.v0.BaseAPI;
import org.apache.http.HttpResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -55,9 +56,9 @@ public class RecordFoldersAPI extends BaseAPI
* @param user the user closing the folder * @param user the user closing the folder
* @param password the user's password * @param password the user's password
* @param recordFolder the record folder name * @param recordFolder the record folder name
* @return true if the action completed successfully * @return The HTTP Response (or null if the response could not be understood).
*/ */
public boolean closeRecordFolder(String user, String password, String recordFolder) public HttpResponse closeRecordFolder(String user, String password, String recordFolder)
{ {
String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordFolder); String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordFolder);
@@ -67,12 +68,12 @@ public class RecordFoldersAPI extends BaseAPI
requestParams.put("name", "closeRecordFolder"); requestParams.put("name", "closeRecordFolder");
requestParams.put("nodeRef", recNodeRef); requestParams.put("nodeRef", recNodeRef);
return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API);
} }
catch (JSONException error) catch (JSONException error)
{ {
LOGGER.error("Unable to extract response parameter", error); LOGGER.error("Unable to extract response parameter", error);
} }
return false; return null;
} }
} }

View File

@@ -29,11 +29,11 @@ package org.alfresco.rest.v0;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Map; import java.util.Map;
import javafx.util.Pair;
import org.alfresco.dataprep.CMISUtil.DocumentType; import org.alfresco.dataprep.CMISUtil.DocumentType;
import org.alfresco.dataprep.ContentService; import org.alfresco.dataprep.ContentService;
import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.core.v0.BaseAPI;
import org.apache.chemistry.opencmis.client.api.CmisObject; import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.http.HttpResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -41,6 +41,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javafx.util.Pair;
/** /**
* Methods to make API requests using v0 API on records * Methods to make API requests using v0 API on records
* *
@@ -65,25 +67,17 @@ public class RecordsAPI extends BaseAPI
* @param password the user's password * @param password the user's password
* @param siteID the site id in which the document exists * @param siteID the site id in which the document exists
* @param documentName the document name * @param documentName the document name
* @return true if the action was successful * @return The HTTP Response.
*/ */
public boolean declareDocumentAsRecord(String user, String password, String siteID, String documentName) public HttpResponse declareDocumentAsRecord(String user, String password, String siteID, String documentName)
{ {
String docNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, siteID, documentName); String docNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, siteID, documentName);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("actionedUponNode", docNodeRef);
JSONObject requestParams = new JSONObject(); requestParams.put("actionDefinitionName", "create-record");
requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "create-record");
return doPostJsonRequest(user, password, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API);
}
catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -92,28 +86,19 @@ public class RecordsAPI extends BaseAPI
* @param user the user declaring the document as record * @param user the user declaring the document as record
* @param password the user's password * @param password the user's password
* @param recordName the record name * @param recordName the record name
* @return true if the action completed successfully * @return The HTTP Response.
*/ */
public boolean completeRecord(String user, String password, String recordName) public HttpResponse completeRecord(String user, String password, String recordName)
{ {
String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName); String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("name", "declareRecord");
JSONObject requestParams = new JSONObject(); requestParams.put("nodeRef", recNodeRef);
requestParams.put("name", "declareRecord");
requestParams.put("nodeRef", recNodeRef);
return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API);
}
catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
* Reject the record given as parameter * Reject the record given as parameter
* *
@@ -121,28 +106,38 @@ public class RecordsAPI extends BaseAPI
* @param password the user's password * @param password the user's password
* @param recordName the record name * @param recordName the record name
* @param reason reject reason * @param reason reject reason
* @return true if the action completed successfully * @return The HTTP Response.
* @throws AssertionError If the POST call is not successful.
*/ */
public boolean rejectRecord(String user, String password, String recordName, String reason) public HttpResponse rejectRecord(String user, String password, String recordName, String reason)
{
return rejectRecordAndGetResponse(user, password, false, recordName, reason);
}
/**
* Reject the record given as parameter
*
* @param user the user declaring the document as record
* @param password the user's password
* @param expectFailure If false then throws an exception if the POST call fails.
* @param recordName the record name
* @param reason reject reason
* @return The HTTP Response.
* @throws AssertionError If expectFailure is false and the POST call is not successful.
*/
public HttpResponse rejectRecordAndGetResponse(String user, String password, boolean expectFailure, String recordName, String reason)
{ {
String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName); String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("name", "reject");
JSONObject requestParams = new JSONObject(); requestParams.put("nodeRef", recNodeRef);
requestParams.put("name", "reject"); requestParams.put("params",new JSONObject()
requestParams.put("nodeRef", recNodeRef); .put("reason",reason));
requestParams.put("params",new JSONObject()
.put("reason",reason));
return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, expectFailure, requestParams, RM_ACTIONS_API);
}
catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
* Declare document version as record * Declare document version as record
* *
@@ -150,29 +145,25 @@ public class RecordsAPI extends BaseAPI
* @param password the user's password * @param password the user's password
* @param siteID the site id in which the document exists * @param siteID the site id in which the document exists
* @param documentName the document name * @param documentName the document name
* @return true if the action was successful * @return The HTTP Response.
*/ */
public boolean declareDocumentVersionAsRecord(String user, String password, String siteID, String documentName) public HttpResponse declareDocumentVersionAsRecord(String user, String password, String siteID, String documentName)
{ {
String docNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, siteID, documentName); String docNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, siteID, documentName);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("actionedUponNode", docNodeRef);
JSONObject requestParams = new JSONObject(); requestParams.put("actionDefinitionName", "declare-as-version-record");
requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "declare-as-version-record");
return doPostJsonRequest(user, password, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API);
}
catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
* Creates a non-electronic record * Creates a non-electronic record
* <ul>
* <li>eg. of usage for Unfiled records with folder : createNonElectronicRecord(getAdminName(), getAdminPassword(), properties, UNFILED_RECORDS_BREADCRUMB, "unfiled records folder");
* <li>eg. of usage for creating record directly in Unfiled Records : createNonElectronicRecord(getAdminName(), getAdminPassword(), properties, UNFILED_RECORDS_BREADCRUMB, "");
* </ul>
* *
* @param username the username * @param username the username
* @param password the password * @param password the password
@@ -180,16 +171,14 @@ public class RecordsAPI extends BaseAPI
* @param categoryName the category that contains the record, in the case in which the container would be Unfiled records use UNFILED_RECORDS_BREADCRUMB as value * @param categoryName the category that contains the record, in the case in which the container would be Unfiled records use UNFILED_RECORDS_BREADCRUMB as value
* @param folderName the folder inside which the record exists, in the case in which the folder name is "", the record will be created directly in the specified container * @param folderName the folder inside which the record exists, in the case in which the folder name is "", the record will be created directly in the specified container
* this case is useful when trying to create a record directly in Unfiled Records * this case is useful when trying to create a record directly in Unfiled Records
* @return true if the creation of the record has been successful * @return The HTTP Response (or null if the request was not needed).
* eg. of usage for Unfiled records with folder : createNonElectronicRecord(getAdminName(), getAdminPassword(), properties, UNFILED_RECORDS_BREADCRUMB, "unfiled records folder");
* eg. of usage for creating record directly in Unfiled Records : createNonElectronicRecord(getAdminName(), getAdminPassword(), properties, UNFILED_RECORDS_BREADCRUMB, "");
*/ */
public <K extends Enum<?>> boolean createNonElectronicRecord(String username, String password, Map<K, String> properties, String categoryName, String folderName) public <K extends Enum<?>> HttpResponse createNonElectronicRecord(String username, String password, Map<K, String> properties, String categoryName, String folderName)
{ {
String recordName = properties.get(RMProperty.NAME); String recordName = properties.get(RMProperty.NAME);
if (getRecord(username, password, folderName, recordName) != null) if (getRecord(username, password, folderName, recordName) != null)
{ {
return true; return null;
} }
String recordPath = "/" + categoryName; String recordPath = "/" + categoryName;
if (!folderName.equals("")) if (!folderName.equals(""))
@@ -201,7 +190,7 @@ public class RecordsAPI extends BaseAPI
if (record != null) if (record != null)
{ {
return true; return null;
} }
// non-electronic properties // non-electronic properties
String recordTitle = getPropertyValue(properties, RMProperty.TITLE); String recordTitle = getPropertyValue(properties, RMProperty.TITLE);
@@ -216,26 +205,19 @@ public class RecordsAPI extends BaseAPI
// retrieve the container nodeRef // retrieve the container nodeRef
String parentNodeRef = getItemNodeRef(username, password, recordPath); String parentNodeRef = getItemNodeRef(username, password, recordPath);
try JSONObject requestParams = new JSONObject();
{ requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef);
JSONObject requestParams = new JSONObject(); requestParams.put("prop_cm_name", recordName);
requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); requestParams.put("prop_cm_title", recordTitle);
requestParams.put("prop_cm_name", recordName); requestParams.put("prop_cm_description", description);
requestParams.put("prop_cm_title", recordTitle); requestParams.put("prop_rma_physicalSize", physicalSize);
requestParams.put("prop_cm_description", description); requestParams.put("prop_rma_numberOfCopies", numberOfCopies);
requestParams.put("prop_rma_physicalSize", physicalSize); requestParams.put("prop_rma_storageLocation", storage);
requestParams.put("prop_rma_numberOfCopies", numberOfCopies); requestParams.put("prop_rma_shelf", shelf);
requestParams.put("prop_rma_storageLocation", storage); requestParams.put("prop_rma_box", box);
requestParams.put("prop_rma_shelf", shelf); requestParams.put("prop_rma_file", file);
requestParams.put("prop_rma_box", box);
requestParams.put("prop_rma_file", file);
return doPostJsonRequest(username, password, requestParams, CREATE_NON_ELECTRONIC_RECORD_API); return doPostJsonRequest(username, password, false, requestParams, CREATE_NON_ELECTRONIC_RECORD_API);
} catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
/** /**
@@ -351,24 +333,17 @@ public class RecordsAPI extends BaseAPI
* @param user the user * @param user the user
* @param password the user's password * @param password the user's password
* @param nodeId the in place record node id * @param nodeId the in place record node id
* @return true if the action was successful * @return The HTTP Response.
*/ */
public boolean hideRecord(String user, String password, String nodeId) public HttpResponse hideRecord(String user, String password, String nodeId)
{ {
String docNodeRef = getNodeRefSpacesStore() + nodeId; String docNodeRef = getNodeRefSpacesStore() + nodeId;
try JSONObject requestParams = new JSONObject();
{ requestParams.put("actionedUponNode", docNodeRef);
JSONObject requestParams = new JSONObject(); requestParams.put("actionDefinitionName", "hide-record");
requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "hide-record");
return doPostJsonRequest(user, password, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API);
} catch (JSONException error)
{
LOGGER.error("Unable to extract response parameter", error);
}
return false;
} }
} }

View File

@@ -37,6 +37,7 @@ import java.util.stream.Collectors;
import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.core.v0.BaseAPI;
import org.alfresco.rest.rm.community.model.rules.ActionsOnRule; import org.alfresco.rest.rm.community.model.rules.ActionsOnRule;
import org.alfresco.rest.rm.community.model.rules.RuleDefinition; import org.alfresco.rest.rm.community.model.rules.RuleDefinition;
import org.apache.http.HttpResponse;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@@ -63,20 +64,20 @@ public class RulesAPI extends BaseAPI
* *
* @param containerNodeRef the container to have the rule created on * @param containerNodeRef the container to have the rule created on
* @param ruleProperties the rule properties * @param ruleProperties the rule properties
* @return true if the rule has been created successfully, false otherwise * @return The HTTP Response (or null if the response could not be understood).
*/ */
public boolean createRule(String username, String password, String containerNodeRef, RuleDefinition ruleProperties) public HttpResponse createRule(String username, String password, String containerNodeRef, RuleDefinition ruleProperties)
{ {
try try
{ {
return doPostJsonRequest(username, password, getRuleRequest(ruleProperties), MessageFormat.format(RULES_API, "{0}", containerNodeRef)); return doPostJsonRequest(username, password, false, getRuleRequest(ruleProperties), MessageFormat.format(RULES_API, "{0}", containerNodeRef));
} }
catch (JSONException error) catch (JSONException error)
{ {
LOGGER.error("Unable to extract response parameter.", error); LOGGER.error("Unable to extract response parameter.", error);
} }
return false; return null;
} }
/** /**
@@ -309,22 +310,15 @@ public class RulesAPI extends BaseAPI
* @param password the password * @param password the password
* @param containerNodeRef the container nodeRef * @param containerNodeRef the container nodeRef
* *
* @return true if the rule has been disabled or if the current state is disabled * @return The HTTP Response (or null if the current state is disabled).
*/ */
public boolean disableRulesInheritance(String username, String password, String containerNodeRef) public HttpResponse disableRulesInheritance(String username, String password, String containerNodeRef)
{ {
try if(containerInheritsRulesFromParent(username, password, containerNodeRef))
{ {
if(containerInheritsRulesFromParent(username, password, containerNodeRef)) return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
{
return doPostJsonRequest(username, password, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
}
} }
catch (JSONException e) return null;
{
return false;
}
return true;
} }
/** /**
@@ -333,22 +327,15 @@ public class RulesAPI extends BaseAPI
* @param username the username * @param username the username
* @param password the password * @param password the password
* @param containerNodeRef the container nodeRef * @param containerNodeRef the container nodeRef
* @return true if the rule has been enabled or if the current state is enabled * @return The HTTP Response (or null if the current state is disabled).
*/ */
public boolean enableRulesInheritance(String username, String password, String containerNodeRef) public HttpResponse enableRulesInheritance(String username, String password, String containerNodeRef)
{ {
try if (!containerInheritsRulesFromParent(username, password, containerNodeRef))
{ {
if (!containerInheritsRulesFromParent(username, password, containerNodeRef)) return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
{
return doPostJsonRequest(username, password, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
}
} }
catch (JSONException e) return null;
{
return false;
}
return true;
} }
/** /**