OverlayNodeServlet.java
6.18 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeType
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringEscapeUtils
* org.apache.commons.lang3.StringUtils
* org.apache.jackrabbit.commons.JcrUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.osgi.framework.BundleContext
* org.slf4j.Logger
*/
package com.day.crx.delite.impl.servlets;
import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
public class OverlayNodeServlet
extends AbstractServlet {
private static final String PARAM_PATH = "path";
private static final String PARAM_OVERLAY_LOCATION = "overlayLocation";
private static final String PARAM_MATCH_TYPE = "matchType";
public OverlayNodeServlet(BundleContext bc) {
super(bc);
}
protected void doService(HttpServletRequest request, HttpServletResponse response, Session session) throws ServletException, IOException {
block10 : {
RequestData requestData = new RequestData(request);
ResourceResolver resolver = (ResourceResolver)request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
String path = requestData.getParameter("path");
if (StringUtils.isBlank((CharSequence)path)) {
response.sendError(404);
return;
}
Resource resource = resolver.getResource(path);
if (resource == null) {
response.sendError(404);
return;
}
String overlayLocation = requestData.getParameter("overlayLocation");
if (StringUtils.isBlank((CharSequence)overlayLocation)) {
response.sendError(404);
return;
}
boolean matchType = "true".equals(requestData.getParameter("matchType"));
try {
try {
Node overlayNode = null;
ArrayList<Resource> intermediateResources = new ArrayList<Resource>();
String currentSearchPath = this.getCurrentSearchPath(resolver, resource.getPath());
if (currentSearchPath != null) {
writer.object();
Resource resourceToOverlay = resource;
this.logger.debug("Node to overlay : {}", (Object)resourceToOverlay.getPath());
this.logger.debug("Overlay location : {}", (Object)overlayLocation);
this.logger.debug("Current search path: {}", (Object)currentSearchPath);
String overlayPath = this.getOverlayPath(resourceToOverlay.getPath(), currentSearchPath, overlayLocation);
writer.key("overlayPath").value((Object)overlayPath);
while (resolver.getResource(overlayPath) == null) {
intermediateResources.add(0, resourceToOverlay);
resourceToOverlay = resourceToOverlay.getParent();
overlayPath = this.getOverlayPath(resourceToOverlay.getPath(), currentSearchPath, overlayLocation);
}
for (Resource currentResource : intermediateResources) {
overlayNode = JcrUtils.getOrCreateByPath((String)currentResource.getPath().replaceFirst(currentSearchPath, overlayLocation), (String)(matchType ? ((Node)currentResource.adaptTo(Node.class)).getPrimaryNodeType().getName() : "nt:unstructured"), (Session)session);
this.logger.debug("Created overlay : {}", (Object)overlayNode.getPath());
}
writer.key("success").value(true);
writer.endObject();
session.save();
break block10;
}
response.sendError(404);
}
catch (RepositoryException e) {
writer.object();
writer.key("success").value(false);
writer.key("message").value((Object)StringEscapeUtils.escapeHtml4((String)e.getMessage()));
writer.endObject();
}
}
catch (JSONException e) {
this.logger.error("Unable to write response", (Throwable)e);
response.sendError(500, "Unable to send status while overlaying node");
}
}
}
private String getCurrentSearchPath(ResourceResolver resolver, String path) {
String currentSearchPath = null;
for (String searchPath : resolver.getSearchPath()) {
if (!path.startsWith(searchPath)) continue;
currentSearchPath = searchPath;
}
return currentSearchPath;
}
private String getOverlayPath(String path, String currentSearchPath, String overlayPath) {
return path.replaceFirst(currentSearchPath, overlayPath);
}
}