From 6e758d721d5c1ce958b84ab9e18d95fa6c92c317 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 15 Jun 2015 14:21:12 +0200 Subject: [PATCH] Fix CDI examples --- .../example/oauth/AppContextListener.java | 1 + .../keycloak/example/oauth/DatabaseClient.java | 16 ++++------------ examples/demo-template/third-party/pom.xml | 1 + .../example/oauth/ProductDatabaseClient.java | 6 +++++- .../WEB-INF/jboss-deployment-structure.xml | 1 + 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/AppContextListener.java b/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/AppContextListener.java index 1fe4a2ff71..bd6a395242 100755 --- a/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/AppContextListener.java +++ b/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/AppContextListener.java @@ -41,6 +41,7 @@ public class AppContextListener implements ServletContextListener { } ServletOAuthClientBuilder.build(is, oauthClient); logger.info("OAuth client configured and started"); + oauthClient.start(); } @Override diff --git a/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/DatabaseClient.java b/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/DatabaseClient.java index 6b96f6a97f..0a440374b8 100755 --- a/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/DatabaseClient.java +++ b/examples/demo-template/third-party-cdi/src/main/java/org/keycloak/example/oauth/DatabaseClient.java @@ -6,6 +6,8 @@ import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.jboss.logging.Logger; +import org.keycloak.KeycloakSecurityContext; +import org.keycloak.adapters.AdapterUtils; import org.keycloak.servlet.ServletOAuthClient; import org.keycloak.util.JsonSerialization; import org.keycloak.util.UriUtils; @@ -104,18 +106,8 @@ public class DatabaseClient { } public String getBaseUrl() { - switch (oauthClient.getRelativeUrlsUsed()) { - case ALL_REQUESTS: - // Resolve baseURI from the request - return UriUtils.getOrigin(request.getRequestURL().toString()); - case BROWSER_ONLY: - // Resolve baseURI from the codeURL (This is already non-relative and based on our hostname) - return UriUtils.getOrigin(oauthClient.getTokenUrl()); - case NEVER: - return ""; - default: - return ""; - } + KeycloakSecurityContext session = (KeycloakSecurityContext)request.getAttribute(KeycloakSecurityContext.class.getName()); + return AdapterUtils.getOriginForRestCalls(request.getRequestURL().toString(), session); } } diff --git a/examples/demo-template/third-party/pom.xml b/examples/demo-template/third-party/pom.xml index 6ec11d9e81..e5e3ac45a2 100755 --- a/examples/demo-template/third-party/pom.xml +++ b/examples/demo-template/third-party/pom.xml @@ -34,6 +34,7 @@ org.keycloak keycloak-servlet-oauth-client + provided org.apache.httpcomponents diff --git a/examples/demo-template/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java b/examples/demo-template/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java index e47716b616..b0e0e00af8 100755 --- a/examples/demo-template/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java +++ b/examples/demo-template/third-party/src/main/java/org/keycloak/example/oauth/ProductDatabaseClient.java @@ -5,6 +5,8 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; +import org.keycloak.KeycloakSecurityContext; +import org.keycloak.adapters.AdapterUtils; import org.keycloak.adapters.ServerRequest; import org.keycloak.representations.AccessTokenResponse; import org.keycloak.servlet.ServletOAuthClient; @@ -71,6 +73,8 @@ public class ProductDatabaseClient { } public static List getProducts(HttpServletRequest request, String accessToken) throws Failure { + KeycloakSecurityContext session = (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName()); + // The ServletOAuthClient is obtained by getting a context attribute // that is set in the Bootstrap context listener in this project. // You really should come up with a better way to initialize @@ -79,7 +83,7 @@ public class ProductDatabaseClient { ServletOAuthClient oAuthClient = (ServletOAuthClient) request.getServletContext().getAttribute(ServletOAuthClient.class.getName()); HttpClient client = new DefaultHttpClient(); - HttpGet get = new HttpGet(getBaseUrl(oAuthClient, request) + "/database/products"); + HttpGet get = new HttpGet(AdapterUtils.getOriginForRestCalls(request.getRequestURL().toString(), session) + "/database/products"); get.addHeader("Authorization", "Bearer " + accessToken); try { HttpResponse response = client.execute(get); diff --git a/examples/demo-template/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/examples/demo-template/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml index 9c1bac9b36..2ee04e01a7 100755 --- a/examples/demo-template/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml +++ b/examples/demo-template/third-party/src/main/webapp/WEB-INF/jboss-deployment-structure.xml @@ -4,6 +4,7 @@ + \ No newline at end of file