Skip to content
Permalink
Browse files
Made cookie jar accessible so I can put stuff in it. This isn't going…
… to solve my autowiring problem though.
  • Loading branch information
mifosio-04-04-2018 committed May 15, 2017
1 parent 3a4a2b7 commit 394486ea41df1b9bd8a6fc1e425052a0f7901ea0
Showing 4 changed files with 75 additions and 0 deletions.
@@ -52,4 +52,20 @@ public <T> T create(final Class<T> clazz, final String target) {
.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);
}
}
@@ -48,6 +48,16 @@ RequestInterceptor getCookieInterceptor() {
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) {
@@ -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;
}
}
@@ -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"));
}
}

0 comments on commit 394486e

Please sign in to comment.