AddCartEntryServlet.java
3.78 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.xss.XSSAPI
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* 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.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.servlets.SlingAllMethodsServlet
* org.apache.sling.auth.core.AuthUtil
*/
package com.adobe.cq.commerce.impl;
import com.adobe.cq.commerce.api.CommerceService;
import com.adobe.cq.commerce.api.CommerceSession;
import com.adobe.cq.commerce.api.Product;
import com.adobe.granite.xss.XSSAPI;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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.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.servlets.SlingAllMethodsServlet;
import org.apache.sling.auth.core.AuthUtil;
@Component
@Service
@Properties(value={@Property(name="service.description", value={"Provides commerce services for product pages"}), @Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.selectors", value={"commerce.addcartentry"}), @Property(name="sling.servlet.extensions", value={"html"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class AddCartEntryServlet
extends SlingAllMethodsServlet {
@Reference
private XSSAPI xssAPI;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String productPath = request.getParameter("product-path");
String qty = request.getParameter("product-quantity");
String redirect = request.getParameter("redirect");
String productNotFound = request.getParameter("redirect-product-not-found");
try {
Resource productResource = request.getResourceResolver().getResource(productPath);
CommerceService commerceService = (CommerceService)request.getResource().adaptTo(CommerceService.class);
CommerceSession session = commerceService.login(request, response);
Product product = (Product)productResource.adaptTo(Product.class);
int quantity = 1;
if (qty != null && qty.length() > 0 && (quantity = this.xssAPI.getValidInteger(qty, 1).intValue()) < 0) {
quantity = 1;
}
session.addCartEntry(product, quantity);
if (AuthUtil.isRedirectValid((HttpServletRequest)request, (String)redirect)) {
response.sendRedirect(redirect);
} else {
response.sendError(403);
}
}
catch (Exception e) {
if (AuthUtil.isRedirectValid((HttpServletRequest)request, (String)productNotFound)) {
response.sendRedirect(productNotFound);
}
response.sendError(403);
}
}
protected void bindXssAPI(XSSAPI xSSAPI) {
this.xssAPI = xSSAPI;
}
protected void unbindXssAPI(XSSAPI xSSAPI) {
if (this.xssAPI == xSSAPI) {
this.xssAPI = null;
}
}
}