CopyRequest.java
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* 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"));
}
}