S7DeliveryContentBuilder.java
7.96 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.AgentConfig
* com.day.cq.replication.ContentBuilder
* com.day.cq.replication.ReplicationAction
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationContent
* com.day.cq.replication.ReplicationContentFactory
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationLog
* com.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService
* com.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService$
* com.scene7.is.catalog.service.publish.atomic.PublishingService
* com.scene7.is.util.FileUtil
* com.scene7.is.util.IOUtil
* com.scene7.is.util.callbacks.Functions
* com.scene7.is.util.callbacks.Prox1
* com.scene7.is.util.collections.CollectionUtil
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
* scala.Function1
*/
package com.adobe.cq.dam.aod.replication;
import com.adobe.cq.dam.aod.replication.helpers.PublishingBuilder;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.ContentBuilder;
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.scene7.is.catalog.service.publish.atomic.BatchStreamingPublishingService;
import com.scene7.is.catalog.service.publish.atomic.PublishingService;
import com.scene7.is.util.FileUtil;
import com.scene7.is.util.IOUtil;
import com.scene7.is.util.callbacks.Functions;
import com.scene7.is.util.callbacks.Prox1;
import com.scene7.is.util.collections.CollectionUtil;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Function1;
@Component
@Service
public final class S7DeliveryContentBuilder
implements ContentBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(S7DeliveryContentBuilder.class);
@Reference
private final ResourceResolverFactory resolverFactory;
@Property(name="name", propertyPrivate=1)
private static final String NAME = "s7delivery";
public S7DeliveryContentBuilder() {
this(null);
}
S7DeliveryContentBuilder(ResourceResolverFactory resolverFactory) {
this.resolverFactory = resolverFactory;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory) throws ReplicationException {
ReplicationContent replicationContent;
if (action == null || contentFactory == null) {
throw new IllegalStateException("session: " + (Object)session + " action: " + (Object)action + " contentFactory: " + (Object)contentFactory);
}
S7DeliveryContentBuilder.info(action, "Building content");
AgentConfig config = action.getConfig();
File file = File.createTempFile("replicationContent", "dat");
try {
long startTime = System.currentTimeMillis();
DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
try {
ResourceResolver resolver = this.getResolver(config);
try {
S7DeliveryContentBuilder.buildContent(out, PublishingBuilder.publish(action, resolver));
}
finally {
resolver.close();
}
}
finally {
IOUtil.closeQuietly((DataOutputStream)out);
}
ReplicationContent rv = contentFactory.create("application/octet-stream", file, true);
S7DeliveryContentBuilder.logSuccess(action, startTime);
replicationContent = rv;
}
catch (Throwable var12_14) {
try {
FileUtil.deleteQuietly((File)file);
throw var12_14;
}
catch (Error | RuntimeException e) {
S7DeliveryContentBuilder.logError(action, e);
throw e;
}
catch (Exception e) {
S7DeliveryContentBuilder.logError(action, e);
throw new ReplicationException(e.getMessage(), e);
}
}
FileUtil.deleteQuietly((File)file);
return replicationContent;
}
public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory contentFactory, Map<String, Object> parameters) throws ReplicationException {
return this.create(session, action, contentFactory);
}
public String getName() {
return "s7delivery";
}
public String getTitle() {
return "S7 Delivery";
}
private ResourceResolver getResolver(AgentConfig config) throws ReplicationException {
try {
String userId = config.getAgentUserId();
Map map = CollectionUtil.map();
map.put("sling.service.subservice", "dmreplicateonmodify");
map.put("user.impersonation", userId);
return this.resolverFactory.getServiceResourceResolver(map);
}
catch (LoginException e) {
throw new ReplicationException(e.getMessage(), (Exception)e);
}
}
private static void buildContent(DataOutput out, Prox1<PublishingService, RepositoryException> code) {
BatchStreamingPublishingService..MODULE$.send(out, Functions.scalaFunc(code));
}
private static void info(ReplicationAction action, String message) {
String msg = String.format("%s for %s", message, S7DeliveryContentBuilder.toString(action));
action.getLog().info(msg);
LOGGER.info(msg);
}
private static void logSuccess(ReplicationAction action, long startTime) {
long elapsedTime = System.currentTimeMillis() - startTime;
String message = String.format("Building content succeeded in %d ms for %s", elapsedTime, S7DeliveryContentBuilder.toString(action));
action.getLog().info(message);
LOGGER.info(message);
}
private static void logError(ReplicationAction action, Throwable e) {
String message = String.format("Content building failed for %s. %s: '%s'", S7DeliveryContentBuilder.toString(action), e.getClass().getName(), e.getMessage());
action.getLog().error(message);
LOGGER.error(message, e);
}
private static String toString(ReplicationAction action) {
return (Object)action.getType() + ": " + CollectionUtil.mkString((Object[])action.getPaths(), (String)"[", (String)", ", (String)"]");
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
}