From a343197d6880711ffa60effb8f2ae548f2a6766c Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 3 Aug 2010 15:12:35 +0000 Subject: [PATCH] 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 --- .../replication-definitions.post.desc.xml | 8 ++ .../replication-definitions.post.json.ftl | 2 + .../web-scripts-application-context.xml | 6 ++ .../AbstractReplicationWebscript.java | 40 +++++++++ .../ReplicationDefinitionsPost.java | 81 +++++++++++++++++++ .../replication/ReplicationRestApiTest.java | 60 ++++++++++++++ 6 files changed, 197 insertions(+) create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.json.ftl create mode 100644 source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsPost.java diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.desc.xml new file mode 100644 index 0000000000..6deb62c2d9 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.desc.xml @@ -0,0 +1,8 @@ + + Create a new persisted Replication Definition + Creates a new replication definition with the supplied details + /api/replication-definitions + + admin + required + diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.json.ftl new file mode 100644 index 0000000000..e92ce023ee --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/replication/replication-definitions.post.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 c0edbc988d..6a4ebd1eac 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -858,6 +858,12 @@ parent="abstractReplicationWebScript"> + + + + buildModel( ReplicationModelBuilder modelBuilder, WebScriptRequest req, diff --git a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsPost.java b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsPost.java new file mode 100644 index 0000000000..ea8ac3d41b --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationDefinitionsPost.java @@ -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 . + */ +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 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); + } +} \ 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 eaade592f2..4462acf6b6 100644 --- a/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/replication/ReplicationRestApiTest.java @@ -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 {