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 ;
3135import java .util .List ;
3236import java .util .Map ;
3337import java .util .zip .GZIPOutputStream ;
34- import org .openide .modules .Modules ;
3538
3639/**
3740 * A frontend for the server.
41+ *
42+ * @deprecated Users are suggested to rely on the tmc-core instead of this implementation.
3843 */
44+ @ Deprecated
3945public class ServerAccess {
4046 public static final int API_VERSION = 7 ;
41-
47+
4248 private NbTmcSettings settings ;
4349 private CourseListParser courseListParser ;
4450 private CourseInfoParser courseInfoParser ;
4551 private ReviewListParser reviewListParser ;
4652 private String clientVersion ;
4753 private TmcCore tmcCore ;
48-
54+
4955 public ServerAccess () {
5056 this (NbTmcSettings .getDefault ());
5157 }
@@ -66,30 +72,30 @@ public ServerAccess(
6672 this .reviewListParser = reviewListParser ;
6773 this .clientVersion = getClientVersion ();
6874 }
69-
75+
7076 private static String getClientVersion () {
7177 return Modules .getDefault ().ownerOf (ServerAccess .class ).getSpecificationVersion ().toString ();
7278 }
73-
79+
7480 public void setSettings (NbTmcSettings settings ) {
7581 this .settings = settings ;
7682 }
77-
83+
7884 private String getCourseListUrl () {
7985 return addApiCallQueryParameters (settings .getServerAddress () + "/courses.json" );
8086 }
81-
87+
8288 public String addApiCallQueryParameters (String url ) {
8389 url = UriUtils .withQueryParam (url , "api_version" , "" +API_VERSION );
8490 url = UriUtils .withQueryParam (url , "client" , "netbeans_plugin" );
8591 url = UriUtils .withQueryParam (url , "client_version" , getClientVersion ());
8692 return url ;
8793 }
88-
94+
8995 private HttpTasks createHttpTasks () {
9096 return new HttpTasks ().setCredentials (settings .getUsername (), settings .getPassword ());
9197 }
92-
98+
9399 public boolean hasEnoughSettings () {
94100 return
95101 !settings .getUsername ().isEmpty () &&
@@ -103,7 +109,7 @@ public boolean needsOnlyPassword() {
103109 settings .getPassword ().isEmpty () &&
104110 !settings .getServerAddress ().isEmpty ();
105111 }
106-
112+
107113 @ Deprecated
108114 public CancellableCallable <List <Course >> getDownloadingCourseListTask () {
109115 final CancellableCallable <String > download = createHttpTasks ().getForText (getCourseListUrl ());
@@ -146,7 +152,7 @@ public boolean cancel() {
146152 }
147153 };
148154 }
149-
155+
150156 public CancellableCallable <Void > getUnlockingTask (Course course ) {
151157 Map <String , String > params = Collections .emptyMap ();
152158 final CancellableCallable <String > download = createHttpTasks ().postForText (getUnlockUrl (course ), params );
@@ -167,16 +173,16 @@ public boolean cancel() {
167173 }
168174 };
169175 }
170-
176+
171177 private String getUnlockUrl (Course course ) {
172178 return addApiCallQueryParameters (course .getUnlockUrl ());
173179 }
174-
180+
175181 public CancellableCallable <byte []> getDownloadingExerciseZipTask (Exercise exercise ) {
176182 String zipUrl = exercise .getDownloadUrl ();
177183 return createHttpTasks ().getForBinary (zipUrl );
178184 }
179-
185+
180186 public CancellableCallable <byte []> getDownloadingExerciseSolutionZipTask (Exercise exercise ) {
181187 String zipUrl = exercise .getSolutionDownloadUrl ();
182188 return createHttpTasks ().getForBinary (zipUrl );
@@ -192,7 +198,7 @@ public CancellableCallable<SubmissionResponse> getSubmittingExerciseTask(final E
192198
193199 final CancellableCallable <String > upload =
194200 createHttpTasks ().uploadFileForTextDownload (submitUrl , params , "submission[file]" , sourceZip );
195-
201+
196202 return new CancellableCallable <SubmissionResponse >() {
197203 @ Override
198204 public SubmissionResponse call () throws Exception {
@@ -202,7 +208,7 @@ public SubmissionResponse call() throws Exception {
202208 } catch (FailedHttpResponseException ex ) {
203209 return checkForObsoleteClient (ex );
204210 }
205-
211+
206212 JsonObject respJson = new JsonParser ().parse (response ).getAsJsonObject ();
207213 if (respJson .get ("error" ) != null ) {
208214 throw new RuntimeException ("Server responded with error: " + respJson .get ("error" ));
@@ -234,11 +240,11 @@ public SubmissionResponse(URI submissionUrl, URI pasteUrl) {
234240 this .pasteUrl = pasteUrl ;
235241 }
236242 }
237-
243+
238244 public CancellableCallable <String > getSubmissionFetchTask (String submissionUrl ) {
239245 return createHttpTasks ().getForText (submissionUrl );
240246 }
241-
247+
242248 public CancellableCallable <List <Review >> getDownloadingReviewListTask (Course course ) {
243249 String url = addApiCallQueryParameters (course .getReviewsUrl ());
244250 final CancellableCallable <String > download = createHttpTasks ().getForText (url );
@@ -259,7 +265,7 @@ public boolean cancel() {
259265 }
260266 };
261267 }
262-
268+
263269 public CancellableCallable <Void > getMarkingReviewAsReadTask (Review review , boolean read ) {
264270 String url = addApiCallQueryParameters (review .getUpdateUrl () + ".json" );
265271 Map <String , String > params = new HashMap <String , String >();
@@ -269,7 +275,7 @@ public CancellableCallable<Void> getMarkingReviewAsReadTask(Review review, boole
269275 } else {
270276 params .put ("mark_as_unread" , "1" );
271277 }
272-
278+
273279 final CancellableCallable <String > task = createHttpTasks ().postForText (url , params );
274280 return new CancellableCallable <Void >() {
275281 @ Override
@@ -284,20 +290,20 @@ public boolean cancel() {
284290 }
285291 };
286292 }
287-
293+
288294 public CancellableCallable <String > getFeedbackAnsweringJob (String answerUrl , List <FeedbackAnswer > answers ) {
289295 final String submitUrl = addApiCallQueryParameters (answerUrl );
290-
296+
291297 Map <String , String > params = new HashMap <String , String >();
292298 for (int i = 0 ; i < answers .size (); ++i ) {
293299 String keyPrefix = "answers[" + i + "]" ;
294300 FeedbackAnswer answer = answers .get (i );
295301 params .put (keyPrefix + "[question_id]" , "" + answer .getQuestion ().getId ());
296302 params .put (keyPrefix + "[answer]" , answer .getAnswer ());
297303 }
298-
304+
299305 final CancellableCallable <String > upload = createHttpTasks ().postForText (submitUrl , params );
300-
306+
301307 return new CancellableCallable <String >() {
302308 @ Override
303309 public String call () throws Exception {
@@ -314,7 +320,7 @@ public boolean cancel() {
314320 }
315321 };
316322 }
317-
323+
318324 public CancellableCallable <Object > getSendEventLogJob (String spywareServerUrl , List <LoggableEvent > events ) {
319325 String url = addApiCallQueryParameters (spywareServerUrl );
320326
@@ -361,7 +367,7 @@ public byte[] eventListToPostBody(List<LoggableEvent> events) throws IOException
361367
362368 return bufferBos .toByteArray ();
363369 }
364-
370+
365371 private <T > T checkForObsoleteClient (FailedHttpResponseException ex ) throws ObsoleteClientException , FailedHttpResponseException {
366372 if (ex .getStatusCode () == 404 ) {
367373 boolean obsolete ;
@@ -377,4 +383,4 @@ private <T> T checkForObsoleteClient(FailedHttpResponseException ex) throws Obso
377383
378384 throw ex ;
379385 }
380- }
386+ }
0 commit comments