diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.desc.xml new file mode 100644 index 0000000000..eb1cc246f5 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.desc.xml @@ -0,0 +1,8 @@ + + Updates a persisted Replication Definition + Updates a previously persisted replication defintion with the supplied details + /api/replication-definition/{replication_definition_name} + + admin + required + diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.json.ftl new file mode 100644 index 0000000000..e92ce023ee --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definition.put.json.ftl @@ -0,0 +1,2 @@ +<#import "replication-definition.lib.ftl" as replicationDefLib /> +<@replicationDefLib.replicationDefinitionJSON replicationDefinition=replicationDefinition /> diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 6a4ebd1eac..824aefea1d 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -870,4 +870,10 @@ parent="abstractReplicationWebScript"> + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionGet.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionGet.java index f59475f76f..8fb8519573 100644 --- a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionGet.java +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionGet.java @@ -18,8 +18,6 @@ */ package org.alfresco.repo.web.scripts.replication; -import java.util.Comparator; -import java.util.List; import java.util.Map; import org.alfresco.service.cmr.replication.ReplicationDefinition; diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java new file mode 100644 index 0000000000..a5f8a03b8a --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionPut.java @@ -0,0 +1,93 @@ +/* + * 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 . + */ +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 ReplicationDefinitionPut extends AbstractReplicationWebscript +{ + @Override + protected Map buildModel(ReplicationModelBuilder modelBuilder, + WebScriptRequest req, Status status, Cache cache) + { + // Which definition did they ask for? + String replicationDefinitionName = + req.getServiceMatch().getTemplateVars().get("replication_definition_name"); + ReplicationDefinition replicationDefinition = + replicationService.loadReplicationDefinition(replicationDefinitionName); + + // Does it exist? + if(replicationDefinition == null) { + throw new WebScriptException( + Status.STATUS_NOT_FOUND, + "No Replication Definition found with that name" + ); + } + + // Grab the JSON, and prepare to update + try + { + JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent())); + + // Are they trying to rename? + if(json.has("name")) { + String jsonName = json.getString("name"); + if(! jsonName.equals(replicationDefinitionName)) { + // Name has changed, rename it + replicationService.renameReplicationDefinition( + replicationDefinitionName, + jsonName + ); + } + } + + // Update everything else + 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 new details on it + return modelBuilder.buildDetails(replicationDefinition); + } +} \ No newline at end of file diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java index 4462acf6b6..8520137ee1 100644 --- a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java @@ -42,6 +42,7 @@ 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.PutRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.Response; /** @@ -677,8 +678,66 @@ public class ReplicationRestApiTest extends BaseWebScriptTest // Check the database for these // TODO + // Ensure we can't create with a duplicate name + // TODO } + public void testReplicationDefinitionPut() throws Exception + { + Response response; + + + // Not allowed if you're not an admin + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getGuestUserName()); + response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_UNAUTHORIZED); + assertEquals(Status.STATUS_UNAUTHORIZED, response.getStatus()); + + AuthenticationUtil.setFullyAuthenticatedUser(USER_NORMAL); + response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", 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()); + + + // You need to specify a real definition + response = sendRequest(new PutRequest(URL_DEFINITION + "MadeUp", "", JSON), Status.STATUS_NOT_FOUND); + assertEquals(Status.STATUS_NOT_FOUND, response.getStatus()); + + + // Create one, and change it + ReplicationDefinition rd = replicationService.createReplicationDefinition("Test", "Testing"); + replicationService.saveReplicationDefinition(rd); + + response = sendRequest(new PutRequest(URL_DEFINITION + "Test", "{}", JSON), Status.STATUS_OK); + assertEquals(Status.STATUS_OK, response.getStatus()); + + + // Check we got the right information back on it + // TODO + + // Change some details, and see them updated in both + // the JSON and on the object in the repo + // TODO + + // Create a 2nd definition, and check that the correct + // one gets updated + // TODO + + // Change the payload, and see the right information in + // the response JSON for it + // TODO + + // Rename to a taken name, won't be allowed + // TODO + + // Rename to a spare name, will be changed + // TODO + } + + @Override protected void setUp() throws Exception {