mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Initial work for ALF-4132 - create replication definition webscript
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@21574 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -21,8 +21,13 @@ package org.alfresco.repo.web.scripts.replication;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.action.ActionTrackingService;
|
||||
import org.alfresco.service.cmr.replication.ReplicationDefinition;
|
||||
import org.alfresco.service.cmr.replication.ReplicationService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
@@ -62,6 +67,41 @@ public abstract class AbstractReplicationWebscript extends DeclarativeWebScript
|
||||
return buildModel(modelBuilder, req, status, cache);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates properties on the definition, based on the JSON.
|
||||
* Doesn't save the definition
|
||||
* Doesn't change the name
|
||||
*/
|
||||
protected void updateDefinitionProperties(ReplicationDefinition replicationDefinition, JSONObject json)
|
||||
throws JSONException
|
||||
{
|
||||
if(json.has("description")) {
|
||||
String description = json.getString("description");
|
||||
replicationDefinition.setDescription(description);
|
||||
}
|
||||
|
||||
if(json.has("targetName")) {
|
||||
String targetName = json.getString("targetName");
|
||||
replicationDefinition.setTargetName(targetName);
|
||||
}
|
||||
|
||||
if(json.has("enabled")) {
|
||||
boolean enabled = json.getBoolean("enabled");
|
||||
replicationDefinition.setEnabled(enabled);
|
||||
}
|
||||
|
||||
if(json.has("payload")) {
|
||||
replicationDefinition.getPayload().clear();
|
||||
JSONArray payload = json.getJSONArray("payload");
|
||||
for(int i=0; i<payload.length(); i++) {
|
||||
String node = payload.getString(i);
|
||||
replicationDefinition.getPayload().add(
|
||||
new NodeRef(node)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Map<String, Object> buildModel(
|
||||
ReplicationModelBuilder modelBuilder,
|
||||
WebScriptRequest req,
|
||||
|
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2010 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
package org.alfresco.repo.web.scripts.replication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.replication.ReplicationDefinition;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
|
||||
/**
|
||||
* @author Nick Burch
|
||||
* @since 3.4
|
||||
*/
|
||||
public class ReplicationDefinitionsPost extends AbstractReplicationWebscript
|
||||
{
|
||||
@Override
|
||||
protected Map<String, Object> buildModel(ReplicationModelBuilder modelBuilder,
|
||||
WebScriptRequest req, Status status, Cache cache)
|
||||
{
|
||||
// Create our definition
|
||||
ReplicationDefinition replicationDefinition = null;
|
||||
JSONObject json;
|
||||
|
||||
try
|
||||
{
|
||||
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
|
||||
|
||||
// Check for the required parameters
|
||||
if(! json.has("name"))
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "name is required but wasn't supplied");
|
||||
if(! json.has("description"))
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "description is required but wasn't supplied");
|
||||
|
||||
// Create
|
||||
replicationDefinition = replicationService.createReplicationDefinition(
|
||||
json.getString("name"), json.getString("description")
|
||||
);
|
||||
|
||||
// Set the extra parts
|
||||
updateDefinitionProperties(replicationDefinition, json);
|
||||
|
||||
// Save the changes
|
||||
replicationService.saveReplicationDefinition(replicationDefinition);
|
||||
}
|
||||
catch (IOException iox)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from request.", iox);
|
||||
}
|
||||
catch (JSONException je)
|
||||
{
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from request.", je);
|
||||
}
|
||||
|
||||
// Return the details on it
|
||||
return modelBuilder.buildDetails(replicationDefinition);
|
||||
}
|
||||
}
|
@@ -41,6 +41,7 @@ import org.json.JSONObject;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
|
||||
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
|
||||
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
|
||||
|
||||
/**
|
||||
@@ -53,6 +54,8 @@ public class ReplicationRestApiTest extends BaseWebScriptTest
|
||||
private static final String URL_DEFINITIONS = "api/replication-definitions";
|
||||
private static final String URL_RUNNING_ACTION = "api/running-action/";
|
||||
|
||||
private static final String JSON = "application/json";
|
||||
|
||||
private static final String USER_NORMAL = "Normal" + GUID.generate();
|
||||
|
||||
private NodeService nodeService;
|
||||
@@ -619,6 +622,63 @@ public class ReplicationRestApiTest extends BaseWebScriptTest
|
||||
assertEquals(0, json.getJSONArray("payload").length());
|
||||
}
|
||||
|
||||
public void testReplicationDefinitionsPost() throws Exception
|
||||
{
|
||||
Response response;
|
||||
|
||||
|
||||
// Not allowed if you're not an admin
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName());
|
||||
response = sendRequest(new PostRequest(URL_DEFINITIONS, "", JSON), Status.STATUS_UNAUTHORIZED);
|
||||
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
|
||||
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL);
|
||||
response = sendRequest(new PostRequest(URL_DEFINITIONS, "", JSON), Status.STATUS_UNAUTHORIZED);
|
||||
assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus());
|
||||
|
||||
|
||||
// Ensure there aren't any to start with
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
|
||||
assertEquals(0, replicationService.loadReplicationDefinitions().size());
|
||||
|
||||
|
||||
// If you don't give it name + description, it won't like you
|
||||
JSONObject json = new JSONObject();
|
||||
response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST);
|
||||
assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
|
||||
|
||||
json.put("name", "NewDefinition");
|
||||
response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_BAD_REQUEST);
|
||||
assertEquals(Status.STATUS_BAD_REQUEST, response.getStatus());
|
||||
|
||||
|
||||
// If it has both, it'll work
|
||||
json.put("description", "Testing");
|
||||
response = sendRequest(new PostRequest(URL_DEFINITIONS, json.toString(), JSON), Status.STATUS_OK);
|
||||
assertEquals(Status.STATUS_OK, response.getStatus());
|
||||
|
||||
|
||||
// Check we got the right information back
|
||||
// TODO
|
||||
|
||||
|
||||
// Check that the right stuff ended up in the database
|
||||
// TODO
|
||||
|
||||
|
||||
// Post with the full set of options
|
||||
// TODO
|
||||
|
||||
|
||||
// Check the response for these
|
||||
// TODO
|
||||
|
||||
|
||||
// Check the database for these
|
||||
// TODO
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
|
Reference in New Issue
Block a user