PackageShareSessionImpl.java
7.14 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.http.auth.Credentials
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
*/
package com.day.crx.packaging.impl;
import com.day.crx.packaging.HtmlStatusResponseParser;
import com.day.crx.packaging.JSONResponse;
import com.day.crx.packaging.PackageInfo;
import com.day.crx.packaging.PackageShare;
import com.day.crx.packaging.PackageShareSession;
import com.day.crx.packaging.Proxy;
import com.day.crx.packaging.ProxyTracker;
import com.day.crx.packaging.impl.PackageShareImpl;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.http.auth.Credentials;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
public class PackageShareSessionImpl
implements PackageShareSession {
private final PackageShareImpl svc;
private final String id;
private final long creationTime;
private Data data;
private Credentials creds;
private CloseableHttpClient httpClient;
public PackageShareSessionImpl(PackageShareImpl svc, String id, CloseableHttpClient httpClient) {
this.svc = svc;
this.id = id;
this.httpClient = httpClient;
this.creationTime = System.currentTimeMillis();
}
public void setUser(String userId, Credentials creds, JSONObject data) {
this.creds = creds;
this.data = new Data(userId, data);
}
@Override
public boolean isNew() {
return this.data == null;
}
@Override
public boolean isAnonymous() {
return this.creds == null;
}
@Override
public PackageShare getService() {
return this.svc;
}
public CloseableHttpClient getHttpClient() {
return this.httpClient;
}
@Override
public String getId() {
return this.id;
}
@Override
public long getCreationTime() {
return this.creationTime;
}
@Override
public String getUserId() {
return this.data == null ? "anonymous" : this.data.userId;
}
@Override
public String getUserName() {
if (this.data == null) {
return null;
}
try {
return this.data.data.getJSONObject("user").optString("name");
}
catch (JSONException e) {
return null;
}
}
@Override
public Set<String> getSharedGroups() {
return this.data == null ? null : this.data.getSharedGroups();
}
@Override
public JSONObject getSessionInfo() {
return this.data == null ? null : this.data.data;
}
@Override
public Set<PackageInfo> getAvailablePackages() {
return null;
}
@Override
public void logout() {
this.data = null;
this.creds = null;
this.svc.discardSession(this.id);
}
@Override
public Proxy createProxy(boolean flag) {
Proxy p = this.svc.createProxy(flag, this.httpClient);
if (this.creds != null) {
p.setCredentials(this.creds);
}
return p;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public JSONResponse uploadPackage(Node fileNode, ProxyTracker tracker, String group) throws IOException, RepositoryException {
String title;
Property prop = fileNode.getProperty("{http://www.jcp.org/jcr/1.0}content/{http://www.jcp.org/jcr/1.0}data");
try {
title = fileNode.getProperty("{http://www.jcp.org/jcr/1.0}content/{http://www.jcp.org/jcr/1.0}title").getString();
}
catch (RepositoryException e) {
title = fileNode.getName();
}
Binary binary = prop.getBinary();
Proxy proxy = this.createProxy(false);
proxy.setTracker(tracker);
proxy.addExtraFile("file", binary, title);
proxy.addExtraParameter("cmd", "promote");
if (group != null && group.length() > 0) {
proxy.addExtraParameter("group", group);
}
try {
int status = proxy.doPost("/apps/packageshare/content/service.html");
String data = proxy.fetch();
try {
JSONResponse jSONResponse = HtmlStatusResponseParser.parse(data);
return jSONResponse;
}
catch (IOException e) {
JSONResponse r = new JSONResponse(false, "Unknown Error");
r.setStatus(status);
JSONResponse jSONResponse = r;
proxy.close();
binary.dispose();
return jSONResponse;
}
}
finally {
proxy.close();
binary.dispose();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public JSONResponse uploadPackage(File file, String fileName, ProxyTracker tracker, String group) throws IOException, RepositoryException {
Proxy proxy = this.createProxy(false);
proxy.setTracker(tracker);
proxy.addExtraFile("file", file, fileName == null ? "unknown" : fileName);
proxy.addExtraParameter("cmd", "promote");
if (group != null && group.length() > 0) {
proxy.addExtraParameter("group", group);
}
try {
int status = proxy.doPost("/apps/packageshare/content/service.html");
String data = proxy.fetch();
try {
JSONResponse jSONResponse = HtmlStatusResponseParser.parse(data);
return jSONResponse;
}
catch (IOException e) {
JSONResponse r = new JSONResponse(false, "Unknown Error");
r.setStatus(status);
JSONResponse jSONResponse = r;
proxy.close();
return jSONResponse;
}
}
finally {
proxy.close();
}
}
private static final class Data {
private final String userId;
private final JSONObject data;
private Set<String> shares;
private Data(String userId, JSONObject data) {
this.userId = userId;
this.data = data;
}
public Set<String> getSharedGroups() {
if (this.shares == null) {
this.shares = new HashSet<String>();
try {
JSONArray names = this.data.getJSONObject("shares").names();
for (int i = 0; i < names.length(); ++i) {
this.shares.add(names.getString(i));
}
}
catch (JSONException e) {
// empty catch block
}
}
return this.shares;
}
}
}