Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C6DEE10D46 for ; Wed, 8 Jan 2014 12:14:06 +0000 (UTC) Received: (qmail 15067 invoked by uid 500); 8 Jan 2014 12:14:03 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 15010 invoked by uid 500); 8 Jan 2014 12:13:58 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 14997 invoked by uid 99); 8 Jan 2014 12:13:55 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jan 2014 12:13:55 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jan 2014 12:13:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 9645E23888D7; Wed, 8 Jan 2014 12:13:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1556512 - /sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java Date: Wed, 08 Jan 2014 12:13:33 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140108121333.9645E23888D7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bdelacretaz Date: Wed Jan 8 12:13:33 2014 New Revision: 1556512 URL: http://svn.apache.org/r1556512 Log: SLING-3300 - API to create replication agent (configuration) - adding missing servlet, contributed by Tommaso Teofili, thanks! Added: sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java Added: sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java?rev=1556512&view=auto ============================================================================== --- sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java (added) +++ sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/servlet/ReplicationAgentCreateServlet.java Wed Jan 8 12:13:33 2014 @@ -0,0 +1,66 @@ +package org.apache.sling.replication.servlet; + +import java.io.IOException; +import javax.servlet.Servlet; +import javax.servlet.ServletException; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Properties; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.SlingHttpServletResponse; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.servlets.SlingAllMethodsServlet; +import org.apache.sling.replication.agent.AgentConfigurationException; +import org.apache.sling.replication.agent.ReplicationAgentConfiguration; +import org.apache.sling.replication.agent.ReplicationAgentConfigurationManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Servlet to create {@link org.apache.sling.replication.agent.ReplicationAgent}s (via HTTP PUT). + */ +@SuppressWarnings("serial") +@Component(metatype = false) +@Service(value = Servlet.class) +@Properties({ + @Property(name = "sling.servlet.paths", value = "/system/replication/agent"), + @Property(name = "sling.servlet.methods", value = "POST")}) +public class ReplicationAgentCreateServlet extends SlingAllMethodsServlet { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + @Reference + private ReplicationAgentConfigurationManager agentConfigurationManager; + + @Override + protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) + throws ServletException, IOException { + response.setContentType("application/json"); + + String name = request.getParameter("name"); + + try { + agentConfigurationManager.createAgentConfiguration(request.getParameterMap()); + if (log.isInfoEnabled()) { + log.info("agent configuration for {} created", name); + } + + } catch (AgentConfigurationException e) { + if (log.isErrorEnabled()) { + log.error("cannot create agent {}", name, e); + } + } + Resource agentResource = request.getResource().getChild(name); + if (agentResource != null) { + Resource resource = agentResource.getChild("configuration"); + ReplicationAgentConfiguration configuration = resource + .adaptTo(ReplicationAgentConfiguration.class); + response.getWriter().write(configuration.toString()); + } else { + response.setStatus(404); + response.getWriter().write("the configuration was correctly created but the related agent cannot be found"); + } + } +}