Skip to content

Commit

Permalink
IDE-Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Jul 24, 2024
1 parent 2e01527 commit 90a319c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import java.util.Map;

/**
* Interface of bean which represents an encapsulates the current user specific
* Interface of bean, which represents an encapsulating the current user-specific
* information in session context. It should call data from authentication
* facade on creation and then hold and provide this information for any other
* beans.
*
* @author Stephan Babkin
*/
@SuppressWarnings("squid:S1214") // We allow constants in interfaces, if they belong together
// (coherence).
@SuppressWarnings("squid:S1214") // We allow constants in interfaces if they belong together
// (coherence).
public interface AuthenticatedUserInfo extends Serializable {

/**
Expand All @@ -44,7 +44,7 @@ public interface AuthenticatedUserInfo extends Serializable {
* <code>false</code> otherwise.
*
* @return <code>true</code> in case of user is authenticated already and
* <code>false</code> otherwise.
* <code>false</code> otherwise.
*/
boolean isAuthenticated();

Expand All @@ -54,10 +54,10 @@ public interface AuthenticatedUserInfo extends Serializable {
List<String> getRoles();

/**
* Checks if is user in role.
* Checks if is user in a role.
*
* @param roleName the role name
* @return true, if is user in role
* @return true, if is user in a role
*/
default boolean isUserInRole(String roleName) {
return getRoles().contains(roleName);
Expand All @@ -75,25 +75,25 @@ default boolean isUserInRole(String roleName) {

/**
* @return the (technical) identifier for the currently authenticated user to be
* used with {@link #getSystem()}.
* used with {@link #getSystem()}.
*/
String getIdentifier();

/**
* @return the (technical) qualified identifier for the currently authenticated
* user to be used without {@link #getSystem()}.
* user to be used without {@link #getSystem()}.
*/
String getQualifiedIdentifier();

/**
* @return the (technical) assigning authority for the {@link #getIdentifier()}
* of the currently authenticated user
* of the currently authenticated user
*/
String getSystem();

/**
* @return the context map containing additional runtime information belonging
* to the {@link AuthenticatedUserInfo}
* to the {@link AuthenticatedUserInfo}
*/
Map<Serializable, Serializable> getContextMap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package de.cuioss.portal.configuration.source;

import java.util.Optional;
import java.util.Set;

import org.eclipse.microprofile.config.spi.ConfigSource;

import de.cuioss.portal.common.priority.PortalPriorities;
import de.cuioss.portal.configuration.FileConfigurationSource;
import de.cuioss.tools.logging.CuiLogger;
import lombok.ToString;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.Optional;
import java.util.Set;

/**
* Portal default configuration source for {@code properties} files. Per
Expand Down Expand Up @@ -60,7 +59,7 @@ public String getName() {
* Do not overwrite this function. Use {@link #getPortalPriority()} instead.
* <p>
* As Portal priorities are within the range 0-150, the final value will fit
* nicely within the MP config source ordinals 100-250. Hence all portal config
* nicely within the MP config source ordinals 100-250. Hence, all portal config
* sources sit below the "EnvConfigSource".
*
* @return {@link ConfigSource#DEFAULT_ORDINAL} + {@link #getPortalPriority()}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package de.cuioss.portal.core.listener;

import static de.cuioss.tools.collect.CollectionLiterals.mutableList;

import java.util.Collections;
import java.util.List;

import de.cuioss.portal.configuration.initializer.ApplicationInitializer;
import de.cuioss.portal.configuration.initializer.PortalInitializer;
import de.cuioss.portal.core.servlet.CuiContextPath;
import de.cuioss.tools.logging.CuiLogger;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.Initialized;
Expand All @@ -32,11 +32,10 @@
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;

import de.cuioss.portal.configuration.initializer.ApplicationInitializer;
import de.cuioss.portal.configuration.initializer.PortalInitializer;
import de.cuioss.portal.core.servlet.CuiContextPath;
import de.cuioss.tools.logging.CuiLogger;
import jakarta.annotation.PreDestroy;
import java.util.Collections;
import java.util.List;

import static de.cuioss.tools.collect.CollectionLiterals.mutableList;

/**
* Central Listener for Application life-cycle events.
Expand Down Expand Up @@ -81,7 +80,7 @@ private void applicationInitializerListener(final ServletContext context) {
final List<ApplicationInitializer> initializers = mutableList(applicationInitializers);
Collections.sort(initializers);
LOGGER.debug("ServletLifecycleListener called for '{}', initializing with order: {}", contextPath,
initializers);
initializers);
for (final ApplicationInitializer applicationInitializer : initializers) {
LOGGER.debug("Initializing '{}' for '{}'", applicationInitializer, context);
applicationInitializer.initialize();
Expand All @@ -98,16 +97,16 @@ public void applicationDestroyListener() {
LOGGER.debug("Executing applicationDestroyListener");
LOGGER.info("Portal-008: Shutting down '{}'", contextPath);
final List<ApplicationInitializer> finalizer = mutableList(applicationInitializers);
Collections.sort(finalizer, Collections.reverseOrder());
finalizer.sort(Collections.reverseOrder());
LOGGER.debug("ServletLifecycleListener called for '{}', finalizing with order: {}", contextPath, finalizer);
for (final ApplicationInitializer applicationInitializer : finalizer) {
LOGGER.debug("Destroying '{}' for '{}'", applicationInitializer, contextPath);
try {
applicationInitializer.destroy();
} catch (RuntimeException e) {
LOGGER.warn(
"Runtime Exception occurred while trying to destroy '{}' for '{}'. message='{}', stracktrace will be available at DEBUG-level",
applicationInitializer, contextPath, e.getMessage());
"Runtime Exception occurred while trying to destroy '{}' for '{}'. message='{}', stacktrace will be available at DEBUG-level",
applicationInitializer, contextPath, e.getMessage());
LOGGER.debug("Detailed exception", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package de.cuioss.portal.core.servlet;

import java.io.IOException;
import java.io.Serial;
import java.util.Collection;
import java.util.Collections;

import de.cuioss.portal.authentication.AuthenticatedUserInfo;
import de.cuioss.portal.authentication.PortalUser;
import de.cuioss.tools.logging.CuiLogger;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.servlet.Servlet;
Expand All @@ -28,9 +26,11 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import de.cuioss.portal.authentication.AuthenticatedUserInfo;
import de.cuioss.portal.authentication.PortalUser;
import de.cuioss.tools.logging.CuiLogger;
import java.io.IOException;
import java.io.Serial;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;

/**
* Provides a minimal layer for modeling {@link Servlet}s that can be enabled by
Expand All @@ -49,11 +49,12 @@
* will be called.
*
* @author Oliver Wolff
*
*/
public abstract class AbstractPortalServlet extends HttpServlet {

/** Portal-523: Could not process Request, due to {}. */
/**
* Portal-523: Could not process Request, due to {}.
*/
public static final String PORTAL_523 = "Portal-523: Could not process Request, due to {}";

private static final String NOT_LOGGED_IN = "Portal-523: Could not process Request, because the user must be logged in for this request";
Expand Down Expand Up @@ -105,7 +106,7 @@ public boolean checkAccess(HttpServletResponse resp) {
return false;
}
var requiredRoles = getRequiredRoles();
if (!requiredRoles.isEmpty() && !user.getRoles().containsAll(requiredRoles)) {
if (!requiredRoles.isEmpty() && !new HashSet<>(user.getRoles()).containsAll(requiredRoles)) {
log.warn(USER, "User should provide the roles " + requiredRoles, user);
resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
return false;
Expand All @@ -115,7 +116,8 @@ public boolean checkAccess(HttpServletResponse resp) {
}

/**
* The actual payload method for the concrete servlet. If it is called all
* The actual payload method for the concrete servlet.
* If it is called, all
* checks are already done and the implementor can focus on the business logic.
* All {@link RuntimeException} are translated to an
* {@link HttpServletResponse#SC_INTERNAL_SERVER_ERROR}
Expand All @@ -128,39 +130,43 @@ public boolean checkAccess(HttpServletResponse resp) {

/**
* @return boolean indicating whether the Servlet-Request is enabled at all.
* Usually this is controlled by a configured-property. If this method
* returns false it will be translated to
* {@link HttpServletResponse#SC_SERVICE_UNAVAILABLE}. Defaults to
* {@code true}
* Usually, this is controlled by a configured property.
* If this method
* returns false it will be translated to
* {@link HttpServletResponse#SC_SERVICE_UNAVAILABLE}.
* Defaults to
* {@code true}
*/
public boolean isEnabled() {
return true;
}

/**
* @return boolean indicating whether the servlet needs a logged in user. If it
* is {@code true} the system will check whether the current user is
* logged in. If the user is not logged in it will return
* {@link HttpServletResponse#SC_FORBIDDEN}. Defaults to {@code false}
* @return boolean indicating whether the servlet needs a logged-in user.
* If it is {@code true} the system will check whether the current user is
* logged in.
* If the user is not logged in it will return
* {@link HttpServletResponse#SC_FORBIDDEN}.
* Defaults to {@code false}
*/
public boolean isLoggedInUserRequired() {
return false;
}

/**
* @return a {@link Collection} of roles that are <em>all</em> required in order
* to render the content. The default implementation will return an
* empty {@link Collection}. If this method provides at least one role
* that user does not provide the servlet will return
* {@link HttpServletResponse#SC_FORBIDDEN}
* @return a {@link Collection} of roles that are <em>all</em> required
* to render the content. The default implementation will return an
* empty {@link Collection}. If this method provides at least one role
* that user does not provide the servlet will return
* {@link HttpServletResponse#SC_FORBIDDEN}
*/
public Collection<String> getRequiredRoles() {
return Collections.emptyList();
}

/**
* @return the {@link AuthenticatedUserInfo} resolved against the injected
* {@link PortalUser}
* {@link PortalUser}
*/
protected AuthenticatedUserInfo getUserInfo() {
return userInfoProvider.get();
Expand Down

0 comments on commit 90a319c

Please sign in to comment.