Repository navigation
Expand file tree
/
Copy pathoss.java
More file actions
164 lines (116 loc) · 4.46 KB
/
Copy pathoss.java
File metadata and controls
164 lines (116 loc) · 4.46 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
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.xml.bind.DatatypeConverter;
import org.json.*;
import com.autodesk.client.auth.OAuth2TwoLegged;
import com.autodesk.client.ApiException;
import com.autodesk.client.ApiResponse;
import com.autodesk.client.api.*;
import com.autodesk.client.model.*;
@WebServlet({ "/oss" })
public class oss extends HttpServlet {
public oss() {
}
public void init() throws ServletException {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// for get buckets info
String id = req.getParameter("id");
resp.setCharacterEncoding("utf8");
resp.setContentType("application/json");
try {
// get oAuth of internal, in order to get the token with higher permissions
OAuth2TwoLegged forgeOAuth = oauth.getOAuthInternal();
if (id.equals("#")) {// root
BucketsApi bucketsApi = new BucketsApi();
ApiResponse<Buckets> buckets = bucketsApi.getBuckets("us", 100, null, forgeOAuth,
forgeOAuth.getCredentials());
JSONArray bucketsArray = new JSONArray();
PrintWriter out = resp.getWriter();
// iterate buckets
for (int i = 0; i < buckets.getData().getItems().size(); i++) {
// get bucker info
BucketsItems eachItem = buckets.getData().getItems().get(i);
JSONObject obj = new JSONObject();
obj.put("id", eachItem.getBucketKey());
obj.put("text", eachItem.getBucketKey());
obj.put("type", "bucket");
obj.put("children", true);
bucketsArray.put(obj);
}
out.print(bucketsArray);
} else {
// as we have the id (bucketKey), let's return all objects
ObjectsApi objectsApi = new ObjectsApi();
ApiResponse<BucketObjects> objects = objectsApi.getObjects(id, 100, null, null, forgeOAuth,
forgeOAuth.getCredentials());
JSONArray objectsArray = new JSONArray();
PrintWriter out = resp.getWriter();
// iterate each items of the bucket
for (int i = 0; i < objects.getData().getItems().size(); i++) {
// make a note with the base64 urn of the item
ObjectDetails eachItem = objects.getData().getItems().get(i);
String base64Urn = DatatypeConverter.printBase64Binary(eachItem.getObjectId().getBytes());
JSONObject obj = new JSONObject();
obj.put("id", base64Urn);
obj.put("text", eachItem.getObjectKey());
obj.put("type", "object");
obj.put("children", false);
objectsArray.put(obj);
}
out.print(objectsArray);
}
} catch (ApiException autodeskExp) {
System.out.print("get buckets & objects exception: " + autodeskExp.toString());
resp.setStatus(500);
} catch (Exception exp) {
System.out.print("get buckets & objects exception: " + exp.toString());
resp.setStatus(500);
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// for create bucket
try {
// from
// https://stackoverflow.com/questions/3831680/httpservletrequest-get-json-post-data/3831791
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {
/* report an error */ }
// Create a new bucket
try {
// get oAuth of internal, in order to get the token with higher permissions
OAuth2TwoLegged forgeOAuth = oauth.getOAuthInternal();
JSONObject jsonObject = new JSONObject(jb.toString());
String bucketKey = jsonObject.getString("bucketKey");
// build the payload of the http request
BucketsApi bucketsApi = new BucketsApi();
PostBucketsPayload postBuckets = new PostBucketsPayload();
postBuckets.setBucketKey(bucketKey);
// expires in 24h
postBuckets.setPolicyKey(PostBucketsPayload.PolicyKeyEnum.TRANSIENT);
ApiResponse<Bucket> newbucket = bucketsApi.createBucket(postBuckets, null, forgeOAuth,
forgeOAuth.getCredentials());
resp.setStatus(200);
} catch (ApiException autodeskExp) {
System.out.print("get buckets & objects exception: " + autodeskExp.toString());
resp.setStatus(500);
} catch (Exception exp) {
System.out.print("get buckets & objects exception: " + exp.toString());
resp.setStatus(500);
}
} catch (JSONException e) {
// crash and burn
throw new IOException("Error parsing JSON request string");
}
}
public void destroy() {
super.destroy();
}
}