ReplicationServlet.java
5.73 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.replication.ReplicationActionType
* com.day.cq.replication.ReplicationException
* com.day.cq.replication.ReplicationStatus
* com.day.cq.replication.Replicator
* javax.jcr.Session
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringUtils
* org.apache.commons.lang3.time.DateFormatUtils
* org.apache.commons.lang3.time.FastDateFormat
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.slf4j.Logger
*/
package com.day.crx.delite.impl.servlets;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Calendar;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
public class ReplicationServlet
extends AbstractServlet {
private Replicator replicator;
public ReplicationServlet(BundleContext bc) {
super(bc);
}
protected void doService(HttpServletRequest req, HttpServletResponse res, Session session) throws ServletException, IOException {
this.initReplicator();
if (req.getMethod().equals("GET")) {
this.doGet(req, res, session);
return;
}
if (req.getMethod().equals("POST")) {
String action = req.getParameter("action");
if ("replicate".equals(action)) {
this.replicate(req, res, session);
} else if ("replicatedelete".equals(action)) {
this.replicatedelete(req, res, session);
}
return;
}
res.sendError(405);
}
private void initReplicator() {
if (this.replicator == null) {
this.replicator = (Replicator)this.bundleContext.getService(this.bundleContext.getServiceReference(Replicator.class.getName()));
}
}
private void doGet(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
RequestData req = new RequestData(request);
String path = req.getParameter("path");
if (StringUtils.isBlank((CharSequence)path)) {
res.sendError(404);
return;
}
try {
ReplicationStatus status = this.replicator.getReplicationStatus(session, path);
if (status == null) {
res.sendError(404);
return;
}
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
JSONWriter writer = new JSONWriter((Writer)res.getWriter());
writer.object();
writer.key("isActivated").value(status.isActivated());
writer.key("isDeactivated").value(status.isDeactivated());
writer.key("isDelivered").value(status.isDelivered());
writer.key("isPending").value(status.isPending());
writer.key("lastPublished").value((Object)this.format(status.getLastPublished()));
writer.key("lastPublishedBy").value((Object)status.getLastPublishedBy());
writer.key("lastReplicationAction").value((Object)status.getLastReplicationAction());
writer.endObject();
}
catch (JSONException e) {
this.logger.error("Error occur getting replication status: " + path, (Throwable)e);
res.sendError(500, e.getMessage());
}
}
private String format(Calendar calendar) {
if (calendar == null) {
return null;
}
return DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(calendar);
}
private void replicate(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
RequestData req = new RequestData(request);
String path = req.getParameter("path");
if (StringUtils.isBlank((CharSequence)path)) {
res.sendError(404);
return;
}
try {
this.replicator.replicate(session, ReplicationActionType.ACTIVATE, path);
}
catch (ReplicationException e) {
this.logger.error("Error occur replicating (activate): " + path, (Throwable)e);
res.sendError(500, e.getMessage());
}
}
private void replicatedelete(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
RequestData req = new RequestData(request);
String path = req.getParameter("path");
if (StringUtils.isBlank((CharSequence)path)) {
res.sendError(404);
return;
}
try {
this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
}
catch (ReplicationException e) {
this.logger.error("Error occur replicating (deactivate): " + path, (Throwable)e);
res.sendError(500, e.getMessage());
}
}
}