DeleteRequest.java 2.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.PostResponse
 */
package com.adobe.granite.rest.impl.servlet;

import com.adobe.granite.rest.RequestException;
import com.adobe.granite.rest.impl.servlet.AbstractRequest;
import java.util.List;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.PostResponse;

public class DeleteRequest
extends AbstractRequest {
    @Override
    protected void doHandle(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws UnsupportedOperationException, RequestException, Exception {
        String notExists = null;
        try {
            RequestPathInfo rpi = request.getRequestPathInfo();
            Resource resource = request.getResource();
            if (ResourceUtil.isNonExistingResource((Resource)resource)) {
                notExists = rpi.getResourcePath();
            } else {
                String resourcePath = resource.getPath();
                resource.getResourceResolver().delete(resource);
                changes.add(Modification.onDeleted((String)resourcePath));
            }
        }
        catch (UnsupportedOperationException e) {
            throw new RequestException(403, "Source does not support deletion");
        }
        catch (Exception e) {
            throw new RequestException(500, e.getMessage());
        }
        if (notExists != null) {
            throw new RequestException(404, "No resource found at path " + notExists);
        }
    }
}