use of org.apache.sling.commons.json.JSONException in project acs-aem-commons by Adobe-Consulting-Services.
the class WCMViewsServlet method doGet.
@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
if (WCMMode.DISABLED.equals(WCMMode.fromRequest(request))) {
response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
response.getWriter().write("");
return;
}
/* Valid WCMMode */
final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
final Page page = pageManager.getContainingPage(request.getResource());
final WCMViewsResourceVisitor visitor = new WCMViewsResourceVisitor();
visitor.accept(page.getContentResource());
final Set<String> viewSet = new HashSet<String>(visitor.getWCMViews());
// Get the Views provided by the Servlet
for (final Map.Entry<String, String[]> entry : this.defaultViews.entrySet()) {
if (StringUtils.startsWith(page.getPath(), entry.getKey())) {
viewSet.addAll(Arrays.asList(entry.getValue()));
}
}
final List<String> views = new ArrayList<String>(viewSet);
Collections.sort(views);
log.debug("Collected WCM Views {} for Page [ {} ]", views, page.getPath());
final JSONArray jsonArray = new JSONArray();
for (final String view : views) {
final JSONObject json = new JSONObject();
try {
json.put("title", StringUtils.capitalize(view) + " View");
json.put("value", view);
jsonArray.put(json);
} catch (JSONException e) {
log.error("Unable to build WCM Views JSON output.", e);
}
}
response.getWriter().write(jsonArray.toString());
}
use of org.apache.sling.commons.json.JSONException in project acs-aem-commons by Adobe-Consulting-Services.
the class QueryPackagerServletImpl method doPost.
@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
final ResourceResolver resourceResolver = request.getResourceResolver();
final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
log.debug("Preview mode: {}", preview);
final ValueMap properties = this.getProperties(request);
try {
final List<Resource> packageResources = queryHelper.findResources(resourceResolver, properties.get("queryLanguage", Query.JCR_SQL2), properties.get("query", String.class), properties.get("relPath", String.class));
final Map<String, String> packageDefinitionProperties = new HashMap<String, String>();
// ACL Handling
packageDefinitionProperties.put(JcrPackageDefinition.PN_AC_HANDLING, properties.get(PACKAGE_ACL_HANDLING, AccessControlHandling.OVERWRITE.toString()));
// Package Description
packageDefinitionProperties.put(JcrPackageDefinition.PN_DESCRIPTION, properties.get(PACKAGE_DESCRIPTION, DEFAULT_PACKAGE_DESCRIPTION));
if (preview) {
// Handle preview mode
response.getWriter().print(packageHelper.getPreviewJSON(packageResources));
} else if (packageResources == null || packageResources.isEmpty()) {
// Do not create empty packages; This will only clutter up CRX Package Manager
response.getWriter().print(packageHelper.getErrorJSON("Refusing to create a package with no filter " + "set rules."));
} else {
// Create JCR Package; Defaults should always be passed in via Request Parameters, but just in case
final JcrPackage jcrPackage = packageHelper.createPackage(packageResources, request.getResourceResolver().adaptTo(Session.class), properties.get(PACKAGE_GROUP_NAME, DEFAULT_PACKAGE_GROUP_NAME), properties.get(PACKAGE_NAME, DEFAULT_PACKAGE_NAME), properties.get(PACKAGE_VERSION, DEFAULT_PACKAGE_VERSION), PackageHelper.ConflictResolution.valueOf(properties.get(CONFLICT_RESOLUTION, PackageHelper.ConflictResolution.IncrementVersion.toString())), packageDefinitionProperties);
// Add thumbnail to the package definition
packageHelper.addThumbnail(jcrPackage, request.getResourceResolver().getResource(QUERY_PACKAGE_THUMBNAIL_RESOURCE_PATH));
log.debug("Successfully created JCR package");
response.getWriter().print(packageHelper.getSuccessJSON(jcrPackage));
}
} catch (RepositoryException ex) {
log.error("Repository error while creating Query Package", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (IOException ex) {
log.error("IO error while creating Query Package", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (JSONException ex) {
log.error("JSON error while creating Query Package response", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
}
}
use of org.apache.sling.commons.json.JSONException in project acs-aem-commons by Adobe-Consulting-Services.
the class LastModifiedOperationImpl method getLastModifiedQuery.
private List<Resource> getLastModifiedQuery(final ResourceResolver resourceResolver, final String userId, final String relativeDateRange, final String nodeType, final String dateProperty, final int limit) {
final List<Resource> resources = new ArrayList<Resource>();
final Map<String, String> map = new HashMap<String, String>();
map.put("path", "/content");
map.put("type", nodeType);
map.put("1_property", NameConstants.PN_PAGE_LAST_MOD_BY);
map.put("1_property.value", userId);
map.put("relativedaterange.property", dateProperty);
map.put("relativedaterange.lowerBound", relativeDateRange);
map.put("orderby", dateProperty);
map.put("orderby.sort", "desc");
map.put("p.limit", String.valueOf(limit));
map.put("p.guessTotal", "true");
try {
log.debug("Lastmod QueryBuilder Map: {}", new JSONObject(map).toString(2));
} catch (JSONException e) {
// no-op
}
final Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
final SearchResult result = query.getResult();
for (final Hit hit : result.getHits()) {
try {
resources.add(hit.getResource());
} catch (RepositoryException e) {
log.error("Error resolving Hit to Resource [ {} ]. " + "Likely issue with lucene index being out of sync.", hit.toString());
}
}
return resources;
}
use of org.apache.sling.commons.json.JSONException in project acs-aem-commons by Adobe-Consulting-Services.
the class ACLPackagerServletImpl method doPost.
@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
final ResourceResolver resourceResolver = request.getResourceResolver();
final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
log.trace("Preview mode: {}", preview);
final ValueMap properties = this.getProperties(request);
final String[] principalNames = properties.get(PRINCIPAL_NAMES, new String[] {});
final List<PathFilterSet> packageResources = this.findResources(resourceResolver, Arrays.asList(principalNames), toPatterns(Arrays.asList(properties.get(INCLUDE_PATTERNS, new String[] {}))));
try {
// Add Principals
if (properties.get(INCLUDE_PRINCIPALS, DEFAULT_INCLUDE_PRINCIPALS)) {
packageResources.addAll(this.getPrincipalResources(resourceResolver, principalNames));
}
doPackaging(request, response, preview, properties, packageResources);
} catch (RepositoryException ex) {
log.error(ex.getMessage());
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (IOException ex) {
log.error(ex.getMessage());
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (JSONException ex) {
log.error(ex.getMessage());
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
}
}
use of org.apache.sling.commons.json.JSONException in project acs-aem-commons by Adobe-Consulting-Services.
the class AuthorizablePackagerServletImpl method doPost.
@Override
public final void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
final ResourceResolver resourceResolver = request.getResourceResolver();
final boolean preview = Boolean.parseBoolean(request.getParameter("preview"));
log.debug("Preview mode: {}", preview);
final ValueMap properties = this.getProperties(request);
try {
final List<PathFilterSet> paths = this.findPaths(resourceResolver, properties.get("authorizableIds", new String[0]));
doPackaging(request, response, preview, properties, paths);
} catch (RepositoryException ex) {
log.error("Repository error while creating Query Package", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (IOException ex) {
log.error("IO error while creating Query Package", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
} catch (JSONException ex) {
log.error("JSON error while creating Query Package response", ex);
response.getWriter().print(packageHelper.getErrorJSON(ex.getMessage()));
}
}
Aggregations