StaticTransportHandler.java
7.02 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.io.FileUtils
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl.transport;
import com.day.cq.replication.AgentConfig;
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.ReplicationLog;
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.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={TransportHandler.class})
public class StaticTransportHandler
implements TransportHandler {
private final Logger logger;
private static final String TRANSPORT_SCHEME = "static";
private static final String PN_DIRECTORY = "directory";
private static final String MT_ZIP = "application/zip";
public StaticTransportHandler() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Activate
private void activate(ComponentContext context) {
this.logger.info("Static Transport Handler started.");
}
@Deactivate
private void deactivate(ComponentContext context) {
this.logger.info("Static Transport Handler stopped.");
}
@Override
public boolean canHandle(AgentConfig config) {
if (config != null) {
String uri = config.getTransportURI();
String directory = (String)config.getProperties().get("directory", String.class);
if (directory != null) {
directory = ResourceUtil.normalize((String)directory);
}
return uri.startsWith("static") && directory != null && new File(directory).isAbsolute();
}
return false;
}
@Override
public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx) throws ReplicationException {
URI transportUri;
ReplicationLog log = tx.getLog();
String s = ctx.getConfig().getTransportURI();
try {
transportUri = new URI(s);
}
catch (URISyntaxException e) {
String msg = String.format("Syntax error in Transport URI %s: %s", s, e.getMessage());
log.error(msg);
return new ReplicationResult(false, 0, msg);
}
if (!transportUri.getScheme().equals("static")) {
String msg = String.format("Unexpected scheme in Transport URI %s: should be %s", s, "static");
log.error(msg);
return new ReplicationResult(false, 0, msg);
}
s = ResourceUtil.normalize((String)((String)ctx.getConfig().getProperties().get("directory", String.class)));
if (s == null) {
String msg = "Target directory not specified.";
log.error(msg);
return new ReplicationResult(false, 0, msg);
}
if (!new File(s).isAbsolute()) {
String msg = "Target directory must be absolute.";
log.error(msg);
return new ReplicationResult(false, 0, msg);
}
File directory = new File(s);
if (!directory.isDirectory()) {
String msg = String.format("Target is not a directory: %s", s);
log.error(msg);
return new ReplicationResult(false, 0, msg);
}
if (tx.getAction().getType() == ReplicationActionType.DELETE || tx.getAction().getType() == ReplicationActionType.DEACTIVATE) {
return ReplicationResult.OK;
}
ReplicationContent content = tx.getContent();
String contentType = content.getContentType();
if (tx.getAction().getType() == ReplicationActionType.TEST) {
throw new ReplicationException("Test replication not supported by this transport handler.");
}
if (tx.getAction().getType() == ReplicationActionType.INTERNAL_POLL || tx.getAction().getType() == ReplicationActionType.REVERSE) {
throw new ReplicationException("Reverse replication not supported by this transport handler.");
}
if (content == ReplicationContent.VOID) {
log.info("Nothing to replicate for " + tx.getAction().getPath());
return new ReplicationResult(true, 0, "Done");
}
if (!contentType.equals("application/zip")) {
String msg = String.format("Unexpected replication content type: %s", contentType);
throw new ReplicationException(msg);
}
ZipInputStream in = null;
try {
InputStream input = content.getInputStream();
if (input != null) {
in = new ZipInputStream(input);
this.extractZip(directory, in, log);
}
}
catch (IOException e) {
String msg = String.format("Unable to deserialize replication content: %s", e.getMessage());
throw new ReplicationException(msg, e);
}
finally {
IOUtils.closeQuietly((InputStream)in);
}
return ReplicationResult.OK;
}
private void extractZip(File directory, ZipInputStream in, ReplicationLog log) throws IOException {
ZipEntry entry;
while ((entry = in.getNextEntry()) != null) {
String name = entry.getName();
if (name.startsWith("/")) {
name = name.substring(1);
}
File target = new File(directory, name);
log.debug("Extracting entry %s to %s", entry.getName(), target.getPath());
FileUtils.writeByteArrayToFile((File)target, (byte[])IOUtils.toByteArray((InputStream)in));
}
}
public ReplicationResult poll(TransportContext ctx, ReplicationTransaction tx, List<ReplicationContent> result, ReplicationContentFactory factory) throws ReplicationException {
throw new ReplicationException("Not implemented");
}
}