Search in sources :

Example 11 with JSONException

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());
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.apache.sling.commons.json.JSONArray) JSONException(org.apache.sling.commons.json.JSONException) Page(com.day.cq.wcm.api.Page) PageManager(com.day.cq.wcm.api.PageManager) JSONObject(org.apache.sling.commons.json.JSONObject) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 12 with JSONException

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()));
    }
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) JcrPackage(org.apache.jackrabbit.vault.packaging.JcrPackage) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 13 with JSONException

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;
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) SearchResult(com.day.cq.search.result.SearchResult) RepositoryException(javax.jcr.RepositoryException) Hit(com.day.cq.search.result.Hit) JSONObject(org.apache.sling.commons.json.JSONObject) Session(javax.jcr.Session)

Example 14 with JSONException

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()));
    }
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 15 with JSONException

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()));
    }
}
Also used : PathFilterSet(org.apache.jackrabbit.vault.fs.api.PathFilterSet) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Aggregations

JSONException (org.apache.sling.commons.json.JSONException)25 JSONObject (org.apache.sling.commons.json.JSONObject)15 Resource (org.apache.sling.api.resource.Resource)10 ServletException (javax.servlet.ServletException)9 RepositoryException (javax.jcr.RepositoryException)6 ValueMap (org.apache.sling.api.resource.ValueMap)6 HashMap (java.util.HashMap)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 Map (java.util.Map)3 Page (com.day.cq.wcm.api.Page)2 PageManager (com.day.cq.wcm.api.PageManager)2 PathFilterSet (org.apache.jackrabbit.vault.fs.api.PathFilterSet)2 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)2 JSONArray (org.apache.sling.commons.json.JSONArray)2 JSONWriter (org.apache.sling.commons.json.io.JSONWriter)2 ActionManager (com.adobe.acs.commons.fam.ActionManager)1 FormImpl (com.adobe.acs.commons.forms.impl.FormImpl)1