From bda57d00df3af22c83870d3a0c7165a721decb5e Mon Sep 17 00:00:00 2001 From: pedroigor Date: Wed, 31 Jan 2018 13:42:30 -0200 Subject: [PATCH] [KEYCLOAK-6164] - Authorization services client doesn't work with JDK7 --- authz/client/pom.xml | 2 -- .../authorization/client/AuthzClient.java | 10 +++++----- .../client/resource/PermissionResource.java | 8 ++++---- .../client/resource/ProtectedResource.java | 18 +++++++++--------- .../client/resource/ProtectionResource.java | 6 +++--- authz/policy/pom.xml | 5 +++++ authz/pom.xml | 6 ------ .../photoz/admin/AdminAlbumService.java | 9 ++++++++- .../example/photoz/album/AlbumService.java | 6 ++++-- .../keycloak/example/photoz/entity/Album.java | 8 +++----- examples/authz/pom.xml | 4 ++-- 11 files changed, 43 insertions(+), 39 deletions(-) diff --git a/authz/client/pom.xml b/authz/client/pom.xml index 4c858a9226..f5a9cf3d19 100644 --- a/authz/client/pom.xml +++ b/authz/client/pom.xml @@ -18,8 +18,6 @@ KeyCloak AuthZ: Client API - 1.7 - 1.7 org.keycloak.authorization.client.* diff --git a/authz/client/src/main/java/org/keycloak/authorization/client/AuthzClient.java b/authz/client/src/main/java/org/keycloak/authorization/client/AuthzClient.java index cb7389a829..1113fd2136 100644 --- a/authz/client/src/main/java/org/keycloak/authorization/client/AuthzClient.java +++ b/authz/client/src/main/java/org/keycloak/authorization/client/AuthzClient.java @@ -30,7 +30,7 @@ import org.keycloak.util.JsonSerialization; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.util.function.Supplier; +import java.util.concurrent.Callable; /** *

