CollectionPostServlet.java
4.99 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.cfm.ContentFragment
* com.adobe.cq.dam.cfm.ContentFragmentException
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.cfm.impl.servlets;
import com.adobe.cq.dam.cfm.ContentFragment;
import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.impl.AssociatedContentException;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(selectors={"cfm.coll", "cfm.coll"}, resourceTypes={"dam:Asset"}, extensions={"json"}, methods={"POST"})
public class CollectionPostServlet
extends SlingAllMethodsServlet {
private final Logger log;
public CollectionPostServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
private void addCollections(Resource resource, String[] collections) throws ContentFragmentException {
ContentFragment cf = (ContentFragment)resource.adaptTo(ContentFragment.class);
if (cf == null) {
throw new AssociatedContentException("'" + resource.getPath() + "' is no valid content fragment.");
}
ResourceResolver resolver = resource.getResourceResolver();
try {
for (String collection : collections) {
Resource collectionRsc = resolver.getResource(collection);
if (collectionRsc == null) {
throw new AssociatedContentException("'" + collection + "' does not point to a valid resource.");
}
cf.addAssociatedContent(collectionRsc);
}
resolver.commit();
}
catch (PersistenceException pe) {
throw new AssociatedContentException("Could not add collections", (Throwable)pe);
}
finally {
resolver.refresh();
}
}
private void removeCollection(Resource resource, String collection) throws ContentFragmentException {
ContentFragment cf = (ContentFragment)resource.adaptTo(ContentFragment.class);
if (cf == null) {
throw new AssociatedContentException("'" + resource.getPath() + "' is no valid content fragment.");
}
ResourceResolver resolver = resource.getResourceResolver();
Resource collectionRsc = resolver.getResource(collection);
if (collectionRsc == null) {
throw new AssociatedContentException("'" + collection + "' does not point to a valid resource.");
}
try {
cf.removeAssociatedContent(collectionRsc);
resolver.commit();
}
catch (PersistenceException pe) {
throw new AssociatedContentException("Could not remove collection", (Throwable)pe);
}
finally {
resolver.refresh();
}
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Resource resource = request.getResource();
String operation = request.getParameter(":operation");
if (operation == null) {
response.sendError(500, "Missing :operation parameter.");
return;
}
try {
if (operation.equals("add")) {
String[] collections = request.getParameterValues("collection");
if (collections == null) {
response.sendError(500, "Missing collection parameter(s).");
return;
}
this.addCollections(resource, collections);
} else if (operation.equals("remove")) {
String collection = request.getParameter("collection");
this.removeCollection(resource, collection);
}
}
catch (Exception e) {
response.sendError(500, "Could not process collection request.");
this.log.error("Could not process collection change", (Throwable)e);
return;
}
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("{\"success\":true}");
}
}