我正在尝试为CQ5.6编写自定义文件上传组件,但我遇到了反向复制的问题。节点在“发布”实例中创建,但未复制到“作者”实例。复制器调用后,下一行出现在error.log中:
com.day.cq.replication.impl.ReplicatorImpl Replication triggered, but no agent found or selected.
已启用复制代理。在其他情况下,用户表单,例如,复制工作成功,所以我认为问题是在我的代码中的某个地方。我使用的代码是:
Node node = session.getNode(path);
ValueFactory valueFactory = session.getValueFactory();
Binary contentValue = valueFactory.createBinary(is);
Node parent = node.addNode(fileName, "nt:unstructured");
parent.setProperty(DELETED, false);
parent.setProperty(DESCRIPTION, description);
Node fileNode = parent.addNode(fileName, "nt:file");
fileNode.addMixin("mix:referenceable");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty(Property.JCR_DATA, contentValue);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(lastModified.getTimeInMillis());
resNode.setProperty(Property.JCR_LAST_MODIFIED, lastModified);
parent.setProperty("cq:distribute", true);
parent.setProperty("cq:lastModified", Calendar.getInstance());
parent.setProperty("cq:lastModifiedBy", session.getUserID());
session.save();
replicator.replicate(session, ReplicationActionType.ACTIVATE, parent.getPath());
session.logout();
如何使反向复制适用于我在servlet中创建的节点?
更新 根据TomekRękawek的回答,我更新了我的代码,但问题仍未解决。这是新代码:
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
Session session = resourceResolver.adaptTo(Session.class);
String path = (String) componentContext.getProperties().get(SAVEPATH);
Node node = session.getNode(path);
ValueFactory valueFactory = session.getValueFactory();
Binary contentValue = valueFactory.createBinary(is);
Node parent = node.addNode(fileName, "cq:Page");
Node jcrContent = parent.addNode("jcr:content", "cq:PageContent");
jcrContent.setProperty("cq:distribute", true);
jcrContent.setProperty("cq:lastModified", Calendar.getInstance());
jcrContent.setProperty("cq:lastModifiedBy", session.getUserID());
Node fileNode = jcrContent.addNode(fileName, "nt:file");
fileNode.addMixin("mix:referenceable");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty(Property.JCR_DATA, contentValue);
session.save();
session.logout();
答案 0 :(得分:4)
反向复制是由作者实例执行的操作,而不是发布。负责此操作的代理是作者上的反向复制代理。它每30秒连接一次发布,并收集cq:distribute
属性集的页面节点。
为了反转复制您需要的图像:
cq:Page
节点cq:PageContent
节点,并将其命名为jcr:content
。jcr:content
下创建您的图片节点并保存您的会话 [已编辑] cq:distribute
节点上设置cq:lastModified
,cq:lastModifiedBy
和jcr:content
属性。创建页面换行输入流并对其进行反向复制的示例方法:
private void reverseReplicateBinary(Session session, String parentPath, String name, InputStream is)
throws RepositoryException {
ValueFactory valueFactory = session.getValueFactory();
Node parent = session.getNode(parentPath);
Node page = JcrUtils.getOrCreateUniqueByPath(parent, name, "cq:Page");
Node jcrContent = page.addNode(Property.JCR_CONTENT, "cq:PageContent");
Node file = jcrContent.addNode("file", "nt:file");
Node resource = file.addNode(Property.JCR_CONTENT, "nt:resource");
resource.setProperty(Property.JCR_DATA, valueFactory.createBinary(is));
session.save();
jcrContent.setProperty("cq:lastModified", Calendar.getInstance());
jcrContent.setProperty("cq:lastModifiedBy", session.getUserID());
jcrContent.setProperty("cq:distribute", false);
session.save();
}
完整的示例可以在the gist找到。
这就是全部。您无需手动调用复制器,作者实例将自动收集页面。
答案 1 :(得分:0)
对于您的复制器对象,请确保将代理ID设置为“发件箱”
AgentIdFilter filter = new AgentIdFilter("outbox");
ReplicationOptions opts = new ReplicationOptions();
opts.setFilter(filter);
replicator.replicate(session, ReplicationActionType.ACTIVATE, parent.getPath(), opts);