Initialize client only if client is null in EduStepicConnector
authorValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Tue, 14 Jun 2016 11:21:23 +0000 (14:21 +0300)
committerValentina Kiryushkina <valentina.kiryushkina@jetbrains.com>
Tue, 14 Jun 2016 11:21:23 +0000 (14:21 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduAdaptiveStepicConnector.java
python/educational-core/student/src/com/jetbrains/edu/learning/stepic/EduStepicConnector.java

index a1a4a62c813b040ad833984ebf1da388d4e5d404..0e200d8b5add460d57584a2c599c91a6eefcd85a 100644 (file)
@@ -160,7 +160,6 @@ public class EduAdaptiveStepicConnector {
 
   public static boolean postRecommendationReaction(@NotNull final Project project, @NotNull final String lessonId,
                                                    @NotNull final String user, int reaction) {
-
     final HttpPost post = new HttpPost(STEPIC_API_URL + RECOMMENDATION_REACTIONS_URL);
     final String json = new Gson()
       .toJson(new StepicWrappers.RecommendationReactionWrapper(new StepicWrappers.RecommendationReaction(reaction, user, lessonId)));
index bb464476407a211483361acdd3f4a2a5af849032..8326b670e12c23ad3173979792e234d189e511d3 100644 (file)
@@ -112,37 +112,46 @@ public class EduStepicConnector {
   }
 
   public static void initializeClient() {
-    final HttpGet request = new HttpGet(stepicUrl);
-    request.addHeader(new BasicHeader("referer", "https://stepic.org"));
-    request.addHeader(new BasicHeader("content-type", "application/json"));
+    if (ourClient == null) {
+      final HttpGet request = new HttpGet(stepicUrl);
+      request.addHeader(new BasicHeader("referer", "https://stepic.org"));
+      request.addHeader(new BasicHeader("content-type", "application/json"));
 
 
-    HttpClientBuilder builder = HttpClients.custom().setSslcontext(CertificateManager.getInstance().getSslContext()).setMaxConnPerRoute(100000).
-      setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE);
-    ourCookieStore = new BasicCookieStore();
+      HttpClientBuilder builder =
+        HttpClients.custom().setSslcontext(CertificateManager.getInstance().getSslContext()).setMaxConnPerRoute(100000).
+          setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE);
+      ourCookieStore = new BasicCookieStore();
 
-    try {
-      // Create a trust manager that does not validate certificate for this connection
-      TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
-        public X509Certificate[] getAcceptedIssuers() { return null; }
-        public void checkClientTrusted(X509Certificate[] certs, String authType) {}
-        public void checkServerTrusted(X509Certificate[] certs, String authType) {}
-      }};
-      SSLContext sslContext = SSLContext.getInstance("TLS");
-      sslContext.init(null, trustAllCerts, new SecureRandom());
-      ourClient = builder.setDefaultCookieStore(ourCookieStore).setSslcontext(sslContext).build();
-
-      ourClient.execute(request);
-      saveCSRFToken();
-    }
-    catch (IOException e) {
-      LOG.error(e.getMessage());
-    }
-    catch (NoSuchAlgorithmException e) {
-      LOG.error(e.getMessage());
-    }
-    catch (KeyManagementException e) {
-      LOG.error(e.getMessage());
+      try {
+        // Create a trust manager that does not validate certificate for this connection
+        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
+          public X509Certificate[] getAcceptedIssuers() {
+            return null;
+          }
+
+          public void checkClientTrusted(X509Certificate[] certs, String authType) {
+          }
+
+          public void checkServerTrusted(X509Certificate[] certs, String authType) {
+          }
+        }};
+        SSLContext sslContext = SSLContext.getInstance("TLS");
+        sslContext.init(null, trustAllCerts, new SecureRandom());
+        ourClient = builder.setDefaultCookieStore(ourCookieStore).setSslcontext(sslContext).build();
+
+        ourClient.execute(request);
+        saveCSRFToken();
+      }
+      catch (IOException e) {
+        LOG.error(e.getMessage());
+      }
+      catch (NoSuchAlgorithmException e) {
+        LOG.error(e.getMessage());
+      }
+      catch (KeyManagementException e) {
+        LOG.error(e.getMessage());
+      }
     }
   }