fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject [fineract-cn-api] 09/44: Made cookie jar accessible so I can put stuff in it. This isn't going to solve my autowiring problem though.
Date Tue, 21 Apr 2020 14:51:46 GMT
This is an automated email from the ASF dual-hosted git repository.

juhan pushed a commit to branch spring_boot_2
in repository https://gitbox.apache.org/repos/asf/fineract-cn-api.git

commit 394486ea41df1b9bd8a6fc1e425052a0f7901ea0
Author: myrle-krantz <mkrantz@mifos.org>
AuthorDate: Mon May 15 09:16:04 2017 +0200

    Made cookie jar accessible so I can put stuff in it.  This isn't going to solve my autowiring
problem though.
---
 .../java/io/mifos/core/api/util/ApiFactory.java    | 16 +++++++++
 .../core/api/util/CookieInterceptingClient.java    | 10 ++++++
 .../core/api/util/FeignTargetWithCookieJar.java    | 38 ++++++++++++++++++++++
 .../api/util/CookieInterceptingClientTest.java     | 11 +++++++
 4 files changed, 75 insertions(+)

diff --git a/src/main/java/io/mifos/core/api/util/ApiFactory.java b/src/main/java/io/mifos/core/api/util/ApiFactory.java
index aa8a7bf..a2535c1 100644
--- a/src/main/java/io/mifos/core/api/util/ApiFactory.java
+++ b/src/main/java/io/mifos/core/api/util/ApiFactory.java
@@ -52,4 +52,20 @@ public class ApiFactory {
         .encoder(new GsonEncoder())
         .target(clazz, target);
   }
+
+  public <T> FeignTargetWithCookieJar<T> createWithCookieJar(final Class<T>
clazz, final String target) {
+    final CookieInterceptingClient client = new CookieInterceptingClient(target);
+    final T feignTarget = Feign.builder()
+            .contract(new SpringMvcContract())
+            .client(client)
+            .errorDecoder(new AnnotatedErrorDecoder(logger, clazz))
+            .requestInterceptor(new TenantedTargetInterceptor())
+            .requestInterceptor(new TokenedTargetInterceptor())
+            .requestInterceptor(client.getCookieInterceptor())
+            .decoder(new GsonDecoder())
+            .encoder(new GsonEncoder())
+            .target(clazz, target);
+
+    return new FeignTargetWithCookieJar<>(feignTarget, client);
+  }
 }
diff --git a/src/main/java/io/mifos/core/api/util/CookieInterceptingClient.java b/src/main/java/io/mifos/core/api/util/CookieInterceptingClient.java
index 98ef622..408a4d7 100644
--- a/src/main/java/io/mifos/core/api/util/CookieInterceptingClient.java
+++ b/src/main/java/io/mifos/core/api/util/CookieInterceptingClient.java
@@ -48,6 +48,16 @@ class CookieInterceptingClient extends Client.Default {
     return new CookieInterceptor();
   }
 
+  void putCookie(final String relativeUrl, final String cookieName, final String cookieValue)
{
+    try {
+      final Map<String, List<String>> map = new HashMap<>();
+      map.put("Set-Cookie", Collections.singletonList(cookieName + "=" + cookieValue));
+      cookieManager.put(mapUriType(target + relativeUrl), map);
+    } catch (final IOException e) {
+      throw new IllegalStateException("Mapping cookies failed unexpectedly.");
+    }
+  }
+
   private class CookieInterceptor implements RequestInterceptor {
     @Override
     public void apply(final RequestTemplate template) {
diff --git a/src/main/java/io/mifos/core/api/util/FeignTargetWithCookieJar.java b/src/main/java/io/mifos/core/api/util/FeignTargetWithCookieJar.java
new file mode 100644
index 0000000..2a9b661
--- /dev/null
+++ b/src/main/java/io/mifos/core/api/util/FeignTargetWithCookieJar.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.core.api.util;
+
+/**
+ * @author Myrle Krantz
+ */
+@SuppressWarnings({"WeakerAccess", "unused"})
+public class FeignTargetWithCookieJar<T> {
+  private final T feignTarget;
+  private final CookieInterceptingClient cookieInterceptor;
+
+  FeignTargetWithCookieJar(final T feignTarget, final CookieInterceptingClient cookieInterceptor)
{
+    this.feignTarget = feignTarget;
+    this.cookieInterceptor = cookieInterceptor;
+  }
+
+  public void putCookie(final String relativeUrl, final String cookieName, final String cookieValue)
{
+    this.cookieInterceptor.putCookie(relativeUrl, cookieName, cookieValue);
+  }
+
+  public T getFeignTarget() {
+    return feignTarget;
+  }
+}
diff --git a/src/test/java/io/mifos/core/api/util/CookieInterceptingClientTest.java b/src/test/java/io/mifos/core/api/util/CookieInterceptingClientTest.java
index 9d9cb33..ebbf23f 100644
--- a/src/test/java/io/mifos/core/api/util/CookieInterceptingClientTest.java
+++ b/src/test/java/io/mifos/core/api/util/CookieInterceptingClientTest.java
@@ -78,4 +78,15 @@ public class CookieInterceptingClientTest {
 
     testSubject.getCookieInterceptor().apply(dummyRequestTemplate);
   }
+
+  @Test()
+  public void setCookieBetweenRemoteCalls() {
+    final CookieInterceptingClient testSubject = new CookieInterceptingClient(TEST_URL);
+    testSubject.putCookie("/blah", "token", "Bearerbear");
+    //request
+    final RequestTemplate dummyRequestTemplate = new RequestTemplate();
+    dummyRequestTemplate.append("/request");
+    testSubject.getCookieInterceptor().apply(dummyRequestTemplate);
+    Assert.assertEquals(Collections.singletonList("token=Bearerbear"), dummyRequestTemplate.headers().get("Cookie"));
+  }
 }
\ No newline at end of file


Mime
View raw message