CommerceProvidersServlet.java
4.22 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* javax.servlet.ServletException
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.impl;
import com.adobe.cq.commerce.api.CommerceServiceFactory;
import com.adobe.granite.xss.XSSAPI;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
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.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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 registered commerce providers"}), @Property(name="sling.servlet.paths", value={"/libs/commerce/providers"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class CommerceProvidersServlet
extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(CommerceProvidersServlet.class);
@Reference
private XSSAPI xssAPI;
@Reference(referenceInterface=CommerceServiceFactory.class, bind="bindFactory", unbind="unbindFactory", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
private Map<String, Map<?, ?>> factories = new HashMap();
protected void bindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
String provider = (String)properties.get("commerceProvider");
this.factories.put(provider, properties);
}
protected void unbindFactory(CommerceServiceFactory factory, Map<?, ?> properties) {
String provider = (String)properties.get("commerceProvider");
this.factories.remove(provider);
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.setHeader("Cache-Control", "no-cache");
JSONWriter w = new JSONWriter((Writer)response.getWriter());
w.array();
for (Map propertyMap : this.factories.values()) {
String value = (String)propertyMap.get("commerceProvider");
String label = (String)propertyMap.get("commerceProviderLabel");
if (label == null) {
label = StringUtils.capitalize((String)value);
}
w.object().key("value").value((Object)this.xssAPI.encodeForHTMLAttr(value)).key("text").value((Object)this.xssAPI.encodeForHTMLAttr(label)).endObject();
}
w.endArray();
}
catch (Exception e) {
log.error("Error while generating JSON list", (Throwable)e);
response.sendError(500, e.toString());
}
}
protected void bindXssAPI(XSSAPI xSSAPI) {
this.xssAPI = xSSAPI;
}
protected void unbindXssAPI(XSSAPI xSSAPI) {
if (this.xssAPI == xSSAPI) {
this.xssAPI = null;
}
}
}