RepositoryTransportHandler.java
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl.transport;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.Outbox;
import com.day.cq.replication.OutboxManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationResult;
import com.day.cq.replication.ReplicationTransaction;
import com.day.cq.replication.TransportContext;
import com.day.cq.replication.TransportHandler;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={TransportHandler.class})
public class RepositoryTransportHandler
implements TransportHandler {
private static final Logger log = LoggerFactory.getLogger(RepositoryTransportHandler.class);
private static final String URI_PREFIX = "repo://";
@Reference
protected SlingRepository repository;
@Reference
protected OutboxManager outboxManager;
@Override
public boolean canHandle(AgentConfig config) {
String uri = config == null ? null : config.getTransportURI();
return uri != null && uri.startsWith("repo://");
}
@Override
public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx) throws ReplicationException {
block16 : {
ReplicationContent rc = tx.getContent();
if (rc == null) {
return ReplicationResult.OK;
}
String uri = ctx.getConfig().getTransportURI();
String path = uri.substring("repo://".length() - 1);
Session systemSession = null;
try {
Outbox outbox;
systemSession = this.repository.loginService("replicationService", null);
if ("/var/replication/outbox".equals(path)) {
outbox = this.outboxManager.getDefaultOutbox(systemSession);
} else if (path.startsWith("/var/replication/outboxes")) {
String name = path.substring("/var/replication/outboxes".length() + 1);
outbox = this.outboxManager.getOutbox(systemSession, name, true);
if (outbox == null) {
throw new ReplicationException("Unable to retrieve outbox at " + path);
}
} else {
log.error("Invalid outbox path: {}. Either /var/replication/outbox or below /var/replication/outboxes", (Object)path);
throw new ReplicationException("Unsupported outbox path: " + path);
}
log.info("using outbox for repository transport: {}", (Object)outbox.getPath());
InputStream in = rc.getInputStream();
if (in != null) {
outbox.put(tx.getAction(), in);
break block16;
}
if (tx.getAction().getType() == ReplicationActionType.ACTIVATE) {
ReplicationResult replicationResult = ReplicationResult.OK;
return replicationResult;
}
outbox.put(tx.getAction());
}
catch (IOException e) {
String msg = "Unable to get content input stream.";
throw new ReplicationException(msg, e);
}
catch (RepositoryException e) {
throw new ReplicationException("Error while accessing repository", (Exception)e);
}
finally {
if (systemSession != null) {
systemSession.logout();
}
}
}
return ReplicationResult.OK;
}
public ReplicationResult poll(TransportContext ctx, ReplicationTransaction tx, List<ReplicationContent> result, ReplicationContentFactory factory) throws ReplicationException {
String msg = "Unsupported operation.";
throw new ReplicationException(msg);
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindOutboxManager(OutboxManager outboxManager) {
this.outboxManager = outboxManager;
}
protected void unbindOutboxManager(OutboxManager outboxManager) {
if (this.outboxManager == outboxManager) {
this.outboxManager = null;
}
}
}