VariantHierarchyServlet.java
3.33 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.TidyJSONWriter
* javax.jcr.Node
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.impl;
import com.day.cq.commons.TidyJSONWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Stack;
import javax.jcr.Node;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"Enumerates the variations hierarchy above a product variant"}), @Property(name="sling.servlet.resourceTypes", value={"commerce/components/product"}), @Property(name="sling.servlet.selectors", value={"commerce.varianthierarchy"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class VariantHierarchyServlet
extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(VariantHierarchyServlet.class);
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
Stack<Resource> hierarchy = new Stack<Resource>();
for (Resource ancestor = request.getResource().getParent(); ancestor != null; ancestor = ancestor.getParent()) {
hierarchy.push(ancestor);
if (!((String)ResourceUtil.getValueMap((Resource)ancestor).get("cq:commerceType", (Object)"")).equals("variant")) break;
}
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
TidyJSONWriter writer = new TidyJSONWriter((Writer)response.getWriter());
writer.array();
while (!hierarchy.isEmpty()) {
Node node = (Node)((Resource)hierarchy.pop()).adaptTo(Node.class);
writer.object().key("name").value((Object)node.getName()).key("path").value((Object)node.getPath()).endObject();
}
writer.endArray();
}
catch (Exception e) {
log.warn("Error fetching product variant hierarchy", (Throwable)e);
response.sendError(500, "Error fetching variant hierarchy: " + e.getMessage());
}
}
}