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 T create(final Class clazz, final String target) { .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 @@ RequestInterceptor getCookieInterceptor() { 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 void unexpectedCookieManagerFailure() throws IOException { 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