This is class serves as an entry point for clients looking for access to Keycloak Authorization Services. @@ -40,7 +40,7 @@ import java.util.function.Supplier; public class AuthzClient { private final Http http; - private Supplier patSupplier; + private Callable patSupplier; public static AuthzClient create() { InputStream configStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("keycloak.json"); @@ -141,13 +141,13 @@ public class AuthzClient { return this.deployment; } - private Supplier createPatSupplier() { + private Callable createPatSupplier() { if (patSupplier == null) { - patSupplier = new Supplier() { + patSupplier = new Callable() { AccessTokenResponse clientToken = obtainAccessToken(); @Override - public String get() { + public String call() { String token = clientToken.getToken(); try { diff --git a/authz/client/src/main/java/org/keycloak/authorization/client/resource/PermissionResource.java b/authz/client/src/main/java/org/keycloak/authorization/client/resource/PermissionResource.java index 5aaccda7a2..785a3a6d4b 100644 --- a/authz/client/src/main/java/org/keycloak/authorization/client/resource/PermissionResource.java +++ b/authz/client/src/main/java/org/keycloak/authorization/client/resource/PermissionResource.java @@ -19,7 +19,7 @@ package org.keycloak.authorization.client.resource; import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException; -import java.util.function.Supplier; +import java.util.concurrent.Callable; import org.keycloak.authorization.client.representation.PermissionRequest; import org.keycloak.authorization.client.representation.PermissionResponse; @@ -32,9 +32,9 @@ import org.keycloak.util.JsonSerialization; public class PermissionResource { private final Http http; - private final Supplier pat; + private final Callable pat; - public PermissionResource(Http http, Supplier pat) { + public PermissionResource(Http http, Callable pat) { this.http = http; this.pat = pat; } @@ -42,7 +42,7 @@ public class PermissionResource { public PermissionResponse forResource(PermissionRequest request) { try { return this.http.post("/authz/protection/permission") - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .json(JsonSerialization.writeValueAsBytes(request)) .response().json(PermissionResponse.class).execute(); } catch (Exception cause) { diff --git a/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectedResource.java b/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectedResource.java index d774c3d73d..fcf1e436f8 100644 --- a/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectedResource.java +++ b/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectedResource.java @@ -20,7 +20,7 @@ package org.keycloak.authorization.client.resource; import static org.keycloak.authorization.client.util.Throwables.handleAndWrapException; import java.util.Set; -import java.util.function.Supplier; +import java.util.concurrent.Callable; import org.keycloak.authorization.client.representation.RegistrationResponse; import org.keycloak.authorization.client.representation.ResourceRepresentation; @@ -33,9 +33,9 @@ import org.keycloak.util.JsonSerialization; public class ProtectedResource { private final Http http; - private final Supplier pat; + private final Callable pat; - public ProtectedResource(Http http, Supplier pat) { + public ProtectedResource(Http http, Callable pat) { this.http = http; this.pat = pat; } @@ -43,7 +43,7 @@ public class ProtectedResource { public RegistrationResponse create(ResourceRepresentation resource) { try { return this.http.post("/authz/protection/resource_set") - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .json(JsonSerialization.writeValueAsBytes(resource)) .response().json(RegistrationResponse.class).execute(); } catch (Exception cause) { @@ -54,7 +54,7 @@ public class ProtectedResource { public void update(ResourceRepresentation resource) { try { this.http.put("/authz/protection/resource_set/" + resource.getId()) - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .json(JsonSerialization.writeValueAsBytes(resource)).execute(); } catch (Exception cause) { throw handleAndWrapException("Could not update resource", cause); @@ -64,7 +64,7 @@ public class ProtectedResource { public RegistrationResponse findById(String id) { try { return this.http.get("/authz/protection/resource_set/" + id) - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .response().json(RegistrationResponse.class).execute(); } catch (Exception cause) { throw handleAndWrapException("Could not find resource", cause); @@ -74,7 +74,7 @@ public class ProtectedResource { public Set findByFilter(String filter) { try { return this.http.get("/authz/protection/resource_set") - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .param("filter", filter) .response().json(Set.class).execute(); } catch (Exception cause) { @@ -85,7 +85,7 @@ public class ProtectedResource { public Set findAll() { try { return this.http.get("/authz/protection/resource_set") - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .response().json(Set.class).execute(); } catch (Exception cause) { throw handleAndWrapException("Could not find resource", cause); @@ -95,7 +95,7 @@ public class ProtectedResource { public void delete(String id) { try { this.http.delete("/authz/protection/resource_set/" + id) - .authorizationBearer(this.pat.get()) + .authorizationBearer(this.pat.call()) .execute(); } catch (Exception cause) { throw handleAndWrapException("Could not delete resource", cause); diff --git a/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectionResource.java b/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectionResource.java index fd3bee9ea8..3d2eb2cfe7 100644 --- a/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectionResource.java +++ b/authz/client/src/main/java/org/keycloak/authorization/client/resource/ProtectionResource.java @@ -17,7 +17,7 @@ */ package org.keycloak.authorization.client.resource; -import java.util.function.Supplier; +import java.util.concurrent.Callable; import org.keycloak.authorization.client.representation.TokenIntrospectionResponse; import org.keycloak.authorization.client.util.Http; @@ -27,10 +27,10 @@ import org.keycloak.authorization.client.util.Http; */ public class ProtectionResource { - private final Supplier pat; + private final Callable pat; private final Http http; - public ProtectionResource(Http http, Supplier pat) { + public ProtectionResource(Http http, Callable pat) { if (pat == null) { throw new RuntimeException("No access token was provided when creating client for Protection API."); } diff --git a/authz/policy/pom.xml b/authz/policy/pom.xml index 9285778401..19f6c91b7f 100644 --- a/authz/policy/pom.xml +++ b/authz/policy/pom.xml @@ -17,6 +17,11 @@ KeyCloak AuthZ: Provider Parent KeyCloak AuthZ: Provider Parent + + 1.8 + 1.8 + + common drools diff --git a/authz/pom.xml b/authz/pom.xml index c466d6f9cb..448b04ac6a 100644 --- a/authz/pom.xml +++ b/authz/pom.xml @@ -21,10 +21,4 @@ policy client - - - 1.8 - 1.8 - - \ No newline at end of file diff --git a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/admin/AdminAlbumService.java b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/admin/AdminAlbumService.java index a9a53f465a..684f2161d7 100644 --- a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/admin/AdminAlbumService.java +++ b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/admin/AdminAlbumService.java @@ -46,7 +46,14 @@ public class AdminAlbumService { List result = this.entityManager.createQuery("from Album").getResultList(); for (Album album : result) { - albums.computeIfAbsent(album.getUserId(), key -> new ArrayList<>()).add(album); + List userAlbums = albums.get(album.getUserId()); + + if (userAlbums == null) { + userAlbums = new ArrayList<>(); + albums.put(album.getUserId(), userAlbums); + } + + userAlbums.add(album); } return Response.ok(albums).build(); diff --git a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/album/AlbumService.java b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/album/AlbumService.java index e84e24d7b2..056ff05f79 100644 --- a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/album/AlbumService.java +++ b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/album/AlbumService.java @@ -28,6 +28,7 @@ import java.security.Principal; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; @Path("/album") @Transaction @@ -47,6 +48,7 @@ public class AlbumService { public Response create(Album newAlbum) { Principal userPrincipal = request.getUserPrincipal(); + newAlbum.setId(UUID.randomUUID().toString()); newAlbum.setUserId(userPrincipal.getName()); Query queryDuplicatedAlbum = this.entityManager.createQuery("from Album where name = :name and userId = :userId"); @@ -68,7 +70,7 @@ public class AlbumService { @Path("{id}") @DELETE public Response delete(@PathParam("id") String id) { - Album album = this.entityManager.find(Album.class, Long.valueOf(id)); + Album album = this.entityManager.find(Album.class, id); try { deleteProtectedResource(album); @@ -90,7 +92,7 @@ public class AlbumService { @Path("{id}") @Produces("application/json") public Response findById(@PathParam("id") String id) { - List result = this.entityManager.createQuery("from Album where id = :id").setParameter("id", Long.valueOf(id)).getResultList(); + List result = this.entityManager.createQuery("from Album where id = :id").setParameter("id", id).getResultList(); if (result.isEmpty()) { return Response.status(Status.NOT_FOUND).build(); diff --git a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/entity/Album.java b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/entity/Album.java index 978bdeabb5..990595e58a 100644 --- a/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/entity/Album.java +++ b/examples/authz/photoz/photoz-restful-api/src/main/java/org/keycloak/example/photoz/entity/Album.java @@ -20,7 +20,6 @@ package org.keycloak.example.photoz.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import java.util.ArrayList; @@ -33,8 +32,7 @@ import java.util.List; public class Album { @Id - @GeneratedValue - private Long id; + private String id; @Column(nullable = false) private String name; @@ -45,11 +43,11 @@ public class Album { @Column(nullable = false) private String userId; - public Long getId() { + public String getId() { return this.id; } - public void setId(final Long id) { + public void setId(final String id) { this.id = id; } diff --git a/examples/authz/pom.xml b/examples/authz/pom.xml index 5e41c0c73b..a32c665187 100755 --- a/examples/authz/pom.xml +++ b/examples/authz/pom.xml @@ -17,8 +17,8 @@ - 1.8 - 1.8 + 1.7 + 1.7