PostServlet.java
4.48 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socialgraph.impl.rest;
import com.adobe.granite.socialgraph.Direction;
import com.adobe.granite.socialgraph.GraphNode;
import com.adobe.granite.socialgraph.Relationship;
import com.adobe.granite.socialgraph.SocialGraph;
import com.adobe.granite.socialgraph.SocialGraphException;
import com.adobe.granite.socialgraph.impl.rest.RequestInfo;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class PostServlet
extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(PostServlet.class);
protected void doPut(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
SocialGraph graph = (SocialGraph)request.getResourceResolver().adaptTo(SocialGraph.class);
if (graph == null) {
response.sendError(500, "Social graph not available.");
return;
}
RequestInfo info = new RequestInfo(request);
if (info.getId() == null) {
response.sendError(404);
return;
}
GraphNode node = graph.getNode(info.getId());
if (node == null) {
log.warn("No social graph node for user {}", (Object)info.getId());
response.sendError(404);
return;
}
if (info.getListType() == null) {
this.updateProperties(info, node);
graph.save();
response.setStatus(200);
return;
}
if (info.getOtherId() == null) {
log.warn("No social relationship addressed");
response.sendError(404);
return;
}
if (!"relationships".equals(info.getListType())) {
log.warn("Only 'relationships' collection supported", (Object)info.getListType());
response.sendError(404);
return;
}
GraphNode other = graph.getNode(info.getOtherId());
if (other == null) {
log.warn("No social graph node for user {}", (Object)info.getOtherId());
response.sendError(404);
return;
}
Relationship rel = node.getRelationship(Direction.OUTGOING, other, info.getRelationshipType());
if (rel == null) {
try {
rel = node.createRelationshipTo(other, info.getRelationshipType());
}
catch (SocialGraphException e) {
log.error("Error creating the relationship", (Throwable)e);
response.sendError(500);
}
}
this.updateProperties(info, rel);
graph.save();
response.setStatus(200);
}
private void updateProperties(RequestInfo info, Map<String, Object> map) throws IOException {
try {
JSONObject obj = info.getJsonPost();
JSONArray names = obj.names();
if (names != null) {
for (int i = 0; i < names.length(); ++i) {
String name = names.getString(i);
Object value = obj.get(name);
map.put(name, value);
}
}
}
catch (JSONException e) {
IOException io = new IOException("Error while writing properties");
io.initCause((Throwable)e);
throw io;
}
}
}