1
1
package fi .helsinki .cs .tmc .model ;
2
2
3
- import com .google .gson .Gson ;
4
- import com .google .gson .GsonBuilder ;
5
- import com .google .gson .JsonObject ;
6
- import com .google .gson .JsonParser ;
7
- import com .google .gson .reflect .TypeToken ;
8
3
import fi .helsinki .cs .tmc .core .domain .Course ;
9
4
import fi .helsinki .cs .tmc .core .domain .Exercise ;
10
5
import fi .helsinki .cs .tmc .data .FeedbackAnswer ;
20
15
import fi .helsinki .cs .tmc .utilities .http .FailedHttpResponseException ;
21
16
import fi .helsinki .cs .tmc .utilities .http .HttpTasks ;
22
17
import fi .helsinki .cs .tmc .core .TmcCore ;
18
+
19
+ import com .google .gson .Gson ;
20
+ import com .google .gson .GsonBuilder ;
21
+ import com .google .gson .JsonObject ;
22
+ import com .google .gson .JsonParser ;
23
+ import com .google .gson .reflect .TypeToken ;
24
+
25
+ import org .openide .modules .Modules ;
26
+
23
27
import java .io .ByteArrayOutputStream ;
24
28
import java .io .IOException ;
25
29
import java .io .OutputStreamWriter ;
31
35
import java .util .List ;
32
36
import java .util .Map ;
33
37
import java .util .zip .GZIPOutputStream ;
34
- import org .openide .modules .Modules ;
35
38
36
39
/**
37
40
* A frontend for the server.
41
+ *
42
+ * @deprecated Users are suggested to rely on the tmc-core instead of this implementation.
38
43
*/
44
+ @ Deprecated
39
45
public class ServerAccess {
40
46
public static final int API_VERSION = 7 ;
41
-
47
+
42
48
private NbTmcSettings settings ;
43
49
private CourseListParser courseListParser ;
44
50
private CourseInfoParser courseInfoParser ;
45
51
private ReviewListParser reviewListParser ;
46
52
private String clientVersion ;
47
53
private TmcCore tmcCore ;
48
-
54
+
49
55
public ServerAccess () {
50
56
this (NbTmcSettings .getDefault ());
51
57
}
@@ -66,30 +72,30 @@ public ServerAccess(
66
72
this .reviewListParser = reviewListParser ;
67
73
this .clientVersion = getClientVersion ();
68
74
}
69
-
75
+
70
76
private static String getClientVersion () {
71
77
return Modules .getDefault ().ownerOf (ServerAccess .class ).getSpecificationVersion ().toString ();
72
78
}
73
-
79
+
74
80
public void setSettings (NbTmcSettings settings ) {
75
81
this .settings = settings ;
76
82
}
77
-
83
+
78
84
private String getCourseListUrl () {
79
85
return addApiCallQueryParameters (settings .getServerAddress () + "/courses.json" );
80
86
}
81
-
87
+
82
88
public String addApiCallQueryParameters (String url ) {
83
89
url = UriUtils .withQueryParam (url , "api_version" , "" +API_VERSION );
84
90
url = UriUtils .withQueryParam (url , "client" , "netbeans_plugin" );
85
91
url = UriUtils .withQueryParam (url , "client_version" , getClientVersion ());
86
92
return url ;
87
93
}
88
-
94
+
89
95
private HttpTasks createHttpTasks () {
90
96
return new HttpTasks ().setCredentials (settings .getUsername (), settings .getPassword ());
91
97
}
92
-
98
+
93
99
public boolean hasEnoughSettings () {
94
100
return
95
101
!settings .getUsername ().isEmpty () &&
@@ -103,7 +109,7 @@ public boolean needsOnlyPassword() {
103
109
settings .getPassword ().isEmpty () &&
104
110
!settings .getServerAddress ().isEmpty ();
105
111
}
106
-
112
+
107
113
@ Deprecated
108
114
public CancellableCallable <List <Course >> getDownloadingCourseListTask () {
109
115
final CancellableCallable <String > download = createHttpTasks ().getForText (getCourseListUrl ());
@@ -146,7 +152,7 @@ public boolean cancel() {
146
152
}
147
153
};
148
154
}
149
-
155
+
150
156
public CancellableCallable <Void > getUnlockingTask (Course course ) {
151
157
Map <String , String > params = Collections .emptyMap ();
152
158
final CancellableCallable <String > download = createHttpTasks ().postForText (getUnlockUrl (course ), params );
@@ -167,16 +173,16 @@ public boolean cancel() {
167
173
}
168
174
};
169
175
}
170
-
176
+
171
177
private String getUnlockUrl (Course course ) {
172
178
return addApiCallQueryParameters (course .getUnlockUrl ());
173
179
}
174
-
180
+
175
181
public CancellableCallable <byte []> getDownloadingExerciseZipTask (Exercise exercise ) {
176
182
String zipUrl = exercise .getDownloadUrl ();
177
183
return createHttpTasks ().getForBinary (zipUrl );
178
184
}
179
-
185
+
180
186
public CancellableCallable <byte []> getDownloadingExerciseSolutionZipTask (Exercise exercise ) {
181
187
String zipUrl = exercise .getSolutionDownloadUrl ();
182
188
return createHttpTasks ().getForBinary (zipUrl );
@@ -192,7 +198,7 @@ public CancellableCallable<SubmissionResponse> getSubmittingExerciseTask(final E
192
198
193
199
final CancellableCallable <String > upload =
194
200
createHttpTasks ().uploadFileForTextDownload (submitUrl , params , "submission[file]" , sourceZip );
195
-
201
+
196
202
return new CancellableCallable <SubmissionResponse >() {
197
203
@ Override
198
204
public SubmissionResponse call () throws Exception {
@@ -202,7 +208,7 @@ public SubmissionResponse call() throws Exception {
202
208
} catch (FailedHttpResponseException ex ) {
203
209
return checkForObsoleteClient (ex );
204
210
}
205
-
211
+
206
212
JsonObject respJson = new JsonParser ().parse (response ).getAsJsonObject ();
207
213
if (respJson .get ("error" ) != null ) {
208
214
throw new RuntimeException ("Server responded with error: " + respJson .get ("error" ));
@@ -234,11 +240,11 @@ public SubmissionResponse(URI submissionUrl, URI pasteUrl) {
234
240
this .pasteUrl = pasteUrl ;
235
241
}
236
242
}
237
-
243
+
238
244
public CancellableCallable <String > getSubmissionFetchTask (String submissionUrl ) {
239
245
return createHttpTasks ().getForText (submissionUrl );
240
246
}
241
-
247
+
242
248
public CancellableCallable <List <Review >> getDownloadingReviewListTask (Course course ) {
243
249
String url = addApiCallQueryParameters (course .getReviewsUrl ());
244
250
final CancellableCallable <String > download = createHttpTasks ().getForText (url );
@@ -259,7 +265,7 @@ public boolean cancel() {
259
265
}
260
266
};
261
267
}
262
-
268
+
263
269
public CancellableCallable <Void > getMarkingReviewAsReadTask (Review review , boolean read ) {
264
270
String url = addApiCallQueryParameters (review .getUpdateUrl () + ".json" );
265
271
Map <String , String > params = new HashMap <String , String >();
@@ -269,7 +275,7 @@ public CancellableCallable<Void> getMarkingReviewAsReadTask(Review review, boole
269
275
} else {
270
276
params .put ("mark_as_unread" , "1" );
271
277
}
272
-
278
+
273
279
final CancellableCallable <String > task = createHttpTasks ().postForText (url , params );
274
280
return new CancellableCallable <Void >() {
275
281
@ Override
@@ -284,20 +290,20 @@ public boolean cancel() {
284
290
}
285
291
};
286
292
}
287
-
293
+
288
294
public CancellableCallable <String > getFeedbackAnsweringJob (String answerUrl , List <FeedbackAnswer > answers ) {
289
295
final String submitUrl = addApiCallQueryParameters (answerUrl );
290
-
296
+
291
297
Map <String , String > params = new HashMap <String , String >();
292
298
for (int i = 0 ; i < answers .size (); ++i ) {
293
299
String keyPrefix = "answers[" + i + "]" ;
294
300
FeedbackAnswer answer = answers .get (i );
295
301
params .put (keyPrefix + "[question_id]" , "" + answer .getQuestion ().getId ());
296
302
params .put (keyPrefix + "[answer]" , answer .getAnswer ());
297
303
}
298
-
304
+
299
305
final CancellableCallable <String > upload = createHttpTasks ().postForText (submitUrl , params );
300
-
306
+
301
307
return new CancellableCallable <String >() {
302
308
@ Override
303
309
public String call () throws Exception {
@@ -314,7 +320,7 @@ public boolean cancel() {
314
320
}
315
321
};
316
322
}
317
-
323
+
318
324
public CancellableCallable <Object > getSendEventLogJob (String spywareServerUrl , List <LoggableEvent > events ) {
319
325
String url = addApiCallQueryParameters (spywareServerUrl );
320
326
@@ -361,7 +367,7 @@ public byte[] eventListToPostBody(List<LoggableEvent> events) throws IOException
361
367
362
368
return bufferBos .toByteArray ();
363
369
}
364
-
370
+
365
371
private <T > T checkForObsoleteClient (FailedHttpResponseException ex ) throws ObsoleteClientException , FailedHttpResponseException {
366
372
if (ex .getStatusCode () == 404 ) {
367
373
boolean obsolete ;
@@ -377,4 +383,4 @@ private <T> T checkForObsoleteClient(FailedHttpResponseException ex) throws Obso
377
383
378
384
throw ex ;
379
385
}
380
- }
386
+ }
0 commit comments