11package fi .helsinki .cs .tmc .model ;
22
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 ;
83import fi .helsinki .cs .tmc .core .domain .Course ;
94import fi .helsinki .cs .tmc .core .domain .Exercise ;
105import fi .helsinki .cs .tmc .data .FeedbackAnswer ;
2015import fi .helsinki .cs .tmc .utilities .http .FailedHttpResponseException ;
2116import fi .helsinki .cs .tmc .utilities .http .HttpTasks ;
2217import 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+
2327import java .io .ByteArrayOutputStream ;
2428import java .io .IOException ;
2529import java .io .OutputStreamWriter ;
@@ -66,30 +70,30 @@ public ServerAccess(
6670 this .reviewListParser = reviewListParser ;
6771 this .clientVersion = getClientVersion ();
6872 }
69-
73+
7074 private static String getClientVersion () {
7175 return Modules .getDefault ().ownerOf (ServerAccess .class ).getSpecificationVersion ().toString ();
7276 }
73-
77+
7478 public void setSettings (NbTmcSettings settings ) {
7579 this .settings = settings ;
7680 }
77-
81+
7882 private String getCourseListUrl () {
7983 return addApiCallQueryParameters (settings .getServerAddress () + "/courses.json" );
8084 }
81-
85+
8286 public String addApiCallQueryParameters (String url ) {
8387 url = UriUtils .withQueryParam (url , "api_version" , "" +API_VERSION );
8488 url = UriUtils .withQueryParam (url , "client" , "netbeans_plugin" );
8589 url = UriUtils .withQueryParam (url , "client_version" , getClientVersion ());
8690 return url ;
8791 }
88-
92+
8993 private HttpTasks createHttpTasks () {
9094 return new HttpTasks ().setCredentials (settings .getUsername (), settings .getPassword ());
9195 }
92-
96+
9397 public boolean hasEnoughSettings () {
9498 return
9599 !settings .getUsername ().isEmpty () &&
@@ -103,7 +107,7 @@ public boolean needsOnlyPassword() {
103107 settings .getPassword ().isEmpty () &&
104108 !settings .getServerAddress ().isEmpty ();
105109 }
106-
110+
107111 @ Deprecated
108112 public CancellableCallable <List <Course >> getDownloadingCourseListTask () {
109113 final CancellableCallable <String > download = createHttpTasks ().getForText (getCourseListUrl ());
@@ -146,7 +150,7 @@ public boolean cancel() {
146150 }
147151 };
148152 }
149-
153+
150154 public CancellableCallable <Void > getUnlockingTask (Course course ) {
151155 Map <String , String > params = Collections .emptyMap ();
152156 final CancellableCallable <String > download = createHttpTasks ().postForText (getUnlockUrl (course ), params );
@@ -167,16 +171,16 @@ public boolean cancel() {
167171 }
168172 };
169173 }
170-
174+
171175 private String getUnlockUrl (Course course ) {
172176 return addApiCallQueryParameters (course .getUnlockUrl ());
173177 }
174-
178+
175179 public CancellableCallable <byte []> getDownloadingExerciseZipTask (Exercise exercise ) {
176180 String zipUrl = exercise .getDownloadUrl ();
177181 return createHttpTasks ().getForBinary (zipUrl );
178182 }
179-
183+
180184 public CancellableCallable <byte []> getDownloadingExerciseSolutionZipTask (Exercise exercise ) {
181185 String zipUrl = exercise .getSolutionDownloadUrl ();
182186 return createHttpTasks ().getForBinary (zipUrl );
@@ -192,7 +196,7 @@ public CancellableCallable<SubmissionResponse> getSubmittingExerciseTask(final E
192196
193197 final CancellableCallable <String > upload =
194198 createHttpTasks ().uploadFileForTextDownload (submitUrl , params , "submission[file]" , sourceZip );
195-
199+
196200 return new CancellableCallable <SubmissionResponse >() {
197201 @ Override
198202 public SubmissionResponse call () throws Exception {
@@ -202,7 +206,7 @@ public SubmissionResponse call() throws Exception {
202206 } catch (FailedHttpResponseException ex ) {
203207 return checkForObsoleteClient (ex );
204208 }
205-
209+
206210 JsonObject respJson = new JsonParser ().parse (response ).getAsJsonObject ();
207211 if (respJson .get ("error" ) != null ) {
208212 throw new RuntimeException ("Server responded with error: " + respJson .get ("error" ));
@@ -234,11 +238,11 @@ public SubmissionResponse(URI submissionUrl, URI pasteUrl) {
234238 this .pasteUrl = pasteUrl ;
235239 }
236240 }
237-
241+
238242 public CancellableCallable <String > getSubmissionFetchTask (String submissionUrl ) {
239243 return createHttpTasks ().getForText (submissionUrl );
240244 }
241-
245+
242246 public CancellableCallable <List <Review >> getDownloadingReviewListTask (Course course ) {
243247 String url = addApiCallQueryParameters (course .getReviewsUrl ());
244248 final CancellableCallable <String > download = createHttpTasks ().getForText (url );
@@ -259,7 +263,7 @@ public boolean cancel() {
259263 }
260264 };
261265 }
262-
266+
263267 public CancellableCallable <Void > getMarkingReviewAsReadTask (Review review , boolean read ) {
264268 String url = addApiCallQueryParameters (review .getUpdateUrl () + ".json" );
265269 Map <String , String > params = new HashMap <String , String >();
@@ -269,7 +273,7 @@ public CancellableCallable<Void> getMarkingReviewAsReadTask(Review review, boole
269273 } else {
270274 params .put ("mark_as_unread" , "1" );
271275 }
272-
276+
273277 final CancellableCallable <String > task = createHttpTasks ().postForText (url , params );
274278 return new CancellableCallable <Void >() {
275279 @ Override
@@ -284,20 +288,20 @@ public boolean cancel() {
284288 }
285289 };
286290 }
287-
291+
288292 public CancellableCallable <String > getFeedbackAnsweringJob (String answerUrl , List <FeedbackAnswer > answers ) {
289293 final String submitUrl = addApiCallQueryParameters (answerUrl );
290-
294+
291295 Map <String , String > params = new HashMap <String , String >();
292296 for (int i = 0 ; i < answers .size (); ++i ) {
293297 String keyPrefix = "answers[" + i + "]" ;
294298 FeedbackAnswer answer = answers .get (i );
295299 params .put (keyPrefix + "[question_id]" , "" + answer .getQuestion ().getId ());
296300 params .put (keyPrefix + "[answer]" , answer .getAnswer ());
297301 }
298-
302+
299303 final CancellableCallable <String > upload = createHttpTasks ().postForText (submitUrl , params );
300-
304+
301305 return new CancellableCallable <String >() {
302306 @ Override
303307 public String call () throws Exception {
@@ -314,7 +318,7 @@ public boolean cancel() {
314318 }
315319 };
316320 }
317-
321+
318322 public CancellableCallable <Object > getSendEventLogJob (String spywareServerUrl , List <LoggableEvent > events ) {
319323 String url = addApiCallQueryParameters (spywareServerUrl );
320324
@@ -361,7 +365,7 @@ public byte[] eventListToPostBody(List<LoggableEvent> events) throws IOException
361365
362366 return bufferBos .toByteArray ();
363367 }
364-
368+
365369 private <T > T checkForObsoleteClient (FailedHttpResponseException ex ) throws ObsoleteClientException , FailedHttpResponseException {
366370 if (ex .getStatusCode () == 404 ) {
367371 boolean obsolete ;
@@ -377,4 +381,4 @@ private <T> T checkForObsoleteClient(FailedHttpResponseException ex) throws Obso
377381
378382 throw ex ;
379383 }
380- }
384+ }
0 commit comments