CopyRequest.java 3.36 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.PostResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.impl.servlet;

import com.adobe.granite.rest.RequestException;
import com.adobe.granite.rest.ResourceManager;
import com.adobe.granite.rest.impl.servlet.AbstractCopyMoveRequest;
import java.util.List;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CopyRequest
extends AbstractCopyMoveRequest {
    private Logger logger;

    public CopyRequest() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    protected void doHandle(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws UnsupportedOperationException, RequestException {
        Resource resource = request.getResource();
        int depth = this.getDepth(request);
        boolean overwrite = this.getOverwrite(request);
        String dstUri = this.getDestination(request);
        if (!dstUri.startsWith("/")) {
            dstUri = ResourceUtil.getParent((String)resource.getPath()) + "/" + dstUri;
        }
        dstUri = ResourceUtil.normalize((String)dstUri);
        if (!overwrite && this.getDestinationResource(request, dstUri) != null) {
            throw new RequestException(412, "Destination already exists");
        }
        if (ResourceUtil.isNonExistingResource((Resource)resource)) {
            throw new RequestException(404, "Missing source " + (Object)resource + " for COPY");
        }
        ResourceManager resourceMgr = (ResourceManager)request.adaptTo(ResourceManager.class);
        if (resourceMgr != null) {
            boolean created = resourceMgr.copy(resource, dstUri, depth);
            changes.add(Modification.onCopied((String)resource.getPath(), (String)dstUri));
            if (created) {
                response.setStatus(201, "Content created " + dstUri);
                response.setCreateRequest(true);
            } else {
                response.setStatus(204, "Content overwritten " + dstUri);
            }
        } else {
            this.logger.warn("SlingHttpServletRequest is not adaptable to ResourceManager");
        }
    }

    protected int getDepth(SlingHttpServletRequest request) throws RequestException {
        int depth = -1;
        String depthHeader = request.getHeader("X-Depth");
        if (depthHeader != null) {
            if (!"infinity".equalsIgnoreCase(depthHeader) && !"0".equals(depthHeader)) {
                throw new RequestException(412, "Request header 'X-Depth' must be either 'infinity' or '0'");
            }
            if ("0".equals(depthHeader)) {
                depth = 0;
            }
        }
        return depth;
    }

    protected boolean getOverwrite(SlingHttpServletRequest request) {
        return request.getHeader("X-Overwrite") == null ? true : !"F".equalsIgnoreCase(request.getHeader("X-Overwrite"));
    }
}