Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from myrle-krantz/develop
Browse files Browse the repository at this point in the history
Made cookie jar accessible
  • Loading branch information
myrle-krantz committed May 15, 2017
2 parents 78e28e6 + 2399a6c commit 5b7e04e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/io/mifos/core/api/util/ApiFactory.java
Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/mifos/core/api/util/CookieInterceptingClient.java
Expand Up @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions 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;
}
}
Expand Up @@ -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 5b7e04e

Please sign in to comment.