MoveRequest.java 4.09 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.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.SyntheticResource
 *  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.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MoveRequest
extends AbstractCopyMoveRequest {
    private Logger logger;

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

    @Override
    protected void doHandle(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws UnsupportedOperationException, RequestException, Exception {
        try {
            Resource resource = request.getResource();
            this.getDepth(request);
            String overwrite = this.getOverwrite(request);
            String dstUri = this.getDestination(request);
            if (!dstUri.startsWith("/")) {
                dstUri = ResourceUtil.getParent((String)resource.getPath()) + "/" + dstUri;
            }
            dstUri = ResourceUtil.normalize((String)dstUri);
            Resource dstRes = this.getDestinationResource(request, dstUri);
            if ("T".equals(overwrite) && dstRes != null) {
                request.getResourceResolver().delete(dstRes);
            }
            if ("F".equals(overwrite) && dstRes != null) {
                throw new RequestException(412, "Destination already exists");
            }
            if (resource instanceof SyntheticResource) {
                throw new RequestException(404, "Missing source " + (Object)resource + " for COPY");
            }
            ResourceManager resourceMgr = (ResourceManager)request.adaptTo(ResourceManager.class);
            if (resourceMgr != null) {
                boolean created = resourceMgr.move(resource, dstUri);
                changes.add(Modification.onMoved((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");
            }
        }
        catch (UnsupportedOperationException e) {
            throw new RequestException(403, "Source does not support copy");
        }
    }

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

    protected String getOverwrite(SlingHttpServletRequest request) throws RequestException {
        String headerValue = request.getHeader("X-Overwrite");
        if (headerValue != null) {
            if ("FT".indexOf(headerValue.toUpperCase().trim()) > -1) {
                return headerValue;
            }
            throw new RequestException(412, "Request header 'X-Overwrite' value '" + headerValue + "' unsupported");
        }
        return null;
    }
}