16
16
import fi .helsinki .cs .tmc .utilities .ByteArrayGsonSerializer ;
17
17
import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
18
18
import fi .helsinki .cs .tmc .utilities .ExceptionUtils ;
19
+ import fi .helsinki .cs .tmc .utilities .JsonMaker ;
20
+ import fi .helsinki .cs .tmc .utilities .JsonMakerGsonSerializer ;
19
21
import fi .helsinki .cs .tmc .utilities .UriUtils ;
20
22
import fi .helsinki .cs .tmc .utilities .http .FailedHttpResponseException ;
21
23
import fi .helsinki .cs .tmc .utilities .http .HttpTasks ;
37
39
*/
38
40
public class ServerAccess {
39
41
public static final int API_VERSION = 7 ;
40
-
42
+
41
43
private TmcSettings settings ;
42
44
private CourseListParser courseListParser ;
43
45
private CourseInfoParser courseInfoParser ;
44
46
private ReviewListParser reviewListParser ;
45
47
private String clientVersion ;
46
-
48
+
47
49
public ServerAccess () {
48
50
this (TmcSettings .getDefault ());
49
51
}
@@ -64,30 +66,30 @@ public ServerAccess(
64
66
this .reviewListParser = reviewListParser ;
65
67
this .clientVersion = getClientVersion ();
66
68
}
67
-
69
+
68
70
private static String getClientVersion () {
69
71
return Modules .getDefault ().ownerOf (ServerAccess .class ).getSpecificationVersion ().toString ();
70
72
}
71
-
73
+
72
74
public void setSettings (TmcSettings settings ) {
73
75
this .settings = settings ;
74
76
}
75
-
77
+
76
78
private String getCourseListUrl () {
77
79
return addApiCallQueryParameters (settings .getServerBaseUrl () + "/courses.json" );
78
80
}
79
-
81
+
80
82
private String addApiCallQueryParameters (String url ) {
81
83
url = UriUtils .withQueryParam (url , "api_version" , "" +API_VERSION );
82
84
url = UriUtils .withQueryParam (url , "client" , "netbeans_plugin" );
83
85
url = UriUtils .withQueryParam (url , "client_version" , clientVersion );
84
86
return url ;
85
87
}
86
-
88
+
87
89
private HttpTasks createHttpTasks () {
88
90
return new HttpTasks ().setCredentials (settings .getUsername (), settings .getPassword ());
89
91
}
90
-
92
+
91
93
public boolean hasEnoughSettings () {
92
94
return
93
95
!settings .getUsername ().isEmpty () &&
@@ -101,7 +103,7 @@ public boolean needsOnlyPassword() {
101
103
settings .getPassword ().isEmpty () &&
102
104
!settings .getServerBaseUrl ().isEmpty ();
103
105
}
104
-
106
+
105
107
public CancellableCallable <List <Course >> getDownloadingCourseListTask () {
106
108
final CancellableCallable <String > download = createHttpTasks ().getForText (getCourseListUrl ());
107
109
return new CancellableCallable <List <Course >>() {
@@ -142,7 +144,7 @@ public boolean cancel() {
142
144
}
143
145
};
144
146
}
145
-
147
+
146
148
public CancellableCallable <Void > getUnlockingTask (Course course ) {
147
149
Map <String , String > params = Collections .emptyMap ();
148
150
final CancellableCallable <String > download = createHttpTasks ().postForText (getUnlockUrl (course ), params );
@@ -163,16 +165,16 @@ public boolean cancel() {
163
165
}
164
166
};
165
167
}
166
-
168
+
167
169
private String getUnlockUrl (Course course ) {
168
170
return addApiCallQueryParameters (course .getUnlockUrl ());
169
171
}
170
-
172
+
171
173
public CancellableCallable <byte []> getDownloadingExerciseZipTask (Exercise exercise ) {
172
174
String zipUrl = exercise .getDownloadUrl ();
173
175
return createHttpTasks ().getForBinary (zipUrl );
174
176
}
175
-
177
+
176
178
public CancellableCallable <byte []> getDownloadingExerciseSolutionZipTask (Exercise exercise ) {
177
179
String zipUrl = exercise .getSolutionDownloadUrl ();
178
180
return createHttpTasks ().getForBinary (zipUrl );
@@ -188,7 +190,7 @@ public CancellableCallable<SubmissionResponse> getSubmittingExerciseTask(final E
188
190
189
191
final CancellableCallable <String > upload =
190
192
createHttpTasks ().uploadFileForTextDownload (submitUrl , params , "submission[file]" , sourceZip );
191
-
193
+
192
194
return new CancellableCallable <SubmissionResponse >() {
193
195
@ Override
194
196
public SubmissionResponse call () throws Exception {
@@ -198,7 +200,7 @@ public SubmissionResponse call() throws Exception {
198
200
} catch (FailedHttpResponseException ex ) {
199
201
return checkForObsoleteClient (ex );
200
202
}
201
-
203
+
202
204
JsonObject respJson = new JsonParser ().parse (response ).getAsJsonObject ();
203
205
if (respJson .get ("error" ) != null ) {
204
206
throw new RuntimeException ("Server responded with error: " + respJson .get ("error" ));
@@ -230,11 +232,11 @@ public SubmissionResponse(URI submissionUrl, URI pasteUrl) {
230
232
this .pasteUrl = pasteUrl ;
231
233
}
232
234
}
233
-
235
+
234
236
public CancellableCallable <String > getSubmissionFetchTask (String submissionUrl ) {
235
237
return createHttpTasks ().getForText (submissionUrl );
236
238
}
237
-
239
+
238
240
public CancellableCallable <List <Review >> getDownloadingReviewListTask (Course course ) {
239
241
String url = addApiCallQueryParameters (course .getReviewsUrl ());
240
242
final CancellableCallable <String > download = createHttpTasks ().getForText (url );
@@ -255,7 +257,7 @@ public boolean cancel() {
255
257
}
256
258
};
257
259
}
258
-
260
+
259
261
public CancellableCallable <Void > getMarkingReviewAsReadTask (Review review , boolean read ) {
260
262
String url = addApiCallQueryParameters (review .getUpdateUrl () + ".json" );
261
263
Map <String , String > params = new HashMap <String , String >();
@@ -265,7 +267,7 @@ public CancellableCallable<Void> getMarkingReviewAsReadTask(Review review, boole
265
267
} else {
266
268
params .put ("mark_as_unread" , "1" );
267
269
}
268
-
270
+
269
271
final CancellableCallable <String > task = createHttpTasks ().postForText (url , params );
270
272
return new CancellableCallable <Void >() {
271
273
@ Override
@@ -280,20 +282,20 @@ public boolean cancel() {
280
282
}
281
283
};
282
284
}
283
-
285
+
284
286
public CancellableCallable <String > getFeedbackAnsweringJob (String answerUrl , List <FeedbackAnswer > answers ) {
285
287
final String submitUrl = addApiCallQueryParameters (answerUrl );
286
-
288
+
287
289
Map <String , String > params = new HashMap <String , String >();
288
290
for (int i = 0 ; i < answers .size (); ++i ) {
289
291
String keyPrefix = "answers[" + i + "]" ;
290
292
FeedbackAnswer answer = answers .get (i );
291
293
params .put (keyPrefix + "[question_id]" , "" + answer .getQuestion ().getId ());
292
294
params .put (keyPrefix + "[answer]" , answer .getAnswer ());
293
295
}
294
-
296
+
295
297
final CancellableCallable <String > upload = createHttpTasks ().postForText (submitUrl , params );
296
-
298
+
297
299
return new CancellableCallable <String >() {
298
300
@ Override
299
301
public String call () throws Exception {
@@ -310,7 +312,7 @@ public boolean cancel() {
310
312
}
311
313
};
312
314
}
313
-
315
+
314
316
public CancellableCallable <Object > getSendEventLogJob (String spywareServerUrl , List <LoggableEvent > events ) {
315
317
String url = addApiCallQueryParameters (spywareServerUrl );
316
318
@@ -347,8 +349,10 @@ private byte[] eventListToPostBody(List<LoggableEvent> events) throws IOExceptio
347
349
GZIPOutputStream gzos = new GZIPOutputStream (bufferBos );
348
350
OutputStreamWriter bufferWriter = new OutputStreamWriter (gzos , Charset .forName ("UTF-8" ));
349
351
352
+
350
353
Gson gson = new GsonBuilder ()
351
354
.registerTypeAdapter (byte [].class , new ByteArrayGsonSerializer ())
355
+ .registerTypeAdapter (JsonMaker .class , new JsonMakerGsonSerializer ())
352
356
.create ();
353
357
354
358
gson .toJson (events , new TypeToken <List <LoggableEvent >>(){}.getType (), bufferWriter );
@@ -357,7 +361,7 @@ private byte[] eventListToPostBody(List<LoggableEvent> events) throws IOExceptio
357
361
358
362
return bufferBos .toByteArray ();
359
363
}
360
-
364
+
361
365
private <T > T checkForObsoleteClient (FailedHttpResponseException ex ) throws ObsoleteClientException , FailedHttpResponseException {
362
366
if (ex .getStatusCode () == 404 ) {
363
367
boolean obsolete ;
0 commit comments