ProductPagesServlet.java
5.1 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.commerce.api.CommerceException
* com.adobe.cq.commerce.api.CommerceService
* com.adobe.cq.commerce.api.CommerceSession
* com.adobe.cq.commerce.api.Product
* com.adobe.cq.commerce.common.CommerceHelper
* com.day.cq.commons.ImageResource
* com.day.cq.commons.TidyJSONWriter
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.commons.ReferenceSearch
* com.day.cq.wcm.commons.ReferenceSearch$Info
* 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.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.pim.impl;
import com.adobe.cq.commerce.api.CommerceException;
import com.adobe.cq.commerce.api.CommerceService;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.Product;
import com.adobe.cq.commerce.common.CommerceHelper;
import com.day.cq.commons.ImageResource;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.commons.ReferenceSearch;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;
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.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
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={"Provides a list of catalog pages generated for a particular PIM product"}), @Property(name="sling.servlet.resourceTypes", value={"commerce/components/product"}), @Property(name="sling.servlet.selectors", value={"productpages"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class ProductPagesServlet
extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(ProductPagesServlet.class);
private void writeProductData(CommerceSession session, Product product, JSONWriter writer) throws JSONException, CommerceException {
writer.object();
writer.key("pagePath").value((Object)product.getPagePath());
writer.key("productPath").value((Object)product.getPath());
writer.key("productTitle").value((Object)product.getTitle());
writer.key("productImage").value((Object)product.getImage().getPath());
if (session != null) {
writer.key("productPrice").value((Object)session.getProductPrice(product));
}
writer.endObject();
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
Resource resource = request.getResource();
CommerceService commerceService = (CommerceService)resource.adaptTo(CommerceService.class);
CommerceSession session = commerceService != null ? commerceService.login(request, response) : null;
Product pimProduct = (Product)request.getResource().adaptTo(Product.class);
Collection resultSet = new ReferenceSearch().search(resource.getResourceResolver(), resource.getPath()).values();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
TidyJSONWriter writer = new TidyJSONWriter((Writer)response.getWriter());
writer.array();
for (ReferenceSearch.Info infoItem : resultSet) {
Product product;
Page productPage = infoItem.getPage();
if (!((String)productPage.getProperties().get("cq:productMaster", (Object)"")).equals(pimProduct.getPath()) || (product = CommerceHelper.findCurrentProduct((Page)productPage)) == null) continue;
this.writeProductData(session, product, (JSONWriter)writer);
}
writer.endArray();
}
catch (Exception e) {
log.warn("Error fetching product references", (Throwable)e);
response.sendError(500, "Error fetching product references: " + e.getMessage());
}
}
}