diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java new file mode 100644 index 0000000000..5404b483c8 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/APIUtils.java @@ -0,0 +1,73 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.rest.core.v0; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helper methods for use with REST APIs. + * + * @author Tom Page + * @since 2.6 + */ +public class APIUtils +{ + /** Logger for this class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(APIUtils.class); + + /** Private constructor for helper class. */ + private APIUtils() + { + } + + /** + * Extract the body of a HTTP response as a JSON object. + * + * @param httpResponse The HTTP response. + * @return A JSON representation of the object. + */ + public static JSONObject convertHTTPResponseToJSON(HttpResponse httpResponse) + { + String source = null; + try + { + source = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); + } + catch (IOException e) + { + throw new IllegalArgumentException("Could not extract JSON from HTTP response.", e); + } + LOGGER.info("Response body:\n{}", source); + return new JSONObject(source); + } +} 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 1d2feb9848..3f0f4c3920 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; @@ -80,6 +82,7 @@ public abstract class BaseAPI protected static final String RM_ACTIONS_API = "{0}rma/actions/ExecutionQueue"; protected static final String RM_SITE_ID = "rm"; protected static final String SHARE_ACTION_API = "{0}internal/shared/share/workspace/SpacesStore/{1}"; + private static final String SLINGSHOT_PREFIX = "alfresco/s/slingshot/"; @Autowired private AlfrescoHttpClientFactory alfrescoHttpClientFactory; @@ -339,28 +342,75 @@ public abstract class BaseAPI /** * Helper method for POST requests * - * @param adminUser user with administrative privileges - * @param adminPassword password for adminUser - * @param requestParams zero or more endpoint specific request parameters - * @param urlTemplate request URL template + * @param adminUser user with administrative privileges + * @param adminPassword password for adminUser + * @param expectedStatusCode The expected return status code. + * @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, + int expectedStatusCode, JSONObject requestParams, String urlTemplate, String... urlTemplateParams) { AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); - String requestUrl = MessageFormat.format( - urlTemplate, - client.getApiUrl(), - urlTemplateParams); - client.close(); + return doPostJsonRequest(adminUser, adminPassword, expectedStatusCode, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams); + } + /** + * Helper method for POST requests to slingshot. + * + * @param adminUser user with administrative privileges + * @param adminPassword password for adminUser + * @param expectedStatusCode The expected return status code. + * @param requestParams zero or more endpoint specific request parameters + * @param urlTemplate request URL template + * @param urlTemplateParams zero or more parameters used with urlTemplate + */ + protected HttpResponse doSlingshotPostJsonRequest(String adminUser, + String adminPassword, + int expectedStatusCode, + JSONObject requestParams, + String urlTemplate, + String... urlTemplateParams) + { + AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); + return doPostJsonRequest(adminUser, adminPassword, expectedStatusCode, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams); + } + + /** + * Helper method for POST requests + * + * @param adminUser user with administrative privileges + * @param adminPassword password for adminUser + * @param expectedStatusCode The expected return status code. + * @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 + * @throws AssertionError if the returned status code is not as expected. + */ + private HttpResponse doPostJsonRequest(String adminUser, + String adminPassword, + int expectedStatusCode, + String urlStart, + JSONObject requestParams, + String urlTemplate, + String... urlTemplateParams) + { + // Ensure the host is part of the request URL. + String requestUrl = MessageFormat.format( + urlTemplate, + urlStart, + urlTemplateParams); try { - return doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); + HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); + assertEquals("POST request to " + requestUrl + " was not successful.", httpResponse.getStatusLine().getStatusCode(), expectedStatusCode); + return httpResponse; } catch (InstantiationException | IllegalAccessException error) { @@ -462,7 +512,7 @@ public abstract class BaseAPI return returnValues; } - private boolean doRequestJson( + private HttpResponse doRequestJson( Class requestType, String requestUrl, String adminUser, @@ -482,7 +532,11 @@ public abstract class BaseAPI ((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(requestParams.toString())); } - return client.execute(adminUser, adminPassword, request).getStatusLine().getStatusCode() == HttpStatus.SC_OK; + LOGGER.info("Sending {} request to {}", requestType.getSimpleName(), requestUrl); + LOGGER.info("Request body: {}", requestParams); + HttpResponse httpResponse = client.execute(adminUser, adminPassword, request); + LOGGER.info("Response: {}", httpResponse.getStatusLine()); + return httpResponse; } catch (UnsupportedEncodingException | URISyntaxException error1) { @@ -497,7 +551,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 new file mode 100644 index 0000000000..f50e6ff4d3 --- /dev/null +++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/CopyToAPI.java @@ -0,0 +1,92 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.rest.v0; + +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.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * The v0 REST API for copy-to (which supports multi-item copy). + * + * @author Tom Page + * @since 2.6 + */ +@Component +public class CopyToAPI extends BaseAPI +{ + /** Logger for the class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(CopyToAPI.class); + /** The URI for the copy-to API. */ + private static final String COPY_TO_API = "{0}doclib/action/copy-to/node/{1}"; + + /** + * 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 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. + * @throws AssertionError If the API call didn't return a 200 response. + */ + public HttpResponse copyTo(String user, String password, String targetContainerPath, List nodeRefs) + { + return copyTo(user, password, 200, targetContainerPath, nodeRefs); + } + + /** + * 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 expectedStatusCode The expected return status code. + * @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. + * @throws AssertionError If the API didn't return the expected status code. + */ + public HttpResponse copyTo(String user, String password, int expectedStatusCode, String targetContainerPath, List nodeRefs) + { + JSONObject requestParams = new JSONObject(); + requestParams.put("nodeRefs", new JSONArray(nodeRefs)); + + return doSlingshotPostJsonRequest(user, password, expectedStatusCode, 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..d5805a9a3b 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,8 @@ package org.alfresco.rest.v0; import static org.alfresco.dataprep.AlfrescoHttpClient.MIME_TYPE_JSON; +import static org.apache.http.HttpStatus.SC_OK; +import static org.testng.AssertJUnit.assertNotNull; import java.io.IOException; import java.text.MessageFormat; @@ -206,9 +208,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 +222,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, SC_OK, requestParams, RM_ACTIONS_API); } /** @@ -288,37 +282,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, SC_OK, requestParams, CREATE_HOLDS_API); + assertNotNull("Expected object to have been created at " + fullHoldPath, + getObjectByPath(user, password, fullHoldPath)); + return httpResponse; } /** @@ -327,24 +316,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, SC_OK, 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..da327055e4 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 @@ -26,12 +26,14 @@ */ package org.alfresco.rest.v0; +import static org.apache.http.HttpStatus.SC_OK; + import java.text.MessageFormat; 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 +58,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, SC_OK, requestParams, RM_ACTIONS_API); } /** @@ -82,24 +77,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, SC_OK, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef)); } /** @@ -108,30 +96,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, SC_OK, 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..ad59119162 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 @@ -26,8 +26,11 @@ */ package org.alfresco.rest.v0; +import static org.apache.http.HttpStatus.SC_OK; + 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 +58,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 +70,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, SC_OK, 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..177f5a2372 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 @@ -26,14 +26,16 @@ */ package org.alfresco.rest.v0; +import static org.apache.http.HttpStatus.SC_OK; + 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 +43,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 +69,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, SC_OK, requestParams, ACTIONS_API); } /** @@ -92,28 +88,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, SC_OK, requestParams, RM_ACTIONS_API); } - /** * Reject the record given as parameter * @@ -121,28 +108,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 rejectRecord(user, password, SC_OK, recordName, reason); + } + + /** + * Reject the record given as parameter + * + * @param user the user declaring the document as record + * @param password the user's password + * @param expectedStatusCode The expected return status code. + * @param recordName the record name + * @param reason reject reason + * @return The HTTP Response. + * @throws AssertionError If the expectedStatusCode was not returned. + */ + public HttpResponse rejectRecord(String user, String password, int expectedStatusCode, 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, expectedStatusCode, requestParams, RM_ACTIONS_API); } + /** * Declare document version as record * @@ -150,29 +147,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, SC_OK, 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 +173,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 +192,7 @@ public class RecordsAPI extends BaseAPI if (record != null) { - return true; + return null; } // non-electronic properties String recordTitle = getPropertyValue(properties, RMProperty.TITLE); @@ -216,26 +207,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, SC_OK, requestParams, CREATE_NON_ELECTRONIC_RECORD_API); } /** @@ -351,24 +335,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, SC_OK, 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..5ff625c9d6 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 @@ -29,6 +29,8 @@ package org.alfresco.rest.v0; import static java.util.Arrays.asList; +import static org.apache.http.HttpStatus.SC_OK; + import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -37,6 +39,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 +66,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, SC_OK, 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 +312,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, SC_OK, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); } - catch (JSONException e) - { - return false; - } - return true; + return null; } /** @@ -333,22 +329,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, SC_OK, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); } - catch (JSONException e) - { - return false; - } - return true; + return null; } /**