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 ;
@@ -66,30 +70,30 @@ public ServerAccess(
66
70
this .reviewListParser = reviewListParser ;
67
71
this .clientVersion = getClientVersion ();
68
72
}
69
-
73
+
70
74
private static String getClientVersion () {
71
75
return Modules .getDefault ().ownerOf (ServerAccess .class ).getSpecificationVersion ().toString ();
72
76
}
73
-
77
+
74
78
public void setSettings (NbTmcSettings settings ) {
75
79
this .settings = settings ;
76
80
}
77
-
81
+
78
82
private String getCourseListUrl () {
79
83
return addApiCallQueryParameters (settings .getServerAddress () + "/courses.json" );
80
84
}
81
-
85
+
82
86
public String addApiCallQueryParameters (String url ) {
83
87
url = UriUtils .withQueryParam (url , "api_version" , "" +API_VERSION );
84
88
url = UriUtils .withQueryParam (url , "client" , "netbeans_plugin" );
85
89
url = UriUtils .withQueryParam (url , "client_version" , getClientVersion ());
86
90
return url ;
87
91
}
88
-
92
+
89
93
private HttpTasks createHttpTasks () {
90
94
return new HttpTasks ().setCredentials (settings .getUsername (), settings .getPassword ());
91
95
}
92
-
96
+
93
97
public boolean hasEnoughSettings () {
94
98
return
95
99
!settings .getUsername ().isEmpty () &&
@@ -103,7 +107,7 @@ public boolean needsOnlyPassword() {
103
107
settings .getPassword ().isEmpty () &&
104
108
!settings .getServerAddress ().isEmpty ();
105
109
}
106
-
110
+
107
111
@ Deprecated
108
112
public CancellableCallable <List <Course >> getDownloadingCourseListTask () {
109
113
final CancellableCallable <String > download = createHttpTasks ().getForText (getCourseListUrl ());
@@ -146,7 +150,7 @@ public boolean cancel() {
146
150
}
147
151
};
148
152
}
149
-
153
+
150
154
public CancellableCallable <Void > getUnlockingTask (Course course ) {
151
155
Map <String , String > params = Collections .emptyMap ();
152
156
final CancellableCallable <String > download = createHttpTasks ().postForText (getUnlockUrl (course ), params );
@@ -167,16 +171,16 @@ public boolean cancel() {
167
171
}
168
172
};
169
173
}
170
-
174
+
171
175
private String getUnlockUrl (Course course ) {
172
176
return addApiCallQueryParameters (course .getUnlockUrl ());
173
177
}
174
-
178
+
175
179
public CancellableCallable <byte []> getDownloadingExerciseZipTask (Exercise exercise ) {
176
180
String zipUrl = exercise .getDownloadUrl ();
177
181
return createHttpTasks ().getForBinary (zipUrl );
178
182
}
179
-
183
+
180
184
public CancellableCallable <byte []> getDownloadingExerciseSolutionZipTask (Exercise exercise ) {
181
185
String zipUrl = exercise .getSolutionDownloadUrl ();
182
186
return createHttpTasks ().getForBinary (zipUrl );
@@ -192,7 +196,7 @@ public CancellableCallable<SubmissionResponse> getSubmittingExerciseTask(final E
192
196
193
197
final CancellableCallable <String > upload =
194
198
createHttpTasks ().uploadFileForTextDownload (submitUrl , params , "submission[file]" , sourceZip );
195
-
199
+
196
200
return new CancellableCallable <SubmissionResponse >() {
197
201
@ Override
198
202
public SubmissionResponse call () throws Exception {
@@ -202,7 +206,7 @@ public SubmissionResponse call() throws Exception {
202
206
} catch (FailedHttpResponseException ex ) {
203
207
return checkForObsoleteClient (ex );
204
208
}
205
-
209
+
206
210
JsonObject respJson = new JsonParser ().parse (response ).getAsJsonObject ();
207
211
if (respJson .get ("error" ) != null ) {
208
212
throw new RuntimeException ("Server responded with error: " + respJson .get ("error" ));
@@ -234,11 +238,11 @@ public SubmissionResponse(URI submissionUrl, URI pasteUrl) {
234
238
this .pasteUrl = pasteUrl ;
235
239
}
236
240
}
237
-
241
+
238
242
public CancellableCallable <String > getSubmissionFetchTask (String submissionUrl ) {
239
243
return createHttpTasks ().getForText (submissionUrl );
240
244
}
241
-
245
+
242
246
public CancellableCallable <List <Review >> getDownloadingReviewListTask (Course course ) {
243
247
String url = addApiCallQueryParameters (course .getReviewsUrl ());
244
248
final CancellableCallable <String > download = createHttpTasks ().getForText (url );
@@ -259,7 +263,7 @@ public boolean cancel() {
259
263
}
260
264
};
261
265
}
262
-
266
+
263
267
public CancellableCallable <Void > getMarkingReviewAsReadTask (Review review , boolean read ) {
264
268
String url = addApiCallQueryParameters (review .getUpdateUrl () + ".json" );
265
269
Map <String , String > params = new HashMap <String , String >();
@@ -269,7 +273,7 @@ public CancellableCallable<Void> getMarkingReviewAsReadTask(Review review, boole
269
273
} else {
270
274
params .put ("mark_as_unread" , "1" );
271
275
}
272
-
276
+
273
277
final CancellableCallable <String > task = createHttpTasks ().postForText (url , params );
274
278
return new CancellableCallable <Void >() {
275
279
@ Override
@@ -284,20 +288,20 @@ public boolean cancel() {
284
288
}
285
289
};
286
290
}
287
-
291
+
288
292
public CancellableCallable <String > getFeedbackAnsweringJob (String answerUrl , List <FeedbackAnswer > answers ) {
289
293
final String submitUrl = addApiCallQueryParameters (answerUrl );
290
-
294
+
291
295
Map <String , String > params = new HashMap <String , String >();
292
296
for (int i = 0 ; i < answers .size (); ++i ) {
293
297
String keyPrefix = "answers[" + i + "]" ;
294
298
FeedbackAnswer answer = answers .get (i );
295
299
params .put (keyPrefix + "[question_id]" , "" + answer .getQuestion ().getId ());
296
300
params .put (keyPrefix + "[answer]" , answer .getAnswer ());
297
301
}
298
-
302
+
299
303
final CancellableCallable <String > upload = createHttpTasks ().postForText (submitUrl , params );
300
-
304
+
301
305
return new CancellableCallable <String >() {
302
306
@ Override
303
307
public String call () throws Exception {
@@ -314,7 +318,7 @@ public boolean cancel() {
314
318
}
315
319
};
316
320
}
317
-
321
+
318
322
public CancellableCallable <Object > getSendEventLogJob (String spywareServerUrl , List <LoggableEvent > events ) {
319
323
String url = addApiCallQueryParameters (spywareServerUrl );
320
324
@@ -361,7 +365,7 @@ public byte[] eventListToPostBody(List<LoggableEvent> events) throws IOException
361
365
362
366
return bufferBos .toByteArray ();
363
367
}
364
-
368
+
365
369
private <T > T checkForObsoleteClient (FailedHttpResponseException ex ) throws ObsoleteClientException , FailedHttpResponseException {
366
370
if (ex .getStatusCode () == 404 ) {
367
371
boolean obsolete ;
@@ -377,4 +381,4 @@ private <T> T checkForObsoleteClient(FailedHttpResponseException ex) throws Obso
377
381
378
382
throw ex ;
379
383
}
380
- }
384
+ }
0 commit comments