diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index ca03bc9143..06674c99e1 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -26,6 +26,8 @@ */ package org.alfresco.rest.core.v0; +import static org.testng.AssertJUnit.assertEquals; + import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; @@ -342,18 +344,20 @@ public abstract class BaseAPI * * @param adminUser user with administrative privileges * @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 urlTemplate request URL template * @param urlTemplateParams zero or more parameters used with urlTemplate */ - protected boolean doPostJsonRequest(String adminUser, + protected HttpResponse doPostJsonRequest(String adminUser, String adminPassword, + boolean expectFailure, JSONObject requestParams, String urlTemplate, String... urlTemplateParams) { 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 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 urlTemplate request URL template * @param urlTemplateParams zero or more parameters used with urlTemplate */ - protected boolean doSlingshotPostJsonRequest(String adminUser, + protected HttpResponse doSlingshotPostJsonRequest(String adminUser, String adminPassword, + boolean expectFailure, JSONObject requestParams, String urlTemplate, String... urlTemplateParams) { 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 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 requestParams zero or more endpoint specific request parameters * @param urlTemplate request URL template * @param urlTemplateParams zero or more parameters used with urlTemplate */ - private boolean doPostJsonRequest(String adminUser, + private HttpResponse doPostJsonRequest(String adminUser, String adminPassword, + boolean expectFailure, String urlStart, JSONObject requestParams, String urlTemplate, @@ -399,7 +407,9 @@ public abstract class BaseAPI urlTemplateParams); 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) { @@ -501,7 +511,7 @@ public abstract class BaseAPI return returnValues; } - private boolean doRequestJson( + private HttpResponse doRequestJson( Class requestType, String requestUrl, String adminUser, @@ -525,7 +535,7 @@ public abstract class BaseAPI LOGGER.info("Request body: {}", requestParams); HttpResponse httpResponse = client.execute(adminUser, adminPassword, request); LOGGER.info("Response: {}", httpResponse.getStatusLine()); - return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK; + return httpResponse; } catch (UnsupportedEncodingException | URISyntaxException error1) { @@ -540,7 +550,7 @@ public abstract class BaseAPI client.close(); } - return false; + return null; } /** diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CopyToAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CopyToAPI.java index 0ec3c49579..ecbfc006ce 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CopyToAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CopyToAPI.java @@ -30,8 +30,8 @@ import java.text.MessageFormat; import java.util.List; import org.alfresco.rest.core.v0.BaseAPI; +import org.apache.http.HttpResponse; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,22 +60,32 @@ public class CopyToAPI extends BaseAPI * "{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 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 nodeRefs) + public HttpResponse copyTo(String user, String password, String targetContainerPath, List nodeRefs) { - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("nodeRefs", new JSONArray(nodeRefs)); + return copyToAndGetResponse(user, password, false, targetContainerPath, nodeRefs); + } - return doSlingshotPostJsonRequest(user, password, requestParams, - MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath)); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + /** + * Copy a list of nodes to the target container. + * + * @param user The username of the user to use. + * @param password The password of the user. + * @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 + * "{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 nodeRefs) + { + JSONObject requestParams = new JSONObject(); + requestParams.put("nodeRefs", new JSONArray(nodeRefs)); + + return doSlingshotPostJsonRequest(user, password, expectFailure, requestParams, + MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath)); } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java index 32fead509e..9ff6069efe 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RMRolesAndActionsAPI.java @@ -27,6 +27,7 @@ package org.alfresco.rest.v0; import static org.alfresco.dataprep.AlfrescoHttpClient.MIME_TYPE_JSON; +import static org.testng.AssertJUnit.assertNotNull; import java.io.IOException; import java.text.MessageFormat; @@ -206,9 +207,9 @@ public class RMRolesAndActionsAPI extends BaseAPI * @param user the user executing the action * @param password the user's password * @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); } @@ -220,32 +221,24 @@ public class RMRolesAndActionsAPI extends BaseAPI * @param password the user's password * @param contentName the record folder name * @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); - try + JSONObject requestParams = new JSONObject(); + requestParams.put("name", action.getAction()); + requestParams.put("nodeRef", recNodeRef); + if (date != null) { - JSONObject requestParams = new JSONObject(); - requestParams.put("name", action.getAction()); - requestParams.put("nodeRef", recNodeRef); - if (date != null) - { - 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); + String thisMoment = date.format(DateTimeFormatter.ISO_INSTANT); + requestParams.put("params", new JSONObject() + .put("asOfDate", new JSONObject() + .put("iso8601", thisMoment) + ) + ); } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); } /** @@ -288,37 +281,32 @@ public class RMRolesAndActionsAPI extends BaseAPI * @param holdName the hold name * @param reason hold reason * @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 String holdsContainerPath = getFilePlanPath() + "/Holds"; - - CmisObject hold = getObjectByPath(user, password, holdsContainerPath + "/" + holdName); + String fullHoldPath = holdsContainerPath + "/" + holdName; + CmisObject hold = getObjectByPath(user, password, fullHoldPath); if (hold != null) { - return true; + return null; } // retrieve the Holds container nodeRef String parentNodeRef = getItemNodeRef(user, password, "/Holds"); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); - requestParams.put("prop_cm_name", holdName); - requestParams.put("prop_cm_description", description); - requestParams.put("prop_rma_holdReason", reason); + JSONObject requestParams = new JSONObject(); + requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); + requestParams.put("prop_cm_name", holdName); + requestParams.put("prop_cm_description", description); + requestParams.put("prop_rma_holdReason", reason); - boolean requestSucceeded = doPostJsonRequest(user, password, requestParams, CREATE_HOLDS_API); - return requestSucceeded && getObjectByPath(user, password, holdsContainerPath + "/" + holdName) != null; - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + // Make the POST request and throw an assertion error if it fails. + HttpResponse httpResponse = doPostJsonRequest(user, password, false, requestParams, CREATE_HOLDS_API); + assertNotNull("Expected object to have been created at " + fullHoldPath, + getObjectByPath(user, password, fullHoldPath)); + return httpResponse; } /** @@ -327,24 +315,16 @@ public class RMRolesAndActionsAPI extends BaseAPI * @param username the user updating the item * @param password the user's password * @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 properties) + public HttpResponse updateMetadata(String username, String password, String itemNodeRef, Map properties) { - try - { - JSONObject requestParams = new JSONObject(); - addPropertyToRequest(requestParams, "prop_cm_name", properties, RMProperty.NAME); - addPropertyToRequest(requestParams, "prop_cm_title", properties, RMProperty.TITLE); - addPropertyToRequest(requestParams, "prop_cm_description", properties, RMProperty.DESCRIPTION); - addPropertyToRequest(requestParams, "prop_cm_author", properties, RMProperty.AUTHOR); + JSONObject requestParams = new JSONObject(); + addPropertyToRequest(requestParams, "prop_cm_name", properties, RMProperty.NAME); + addPropertyToRequest(requestParams, "prop_cm_title", properties, RMProperty.TITLE); + 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)); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(username, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", itemNodeRef)); } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordCategoriesAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordCategoriesAPI.java index 8d11c14b14..96e12a7744 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordCategoriesAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordCategoriesAPI.java @@ -31,7 +31,7 @@ import java.util.HashMap; import java.util.Map; import org.alfresco.rest.core.v0.BaseAPI; -import org.json.JSONException; +import org.apache.http.HttpResponse; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,24 +56,17 @@ public class RecordCategoriesAPI extends BaseAPI * @param user the user creating the disposition schedule * @param password the user's password * @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); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("name", "createDispositionSchedule"); - requestParams.put("nodeRef", catNodeRef); + JSONObject requestParams = new JSONObject(); + requestParams.put("name", "createDispositionSchedule"); + requestParams.put("nodeRef", catNodeRef); - return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); - } catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); } /** @@ -82,24 +75,17 @@ public class RecordCategoriesAPI extends BaseAPI * @param user the user creating the disposition schedule * @param password the user's password * @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 retentionProperties, Boolean appliedToRecords) + public HttpResponse setRetentionScheduleGeneralFields(String user, String password, String retentionNodeRef, Map retentionProperties, Boolean appliedToRecords) { String dispRetentionNodeRef = NODE_PREFIX + retentionNodeRef; - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY)); - requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS)); - 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; + JSONObject requestParams = new JSONObject(); + requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY)); + requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS)); + requestParams.put("prop_rma_recordLevelDisposition", appliedToRecords.toString()); + return doPostJsonRequest(user, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef)); } /** @@ -108,30 +94,22 @@ public class RecordCategoriesAPI extends BaseAPI * @param user the user creating the disposition schedule * @param password the user's password * @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 properties) + public HttpResponse addDispositionScheduleSteps(String user, String password, String categoryName, Map properties) { 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)); - } catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; - } + 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, false, requestParams, MessageFormat.format(DISPOSITION_ACTIONS_API, "{0}", catNodeRef)); } /** diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordFoldersAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordFoldersAPI.java index c605fee395..776888b633 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordFoldersAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordFoldersAPI.java @@ -28,6 +28,7 @@ package org.alfresco.rest.v0; import org.alfresco.dataprep.ContentService; import org.alfresco.rest.core.v0.BaseAPI; +import org.apache.http.HttpResponse; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -55,9 +56,9 @@ public class RecordFoldersAPI extends BaseAPI * @param user the user closing the folder * @param password the user's password * @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); @@ -67,12 +68,12 @@ public class RecordFoldersAPI extends BaseAPI requestParams.put("name", "closeRecordFolder"); 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; + return null; } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordsAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordsAPI.java index ca51c60200..0801d3b08c 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordsAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RecordsAPI.java @@ -29,11 +29,11 @@ package org.alfresco.rest.v0; import java.text.MessageFormat; import java.util.Map; -import javafx.util.Pair; import org.alfresco.dataprep.CMISUtil.DocumentType; import org.alfresco.dataprep.ContentService; import org.alfresco.rest.core.v0.BaseAPI; import org.apache.chemistry.opencmis.client.api.CmisObject; +import org.apache.http.HttpResponse; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; @@ -41,6 +41,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import javafx.util.Pair; + /** * 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 siteID the site id in which the document exists * @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); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("actionedUponNode", docNodeRef); - requestParams.put("actionDefinitionName", "create-record"); + JSONObject requestParams = new JSONObject(); + requestParams.put("actionedUponNode", docNodeRef); + requestParams.put("actionDefinitionName", "create-record"); - return doPostJsonRequest(user, password, requestParams, ACTIONS_API); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); } /** @@ -92,28 +86,19 @@ public class RecordsAPI extends BaseAPI * @param user the user declaring the document as record * @param password the user's password * @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); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("name", "declareRecord"); - requestParams.put("nodeRef", recNodeRef); + JSONObject requestParams = new JSONObject(); + requestParams.put("name", "declareRecord"); + requestParams.put("nodeRef", recNodeRef); - return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); } - /** * Reject the record given as parameter * @@ -121,28 +106,38 @@ public class RecordsAPI extends BaseAPI * @param password the user's password * @param recordName the record name * @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); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("name", "reject"); - requestParams.put("nodeRef", recNodeRef); - requestParams.put("params",new JSONObject() - .put("reason",reason)); + JSONObject requestParams = new JSONObject(); + requestParams.put("name", "reject"); + requestParams.put("nodeRef", recNodeRef); + requestParams.put("params",new JSONObject() + .put("reason",reason)); - return doPostJsonRequest(user, password, requestParams, RM_ACTIONS_API); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, expectFailure, requestParams, RM_ACTIONS_API); } + /** * Declare document version as record * @@ -150,29 +145,25 @@ public class RecordsAPI extends BaseAPI * @param password the user's password * @param siteID the site id in which the document exists * @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); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("actionedUponNode", docNodeRef); - requestParams.put("actionDefinitionName", "declare-as-version-record"); + JSONObject requestParams = new JSONObject(); + requestParams.put("actionedUponNode", docNodeRef); + requestParams.put("actionDefinitionName", "declare-as-version-record"); - return doPostJsonRequest(user, password, requestParams, ACTIONS_API); - } - catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); } /** * Creates a non-electronic record + *
    + *
  • 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, ""); + *
* * @param username the username * @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 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 - * @return true if the creation of the record has been successful - * 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, ""); + * @return The HTTP Response (or null if the request was not needed). */ - public > boolean createNonElectronicRecord(String username, String password, Map properties, String categoryName, String folderName) + public > HttpResponse createNonElectronicRecord(String username, String password, Map properties, String categoryName, String folderName) { String recordName = properties.get(RMProperty.NAME); if (getRecord(username, password, folderName, recordName) != null) { - return true; + return null; } String recordPath = "/" + categoryName; if (!folderName.equals("")) @@ -201,7 +190,7 @@ public class RecordsAPI extends BaseAPI if (record != null) { - return true; + return null; } // non-electronic properties String recordTitle = getPropertyValue(properties, RMProperty.TITLE); @@ -216,26 +205,19 @@ public class RecordsAPI extends BaseAPI // retrieve the container nodeRef String parentNodeRef = getItemNodeRef(username, password, recordPath); - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); - requestParams.put("prop_cm_name", recordName); - requestParams.put("prop_cm_title", recordTitle); - requestParams.put("prop_cm_description", description); - requestParams.put("prop_rma_physicalSize", physicalSize); - requestParams.put("prop_rma_numberOfCopies", numberOfCopies); - requestParams.put("prop_rma_storageLocation", storage); - requestParams.put("prop_rma_shelf", shelf); - requestParams.put("prop_rma_box", box); - requestParams.put("prop_rma_file", file); + JSONObject requestParams = new JSONObject(); + requestParams.put("alf_destination", getNodeRefSpacesStore() + parentNodeRef); + requestParams.put("prop_cm_name", recordName); + requestParams.put("prop_cm_title", recordTitle); + requestParams.put("prop_cm_description", description); + requestParams.put("prop_rma_physicalSize", physicalSize); + requestParams.put("prop_rma_numberOfCopies", numberOfCopies); + requestParams.put("prop_rma_storageLocation", storage); + requestParams.put("prop_rma_shelf", shelf); + requestParams.put("prop_rma_box", box); + requestParams.put("prop_rma_file", file); - return doPostJsonRequest(username, password, requestParams, CREATE_NON_ELECTRONIC_RECORD_API); - } catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(username, password, false, requestParams, CREATE_NON_ELECTRONIC_RECORD_API); } /** @@ -351,24 +333,17 @@ public class RecordsAPI extends BaseAPI * @param user the user * @param password the user's password * @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; - try - { - JSONObject requestParams = new JSONObject(); - requestParams.put("actionedUponNode", docNodeRef); - requestParams.put("actionDefinitionName", "hide-record"); + JSONObject requestParams = new JSONObject(); + requestParams.put("actionedUponNode", docNodeRef); + requestParams.put("actionDefinitionName", "hide-record"); - return doPostJsonRequest(user, password, requestParams, ACTIONS_API); - } catch (JSONException error) - { - LOGGER.error("Unable to extract response parameter", error); - } - return false; + return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); } } diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RulesAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RulesAPI.java index 00704081da..8b3bef51b0 100644 --- a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RulesAPI.java +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/RulesAPI.java @@ -37,6 +37,7 @@ import java.util.stream.Collectors; import org.alfresco.rest.core.v0.BaseAPI; import org.alfresco.rest.rm.community.model.rules.ActionsOnRule; import org.alfresco.rest.rm.community.model.rules.RuleDefinition; +import org.apache.http.HttpResponse; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -63,20 +64,20 @@ public class RulesAPI extends BaseAPI * * @param containerNodeRef the container to have the rule created on * @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 { - 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) { 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 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, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); - } + return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); } - catch (JSONException e) - { - return false; - } - return true; + return null; } /** @@ -333,22 +327,15 @@ public class RulesAPI extends BaseAPI * @param username the username * @param password the password * @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, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); - } + return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); } - catch (JSONException e) - { - return false; - } - return true; + return null; } /**