RequestInfo.java
4.24 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
124
125
126
127
128
129
130
131
132
133
134
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletInputStream
* org.apache.commons.io.IOUtils
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.adobe.granite.socialgraph.impl.rest;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Set;
import javax.servlet.ServletInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public class RequestInfo {
public static final String RELATIONSHIPS_LIST_TYPE = "relationships";
private final SlingHttpServletRequest request;
private String extension;
private String[] segs;
private String id;
private String listType;
private String relationshipType;
private String otherId;
private JSONObject jsonPost;
protected RequestInfo() {
this.request = null;
}
public RequestInfo(SlingHttpServletRequest request) throws IOException {
this.request = request;
this.parseSuffix(request.getRequestPathInfo().getSuffix(), "GET".equals(request.getMethod()));
if ("application/json".equals(request.getContentType())) {
StringWriter json = new StringWriter();
InputStreamReader in = new InputStreamReader((InputStream)request.getInputStream(), request.getCharacterEncoding());
IOUtils.copy((Reader)in, (Writer)json);
try {
this.jsonPost = new JSONObject(json.toString());
}
catch (JSONException e) {
IOException io = new IOException("Error while reading json input.");
io.initCause((Throwable)e);
throw io;
}
} else {
this.jsonPost = new JSONObject();
for (String name : request.getRequestParameterMap().keySet()) {
try {
this.jsonPost.put(name, (Object)request.getRequestParameter(name));
continue;
}
catch (JSONException e) {
IOException io = new IOException("Error while mapping input.");
io.initCause((Throwable)e);
throw io;
}
}
}
}
protected void parseSuffix(String suffix, boolean respectExtension) {
int idx;
if (suffix == null || suffix.length() == 0) {
return;
}
if (respectExtension && (idx = suffix.lastIndexOf(46)) > 0) {
this.extension = suffix.substring(idx + 1);
suffix = suffix.substring(0, idx);
}
this.segs = Text.explode((String)suffix, (int)47);
this.id = Text.unescapeIllegalJcrChars((String)this.segs[0]);
if (this.segs.length > 1) {
this.listType = this.segs[1];
}
if (this.segs.length > 2) {
this.relationshipType = this.segs[2];
}
if (this.segs.length > 3) {
this.otherId = Text.unescapeIllegalJcrChars((String)this.segs[3]);
}
}
public SlingHttpServletRequest getRequest() {
return this.request;
}
public String getExtension() {
return this.extension;
}
public String getId() {
return this.id;
}
public String getListType() {
return this.listType;
}
public String getRelationshipType() {
return this.relationshipType;
}
public String getOtherId() {
return this.otherId;
}
public JSONObject getJsonPost() {
return this.jsonPost;
}
public String[] getSegments() {
return this.segs;
}
}