You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/09/16 08:48:06 UTC

[sling-whiteboard] branch master updated: Add /userinfo

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 2b30ec0  Add /userinfo
2b30ec0 is described below

commit 2b30ec0b6b4a728a67ff00f06a946fe77400493e
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Sep 16 10:47:57 2021 +0200

    Add /userinfo
---
 aries-jax-rs-whiteboard/pom.xml                    |  5 ++++
 .../java/org/apache/sling/jaxrs/TestService.java   | 28 ++++++++++++++++++----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/aries-jax-rs-whiteboard/pom.xml b/aries-jax-rs-whiteboard/pom.xml
index ab616ab..cf76bdb 100644
--- a/aries-jax-rs-whiteboard/pom.xml
+++ b/aries-jax-rs-whiteboard/pom.xml
@@ -155,6 +155,11 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>javax.jcr</groupId>
+      <artifactId>jcr</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <scope>provided</scope>
diff --git a/aries-jax-rs-whiteboard/src/main/java/org/apache/sling/jaxrs/TestService.java b/aries-jax-rs-whiteboard/src/main/java/org/apache/sling/jaxrs/TestService.java
index d865f63..9f5ed6f 100644
--- a/aries-jax-rs-whiteboard/src/main/java/org/apache/sling/jaxrs/TestService.java
+++ b/aries-jax-rs-whiteboard/src/main/java/org/apache/sling/jaxrs/TestService.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.jaxrs;
 
+import javax.jcr.Session;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -41,14 +42,19 @@ public class TestService {
 
 	static int counter;
 	 
+	static ResourceResolver getResourceResolver(HttpServletRequest request) {
+		final Object obj = request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
+		if(obj instanceof ResourceResolver) {
+			return (ResourceResolver)obj;
+		}
+		throw new IllegalStateException("Request does not provide a ResourceResolver");
+
+	}
+
 	@GET
 	@Path("/sling/resource")
 	public String getResource(@Context HttpServletRequest request, @QueryParam("path") String path) throws Exception {
-		final Object obj = request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
-		try (ResourceResolver rr = obj instanceof ResourceResolver ? (ResourceResolver)obj : null) {
-			if(rr == null) {
-				throw new Exception("ResourceResolver not available");
-			}
+		try (ResourceResolver rr = getResourceResolver(request)) {
 			final Resource r = rr.getResource(path);
 			if(r == null) {
 				throw new Exception(String.format("Resource %s not found", path));
@@ -58,6 +64,18 @@ public class TestService {
 	}
 
 	@GET
+	@Path("/userinfo")
+	public String getUserInfo(@Context HttpServletRequest request) throws Exception {
+		try (ResourceResolver rr = getResourceResolver(request)) {
+			final Session s = rr.adaptTo(Session.class);
+			if(s == null) {
+				throw new Exception("ResourceResolver does not adapt to Session");
+			}
+			return String.format("userID='%s'%n", s.getUserID());
+		}
+	}
+
+	@GET
 	@Path("/{one}")
 	public String getOne(@PathParam("one") String one) {
 		return String.format(