RM-5837 Add support for v0 copy-to API.

This commit is contained in:
Tom Page
2017-11-06 16:33:11 +00:00
parent 01c52bc89b
commit ac9d586167
2 changed files with 130 additions and 6 deletions

View File

@@ -80,6 +80,7 @@ public abstract class BaseAPI
protected static final String RM_ACTIONS_API = "{0}rma/actions/ExecutionQueue"; protected static final String RM_ACTIONS_API = "{0}rma/actions/ExecutionQueue";
protected static final String RM_SITE_ID = "rm"; protected static final String RM_SITE_ID = "rm";
protected static final String SHARE_ACTION_API = "{0}internal/shared/share/workspace/SpacesStore/{1}"; protected static final String SHARE_ACTION_API = "{0}internal/shared/share/workspace/SpacesStore/{1}";
private static final String SLINGSHOT_PREFIX = "alfresco/s/slingshot/";
@Autowired @Autowired
private AlfrescoHttpClientFactory alfrescoHttpClientFactory; private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
@@ -352,12 +353,50 @@ public abstract class BaseAPI
String... urlTemplateParams) String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams);
}
/**
* Helper method for POST requests to slingshot.
*
* @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 urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/
protected boolean doSlingshotPostJsonRequest(String adminUser,
String adminPassword,
JSONObject requestParams,
String urlTemplate,
String... urlTemplateParams)
{
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams);
}
/**
* Helper method for POST requests
*
* @param adminUser user with administrative privileges
* @param adminPassword password for adminUser
* @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 <i>urlTemplate</i>
*/
private boolean doPostJsonRequest(String adminUser,
String adminPassword,
String urlStart,
JSONObject requestParams,
String urlTemplate,
String... urlTemplateParams)
{
// Ensure the host is part of the request URL.
String requestUrl = MessageFormat.format( String requestUrl = MessageFormat.format(
urlTemplate, urlTemplate,
client.getApiUrl(), urlStart,
urlTemplateParams); urlTemplateParams);
client.close();
try try
{ {
return doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); return doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
@@ -482,7 +521,11 @@ public abstract class BaseAPI
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(requestParams.toString())); ((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.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
} }
catch (UnsupportedEncodingException | URISyntaxException error1) catch (UnsupportedEncodingException | URISyntaxException error1)
{ {

View File

@@ -0,0 +1,81 @@
/*
* #%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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.v0;
import java.text.MessageFormat;
import java.util.List;
import org.alfresco.rest.core.v0.BaseAPI;
import org.json.JSONArray;
import org.json.JSONException;
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 true if the request was successful.
*/
public boolean copyTo(String user, String password, String targetContainerPath, List<String> nodeRefs)
{
try
{
JSONObject requestParams = new JSONObject();
requestParams.put("nodeRefs", new JSONArray(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;
}
}