Author: tommaso
Date: Thu Sep 18 08:37:02 2014
New Revision: 1625912
URL: http://svn.apache.org/r1625912
Log:
SLING-3951 - add a repository package importer
Added:
sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java
(with props)
Added:
sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java?rev=1625912&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java
(added)
+++
sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java
Thu Sep 18 08:37:02 2014
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.replication.packaging.impl.importer;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Properties;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.util.Text;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.replication.communication.ReplicationEndpoint;
+import org.apache.sling.replication.event.ReplicationEventFactory;
+import org.apache.sling.replication.event.ReplicationEventType;
+import org.apache.sling.replication.packaging.ReplicationPackage;
+import org.apache.sling.replication.packaging.ReplicationPackageImporter;
+import
org.apache.sling.replication.serialization.ReplicationPackageReadingException;
+import
org.apache.sling.replication.transport.authentication.TransportAuthenticationContext;
+import
org.apache.sling.replication.transport.authentication.TransportAuthenticationProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link ReplicationPackageImporter} importing
+ * {@link ReplicationPackage} stream + type into an underlying JCR repository.
+ */
+@Component(label = "Repository Replication Package Importer")
+@Service(value = ReplicationPackageImporter.class)
+@Property(name = "name", value = RepositoryReplicationPackageImporter.NAME)
+public class RepositoryReplicationPackageImporter implements
ReplicationPackageImporter {
+
+ static final String NAME = "repository";
+
+ private static final String REPO_SCHEME = "repo";
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference
+ private SlingRepository repository;
+
+ @Reference
+ private ReplicationEventFactory replicationEventFactory;
+
+ @Reference
+ private TransportAuthenticationProvider<SlingRepository, Session>
transportAuthenticationProvider;
+
+ public void deliverPackageToEndpoint(ReplicationPackage
replicationPackage, ReplicationEndpoint replicationEndpoint)
+ throws Exception {
+
+ Session session = null;
+ try {
+ TransportAuthenticationContext transportAuthenticationContext =
new TransportAuthenticationContext();
+ String path =
replicationEndpoint.getUri().toString().replace("repo:/", "");
+ transportAuthenticationContext.addAttribute("path", path);
+ session = transportAuthenticationProvider.authenticate(repository,
transportAuthenticationContext);
+ int lastSlash = replicationPackage.getId().lastIndexOf('/');
+ String nodeName = Text.escape(lastSlash < 0 ?
replicationPackage.getId() : replicationPackage.getId().substring(lastSlash +
1));
+ log.info("creating node {} in {}", replicationPackage.getId(),
nodeName);
+
+ if (session != null) {
+ Node addedNode = session.getNode(path).addNode(nodeName,
+ NodeType.NT_FILE);
+ Node contentNode = addedNode.addNode(JcrConstants.JCR_CONTENT,
NodeType.NT_RESOURCE);
+ if (contentNode != null) {
+ InputStream inputStream = null;
+ try {
+ inputStream = replicationPackage.createInputStream();
+ contentNode.setProperty(JcrConstants.JCR_DATA,
session.getValueFactory().createBinary(inputStream));
+ contentNode.setProperty("package.type",
replicationPackage.getType());
+ session.save();
+ } finally {
+ IOUtils.closeQuietly(inputStream);
+ }
+ }
+ log.info("package {} imported into the repository as node {} ",
+ replicationPackage.getId(), addedNode.getPath());
+
+ Dictionary<Object, Object> props = new Properties();
+ props.put("path", replicationPackage.getPaths());
+
replicationEventFactory.generateEvent(ReplicationEventType.PACKAGE_REPLICATED,
props);
+ } else {
+ throw new Exception("could not get a Session to deliver
package to the repository");
+ }
+ } finally {
+ if (session != null) {
+ session.logout();
+ }
+ }
+ }
+
+ public boolean importPackage(ReplicationPackage replicationPackage) throws
ReplicationPackageReadingException {
+ return false;
+ }
+
+ public ReplicationPackage readPackage(InputStream stream) throws
ReplicationPackageReadingException {
+ return null;
+ }
+}
Propchange:
sling/trunk/contrib/extensions/replication/core/src/main/java/org/apache/sling/replication/packaging/impl/importer/RepositoryReplicationPackageImporter.java
------------------------------------------------------------------------------
svn:eol-style = native