use of org.apache.sling.commons.json.io.JSONWriter in project acs-aem-commons by Adobe-Consulting-Services.
the class DispatcherFlusherServlet method doPost.
@Override
@SuppressWarnings("squid:S3776")
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
final Resource resource = request.getResource();
final ResourceResolver resourceResolver = request.getResourceResolver();
final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
final Page currentPage = pageManager.getContainingPage(resource);
final ValueMap properties = resource.getValueMap();
/* Properties */
final String[] paths = properties.get("paths", new String[0]);
final ReplicationActionType replicationActionType = ReplicationActionType.valueOf(properties.get("replicationActionType", ReplicationActionType.ACTIVATE.name()));
final List<FlushResult> overallResults = new ArrayList<FlushResult>();
boolean caughtException = false;
ResourceResolver flushingResourceResolver = null;
try {
if (paths.length > 0) {
if (flushWithAdminResourceResolver) {
// Use the admin resource resolver for replication to ensure all
// replication permission checks are OK
// Make sure to close this resource resolver
flushingResourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
} else {
// Use the HTTP Request's resource resolver; don't close this resource resolver
flushingResourceResolver = resourceResolver;
}
final Map<Agent, ReplicationResult> results = dispatcherFlusher.flush(flushingResourceResolver, replicationActionType, true, paths);
for (final Map.Entry<Agent, ReplicationResult> entry : results.entrySet()) {
final Agent agent = entry.getKey();
final ReplicationResult result = entry.getValue();
overallResults.add(new FlushResult(agent, result));
}
}
} catch (ReplicationException e) {
log.error("Replication exception occurred during Dispatcher Flush request.", e);
caughtException = true;
} catch (LoginException e) {
log.error("Could not obtain an Admin Resource Resolver during Dispatcher Flush request.", e);
caughtException = true;
} finally {
if (flushWithAdminResourceResolver && flushingResourceResolver != null) {
// Close the admin resource resolver if opened by this servlet
flushingResourceResolver.close();
}
}
if (request.getRequestPathInfo().getExtension().equals("json")) {
response.setContentType("application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
try {
writer.object();
for (final FlushResult result : overallResults) {
writer.key(result.agentId);
writer.value(result.success);
}
writer.endObject();
} catch (JSONException e) {
throw new ServletException("Unable to output JSON data", e);
}
} else {
String suffix;
if (caughtException) {
suffix = "replication-error";
} else {
suffix = StringUtils.join(overallResults, '/');
}
response.sendRedirect(request.getContextPath() + currentPage.getPath() + ".html/" + suffix);
}
}
use of org.apache.sling.commons.json.io.JSONWriter in project acs-aem-commons by Adobe-Consulting-Services.
the class VanityDuplicateCheckServlet method doGet.
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
final ResourceResolver resolver = request.getResourceResolver();
final String vanityPath = request.getParameter("vanityPath");
final String pagePath = request.getParameter("pagePath");
log.debug("vanity path parameter passed is {}; page path parameter passed is {}", vanityPath, pagePath);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JSONWriter jsonWriter = new JSONWriter(response.getWriter());
jsonWriter.array();
if (StringUtils.isNotBlank(vanityPath)) {
String xpath = "//element(*)[" + NameConstants.PN_SLING_VANITY_PATH + "='" + vanityPath + "']";
@SuppressWarnings("deprecation") Iterator<Resource> resources = resolver.findResources(xpath, Query.XPATH);
while (resources.hasNext()) {
Resource resource = resources.next();
String path = resource.getPath();
if (path.startsWith("/content") && !path.equals(pagePath)) {
jsonWriter.value(path);
}
}
}
jsonWriter.endArray();
} catch (JSONException e) {
throw new ServletException("Unable to generate JSON result", e);
}
}
Aggregations