From 2fc47d8d213436b186fe252a6f18baeba0a69089 Mon Sep 17 00:00:00 2001 From: Alfredo Oliviero Date: Mon, 26 Feb 2024 10:11:45 +0100 Subject: [PATCH] code format --- .vscode/settings.json | 12 +- deploy_tomcat_manager.sh | 2 +- docker/logback.xml | 15 +- docker_start.sh | 2 +- docs/index.md | 30 +-- enunciate.xml | 5 +- gcube/extra-resources/WEB-INF/web.xml | 15 +- .../IdentityManagerApplicationManager.java | 59 ++--- .../java/org/gcube/idm/rest/UsersRest.java | 245 ++++++++++-------- .../java/org/gcube/rest/ResponseBean.java | 2 +- .../java/org/gcube/rest/annotation/PATCH.java | 2 +- .../java/org/gcube/rest/annotation/PURGE.java | 2 +- .../org/gcube/rest/utils/HTTPUtility.java | 16 +- 13 files changed, 218 insertions(+), 189 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 351f903..612f716 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,5 +8,15 @@ "debug": false } ], - "java.dependency.packagePresentation": "hierarchical" + "java.dependency.packagePresentation": "hierarchical", + "Workspace_Formatter.excludePattern": [ + "**/build", + "**/.*", + "**/.vscode", + "**/docs/*", + "**/documentazione/*", + "**/target/*", + "**/tomcat/*", + "**/webapps/*" + ] } \ No newline at end of file diff --git a/deploy_tomcat_manager.sh b/deploy_tomcat_manager.sh index a21630a..3a622db 100755 --- a/deploy_tomcat_manager.sh +++ b/deploy_tomcat_manager.sh @@ -1,2 +1,2 @@ echo "deploying tomcat with tomcat/manager applications" -cp -a tomcat/manager/* tomcat/webappsgit \ No newline at end of file +cp -a tomcat/manager/* tomcat/webappsgit diff --git a/docker/logback.xml b/docker/logback.xml index 228ebc6..4799d20 100644 --- a/docker/logback.xml +++ b/docker/logback.xml @@ -1,23 +1,22 @@ - Ï - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - + Ï %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + - + - - + + - - + + \ No newline at end of file diff --git a/docker_start.sh b/docker_start.sh index f7de91a..b47c73e 100755 --- a/docker_start.sh +++ b/docker_start.sh @@ -6,4 +6,4 @@ rm -fr tomcat/webapps/* mvn clean package docker compose up -d --build --force-recreate -# curl http://localhost:8080/identity-manager/gcube/resource/healthx \ No newline at end of file +# curl http://localhost:8080/identity-manager/gcube/resource/healthx diff --git a/docs/index.md b/docs/index.md index 0b79774..4bdd78b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -90,21 +90,21 @@ on instances. :widths: grid -| Operation | HTTP Method | URL | Success HTTP Status | Safe | Idempotent | -|-----------|-------------|-----|---------------------|------|------------| -| **Supported
HTTP Methods** | OPTIONS | /{COLLECTION} | 204 No Content | Y | Y | -| **List** | GET | /{COLLECTION} | 200 OK | Y | Y | -| **Count** | GET | /{COLLECTION}?count=true | 200 OK | Y | Y | -| **Exists** | HEAD | /{COLLECTION} | 204 No Content | Y | Y | -| **Create** | POST | /{COLLECTION} | 201 Created | N | N | -| **Supported
HTTP Methods** | OPTIONS | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | Y | Y | -| **Exist** | HEAD | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | Y | Y | -| **Read** | GET | /{COLLECTION}/{INSTANCE_ID} | 200 OK | Y | Y | -| **Update** | PUT | /{COLLECTION}/{INSTANCE_ID} | 200 OK | N | Y | -| **Patch** | PATCH | /{COLLECTION}/{INSTANCE_ID} | 200 OK | N | Y | -| **Delete** | DELETE | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | N | N | -| **Purge** | PURGE | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | N | N | -| **Purge** | DELETE | /{COLLECTION}/{INSTANCE_ID}?purge=true | 204 No Content | N | N | +| Operation | HTTP Method | URL | Success HTTP Status | Safe | Idempotent | +| ------------------------------ | ----------- | -------------------------------------- | ------------------- | ---- | ---------- | +| **Supported
HTTP Methods** | OPTIONS | /{COLLECTION} | 204 No Content | Y | Y | +| **List** | GET | /{COLLECTION} | 200 OK | Y | Y | +| **Count** | GET | /{COLLECTION}?count=true | 200 OK | Y | Y | +| **Exists** | HEAD | /{COLLECTION} | 204 No Content | Y | Y | +| **Create** | POST | /{COLLECTION} | 201 Created | N | N | +| **Supported
HTTP Methods** | OPTIONS | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | Y | Y | +| **Exist** | HEAD | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | Y | Y | +| **Read** | GET | /{COLLECTION}/{INSTANCE_ID} | 200 OK | Y | Y | +| **Update** | PUT | /{COLLECTION}/{INSTANCE_ID} | 200 OK | N | Y | +| **Patch** | PATCH | /{COLLECTION}/{INSTANCE_ID} | 200 OK | N | Y | +| **Delete** | DELETE | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | N | N | +| **Purge** | PURGE | /{COLLECTION}/{INSTANCE_ID} | 204 No Content | N | N | +| **Purge** | DELETE | /{COLLECTION}/{INSTANCE_ID}?purge=true | 204 No Content | N | N | ### About URL diff --git a/enunciate.xml b/enunciate.xml index eb0f240..aa474a7 100644 --- a/enunciate.xml +++ b/enunciate.xml @@ -2,7 +2,7 @@ - + @@ -15,7 +15,8 @@ - + diff --git a/gcube/extra-resources/WEB-INF/web.xml b/gcube/extra-resources/WEB-INF/web.xml index 8b98727..7c5d746 100644 --- a/gcube/extra-resources/WEB-INF/web.xml +++ b/gcube/extra-resources/WEB-INF/web.xml @@ -1,12 +1,11 @@ - Identity Manager Service - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" + version="4.0" + metadata-complete="true"> + Identity Manager Service + A gcube service - smartgears 4 @@ -16,4 +15,4 @@ org.gcube.idm.IdentityManagerResourceInitializer /idm/* - + \ No newline at end of file diff --git a/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java b/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java index 09d4708..8a85a71 100644 --- a/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java +++ b/src/main/java/org/gcube/idm/IdentityManagerApplicationManager.java @@ -6,7 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * @author Alfredo Oliviero (ISTI - CNR) + * @author Alfredo gOliviero (ISTI - CNR) */ public class IdentityManagerApplicationManager implements ApplicationManager { @@ -14,55 +14,52 @@ public class IdentityManagerApplicationManager implements ApplicationManager { * Logger */ private static Logger logger = LoggerFactory.getLogger(IdentityManagerApplicationManager.class); - + public static boolean initialised; - - /** + + /** * {@inheritDoc} */ @Override public synchronized void onInit() { - + String context = SecretManagerProvider.get().getContext(); - + logger.trace( "\n-------------------------------------------------------\n" - + "Identity Manager Service is Starting on context {}\n" - + "-------------------------------------------------------", - context); - -// ApplicationContext applicationContext = ContextProvider.get(); -// String helloWorldEServiceID = applicationContext.id(); - - logger.trace( - "\n-------------------------------------------------------\n" - + "Identity Manager Service Started Successfully on context {}\n" - + "-------------------------------------------------------", + + "Identity Manager Service is Starting on context {}\n" + + "-------------------------------------------------------", + context); + + // ApplicationContext applicationContext = ContextProvider.get(); + // String helloWorldEServiceID = applicationContext.id(); + + logger.trace( + "\n-------------------------------------------------------\n" + + "Identity Manager Service Started Successfully on context {}\n" + + "-------------------------------------------------------", context); - - } - - /** - * {@inheritDoc} + + /** + * {@inheritDoc} */ @Override - public synchronized void onShutdown(){ - + public synchronized void onShutdown() { + String context = SecretManagerProvider.get().getContext(); - + logger.trace( "\n-------------------------------------------------------\n" - + "Identity Manager Service is Stopping on context {}\n" - + "-------------------------------------------------------", + + "Identity Manager Service is Stopping on context {}\n" + + "-------------------------------------------------------", context); - - + logger.trace( "\n-------------------------------------------------------\n" - + "Identity Manager Service Stopped Successfully on context {}\n" - + "-------------------------------------------------------", + + "Identity Manager Service Stopped Successfully on context {}\n" + + "-------------------------------------------------------", context); } } diff --git a/src/main/java/org/gcube/idm/rest/UsersRest.java b/src/main/java/org/gcube/idm/rest/UsersRest.java index 8e77a48..346811f 100644 --- a/src/main/java/org/gcube/idm/rest/UsersRest.java +++ b/src/main/java/org/gcube/idm/rest/UsersRest.java @@ -12,116 +12,139 @@ import org.slf4j.LoggerFactory; public class UsersRest { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class); - /* - // @GET - // @Path("/get-usernames-by-role") - // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public Response getUsernamesByRole( - @QueryParam("role-name") String roleName) { - Status status = Status.OK; - ResponseBean responseBean = new ResponseBean(); - - List usernames = new ArrayList(); - try { - String ctx = SecretManagerProvider.get().getContext(); - KeycloakApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); - - List users = null; // searchByRole(keycloackApiClient, roleName); - if (users != null) { - for (UserRepresentation user : users) { - usernames.add(user.getUsername()); - } - } - responseBean.setResult(usernames); - responseBean.setSuccess(true); - } catch (Exception e) { - logger.error("Unable to retrieve user with the requested role", e); - responseBean.setMessage(e.getMessage()); - status = Status.INTERNAL_SERVER_ERROR; - } - - return Response.status(status).entity(responseBean).build(); - - } - - private static List searchByRole(KeycloackApiClient keycloackApiClient, String roleName) { - logger.info("Searching by role: {}", roleName); - - List clients = keycloackApiClient.kclient.realm(keycloackApiClient.realmName) - .clients().findByClientId(keycloackApiClient.clientIdContext); - - String id = ""; - for (ClientRepresentation client : clients) { - logger.info("found client =" + client.getClientId()); - logger.info("found client id=" + client.getId()); - id = client.getId(); - } - - List users = keycloackApiClient.kclient.realm(keycloackApiClient.realmName) - .clients() - .get(id).roles().get(roleName) - .getUserMembers(0, 100000); - return users; - } - - @GET - @Path("/{get-profile}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public String getCurrentProfile() { - // SMARTGEARS Specializza il tracciamento della chiamata su Accounting - InnerMethodName.instance.set("getCurrentProfile"); - Owner owner = SecretManagerProvider.get().getOwner(); - - ApplicationContext appContext = ContextProvider.get(); - SimpleCredentials credentials = ((DefaultAuthorizationProvider) appContext.container().authorizationProvider()) - .getCredentials(); - - String ctx = SecretManagerProvider.get().getContext(); - KeycloackApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); - return null; - } - - @GET - @Path("/{get-email}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public String getCurrentEmail() { - throw new NotImplementedYetException(); - } - - @GET - @Path("/{get-fullname}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public String getCurrentFullname() { - throw new NotImplementedYetException(); - } - - @GET - @Path("/{get-all-usernames}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public String getAllUsernames() { - throw new NotImplementedYetException(); - } - - @GET - @Path("/{get-all-fullnames-and-usernames}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public String getAllUsernamesFullnames() { - throw new NotImplementedYetException(); - } - - @GET - @Path("/{user-exists}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public boolean checkUserExists() { - throw new NotImplementedYetException(); - } - - @GET - @Path("/{get-oauth-profile}") - @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) - public boolean getCurrentOAuthProfile() { - throw new NotImplementedYetException(); - } - */ + /* + * // @GET + * // @Path("/get-usernames-by-role") + * // @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" + * }) + * public Response getUsernamesByRole( + * + * @QueryParam("role-name") String roleName) { + * Status status = Status.OK; + * ResponseBean responseBean = new ResponseBean(); + * + * List usernames = new ArrayList(); + * try { + * String ctx = SecretManagerProvider.get().getContext(); + * KeycloakApiClient keycloackApiClient = + * KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); + * + * List users = null; // searchByRole(keycloackApiClient, + * roleName); + * if (users != null) { + * for (UserRepresentation user : users) { + * usernames.add(user.getUsername()); + * } + * } + * responseBean.setResult(usernames); + * responseBean.setSuccess(true); + * } catch (Exception e) { + * logger.error("Unable to retrieve user with the requested role", e); + * responseBean.setMessage(e.getMessage()); + * status = Status.INTERNAL_SERVER_ERROR; + * } + * + * return Response.status(status).entity(responseBean).build(); + * + * } + * + * private static List searchByRole(KeycloackApiClient + * keycloackApiClient, String roleName) { + * logger.info("Searching by role: {}", roleName); + * + * List clients = + * keycloackApiClient.kclient.realm(keycloackApiClient.realmName) + * .clients().findByClientId(keycloackApiClient.clientIdContext); + * + * String id = ""; + * for (ClientRepresentation client : clients) { + * logger.info("found client =" + client.getClientId()); + * logger.info("found client id=" + client.getId()); + * id = client.getId(); + * } + * + * List users = + * keycloackApiClient.kclient.realm(keycloackApiClient.realmName) + * .clients() + * .get(id).roles().get(roleName) + * .getUserMembers(0, 100000); + * return users; + * } + * + * @GET + * + * @Path("/{get-profile}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public String getCurrentProfile() { + * // SMARTGEARS Specializza il tracciamento della chiamata su Accounting + * InnerMethodName.instance.set("getCurrentProfile"); + * Owner owner = SecretManagerProvider.get().getOwner(); + * + * ApplicationContext appContext = ContextProvider.get(); + * SimpleCredentials credentials = ((DefaultAuthorizationProvider) + * appContext.container().authorizationProvider()) + * .getCredentials(); + * + * String ctx = SecretManagerProvider.get().getContext(); + * KeycloackApiClient keycloackApiClient = + * KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx); + * return null; + * } + * + * @GET + * + * @Path("/{get-email}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public String getCurrentEmail() { + * throw new NotImplementedYetException(); + * } + * + * @GET + * + * @Path("/{get-fullname}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public String getCurrentFullname() { + * throw new NotImplementedYetException(); + * } + * + * @GET + * + * @Path("/{get-all-usernames}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public String getAllUsernames() { + * throw new NotImplementedYetException(); + * } + * + * @GET + * + * @Path("/{get-all-fullnames-and-usernames}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public String getAllUsernamesFullnames() { + * throw new NotImplementedYetException(); + * } + * + * @GET + * + * @Path("/{user-exists}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public boolean checkUserExists() { + * throw new NotImplementedYetException(); + * } + * + * @GET + * + * @Path("/{get-oauth-profile}") + * + * @Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" }) + * public boolean getCurrentOAuthProfile() { + * throw new NotImplementedYetException(); + * } + */ } diff --git a/src/main/java/org/gcube/rest/ResponseBean.java b/src/main/java/org/gcube/rest/ResponseBean.java index 84daeac..2e5fea8 100644 --- a/src/main/java/org/gcube/rest/ResponseBean.java +++ b/src/main/java/org/gcube/rest/ResponseBean.java @@ -1,4 +1,5 @@ package org.gcube.rest; + import java.io.Serializable; /** @@ -68,4 +69,3 @@ public class ResponseBean implements Serializable { + ", message=" + message + ", result=" + result + "]"; } } - diff --git a/src/main/java/org/gcube/rest/annotation/PATCH.java b/src/main/java/org/gcube/rest/annotation/PATCH.java index bd46340..72dd40e 100644 --- a/src/main/java/org/gcube/rest/annotation/PATCH.java +++ b/src/main/java/org/gcube/rest/annotation/PATCH.java @@ -10,7 +10,7 @@ import javax.ws.rs.HttpMethod; /** * @author Luca Frosini (ISTI - CNR) */ -@Target({ElementType.METHOD}) +@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @HttpMethod("PATCH") public @interface PATCH { diff --git a/src/main/java/org/gcube/rest/annotation/PURGE.java b/src/main/java/org/gcube/rest/annotation/PURGE.java index 004980d..928cfe3 100644 --- a/src/main/java/org/gcube/rest/annotation/PURGE.java +++ b/src/main/java/org/gcube/rest/annotation/PURGE.java @@ -10,7 +10,7 @@ import javax.ws.rs.HttpMethod; /** * @author Luca Frosini (ISTI - CNR) */ -@Target({ElementType.METHOD}) +@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @HttpMethod("PURGE") public @interface PURGE { diff --git a/src/main/java/org/gcube/rest/utils/HTTPUtility.java b/src/main/java/org/gcube/rest/utils/HTTPUtility.java index d39fa5a..1318545 100644 --- a/src/main/java/org/gcube/rest/utils/HTTPUtility.java +++ b/src/main/java/org/gcube/rest/utils/HTTPUtility.java @@ -16,24 +16,24 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) */ public class HTTPUtility { - + private static final Logger logger = LoggerFactory.getLogger(HTTPUtility.class); - + public static StringBuilder getStringBuilder(InputStream inputStream) throws IOException { StringBuilder result = new StringBuilder(); - try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while((line = reader.readLine()) != null) { + while ((line = reader.readLine()) != null) { result.append(line); } } - + return result; } - + public static String getResultAsString(HttpURLConnection httpURLConnection) throws IOException { int responseCode = httpURLConnection.getResponseCode(); - if(responseCode >= Status.BAD_REQUEST.getStatusCode()) { + if (responseCode >= Status.BAD_REQUEST.getStatusCode()) { Status status = Status.fromStatusCode(responseCode); InputStream inputStream = httpURLConnection.getErrorStream(); StringBuilder result = getStringBuilder(inputStream); @@ -45,5 +45,5 @@ public class HTTPUtility { logger.trace("Got Respose is {}", ret); return ret; } - + }