CollectionPostServlet.java 4.99 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.ContentFragment
 *  com.adobe.cq.dam.cfm.ContentFragmentException
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.cfm.impl.servlets;

import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.impl.AssociatedContentException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(selectors={"cfm.coll", "cfm.coll"}, resourceTypes={"dam:Asset"}, extensions={"json"}, methods={"POST"})
public class CollectionPostServlet
extends SlingAllMethodsServlet {
    private final Logger log;

    public CollectionPostServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    private void addCollections(Resource resource, String[] collections) throws ContentFragmentException {
        ContentFragment cf = (ContentFragment)resource.adaptTo(ContentFragment.class);
        if (cf == null) {
            throw new AssociatedContentException("'" + resource.getPath() + "' is no valid content fragment.");
        }
        ResourceResolver resolver = resource.getResourceResolver();
        try {
            for (String collection : collections) {
                Resource collectionRsc = resolver.getResource(collection);
                if (collectionRsc == null) {
                    throw new AssociatedContentException("'" + collection + "' does not point to a valid resource.");
                }
                cf.addAssociatedContent(collectionRsc);
            }
            resolver.commit();
        }
        catch (PersistenceException pe) {
            throw new AssociatedContentException("Could not add collections", (Throwable)pe);
        }
        finally {
            resolver.refresh();
        }
    }

    private void removeCollection(Resource resource, String collection) throws ContentFragmentException {
        ContentFragment cf = (ContentFragment)resource.adaptTo(ContentFragment.class);
        if (cf == null) {
            throw new AssociatedContentException("'" + resource.getPath() + "' is no valid content fragment.");
        }
        ResourceResolver resolver = resource.getResourceResolver();
        Resource collectionRsc = resolver.getResource(collection);
        if (collectionRsc == null) {
            throw new AssociatedContentException("'" + collection + "' does not point to a valid resource.");
        }
        try {
            cf.removeAssociatedContent(collectionRsc);
            resolver.commit();
        }
        catch (PersistenceException pe) {
            throw new AssociatedContentException("Could not remove collection", (Throwable)pe);
        }
        finally {
            resolver.refresh();
        }
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Resource resource = request.getResource();
        String operation = request.getParameter(":operation");
        if (operation == null) {
            response.sendError(500, "Missing :operation parameter.");
            return;
        }
        try {
            if (operation.equals("add")) {
                String[] collections = request.getParameterValues("collection");
                if (collections == null) {
                    response.sendError(500, "Missing collection parameter(s).");
                    return;
                }
                this.addCollections(resource, collections);
            } else if (operation.equals("remove")) {
                String collection = request.getParameter("collection");
                this.removeCollection(resource, collection);
            }
        }
        catch (Exception e) {
            response.sendError(500, "Could not process collection request.");
            this.log.error("Could not process collection change", (Throwable)e);
            return;
        }
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println("{\"success\":true}");
    }
}