From commits-return-8209-archive-asf-public=cust-asf.ponee.io@fineract.apache.org Tue Apr 21 14:51:41 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 5BFA518065D for ; Tue, 21 Apr 2020 16:51:41 +0200 (CEST) Received: (qmail 75314 invoked by uid 500); 21 Apr 2020 14:51:38 -0000 Mailing-List: contact commits-help@fineract.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@fineract.apache.org Delivered-To: mailing list commits@fineract.apache.org Received: (qmail 74912 invoked by uid 99); 21 Apr 2020 14:51:37 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Apr 2020 14:51:37 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id 9814C8B6CF; Tue, 21 Apr 2020 14:51:37 +0000 (UTC) Date: Tue, 21 Apr 2020 14:51:46 +0000 To: "commits@fineract.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. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: juhan@apache.org In-Reply-To: <158748069727.19051.9106929165590100004@gitbox.apache.org> References: <158748069727.19051.9106929165590100004@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: fineract-cn-api X-Git-Refname: refs/heads/spring_boot_2 X-Git-Reftype: branch X-Git-Rev: 394486ea41df1b9bd8a6fc1e425052a0f7901ea0 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20200421145137.9814C8B6CF@gitbox.apache.org> 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 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 FeignTargetWithCookieJar createWithCookieJar(final Class 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> 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 { + 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