Skip to content

Commit

Permalink
feat(cdi) Upgrading Jersey to 2.28 and including CDI through Weld Ref…
Browse files Browse the repository at this point in the history
…s:29552 (#29822)

Proposed Changes
In this PR we are upgrading to jersey 2.28
We are changing DotRestApplication to use package discovery to load
Resources and Providers
We're making some adjustments on some Mappers to preserve the present
behavior
We're introducing CDI through Weld container
This PR fixes: #29552

---------

Co-authored-by: spbolton <steve.bolton@dotcms.com>
  • Loading branch information
fabrizzio-dotCMS and spbolton authored Sep 3, 2024
1 parent 07a110e commit b3427e9
Show file tree
Hide file tree
Showing 43 changed files with 533 additions and 1,957 deletions.
54 changes: 47 additions & 7 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<shedlock.version>4.33.0</shedlock.version>
<glowroot.version>0.14.1</glowroot.version>
<jackson.version>2.17.2</jackson.version>
<jersey.version>2.22.1</jersey.version>
<jersey.version>2.28</jersey.version>
<graalvm.version>22.3.3</graalvm.version>
</properties>
<dependencyManagement>
Expand Down Expand Up @@ -84,11 +84,6 @@
*****************************
-->

<dependency>
<groupId>com.dotcms</groupId>
<artifactId>ant-tooling</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.dotcms.lib</groupId>
<artifactId>dot.aopalliance-repackaged</artifactId>
Expand Down Expand Up @@ -1137,7 +1132,7 @@
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<version>2.1.1</version>
</dependency>

<!-- Rest API Documentation -->
Expand All @@ -1152,6 +1147,51 @@
<version>${swagger.version}</version>
</dependency>

<!-- CDI Support -->

<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.1.9.Final</version>
</dependency>

<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>bean-validator</artifactId>
<version>2.5.0-b06</version>
</dependency>

<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit5</artifactId>
<version>2.0.2.Final</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-shaded</artifactId>
<version>3.1.9.Final</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<version>3.0.5</version>
</dependency>

<!--
*****************************
Expand Down
1,141 changes: 0 additions & 1,141 deletions build.xml

This file was deleted.

42 changes: 35 additions & 7 deletions dotCMS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@
*****************************
-->

<dependency>
<groupId>com.dotcms</groupId>
<artifactId>ant-tooling</artifactId>
</dependency>
<dependency>
<groupId>com.dotcms.lib</groupId>
<artifactId>dot.aopalliance-repackaged</artifactId>
Expand Down Expand Up @@ -1199,12 +1195,11 @@
<artifactId>jersey-common</artifactId>
</dependency>

<!--
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-cdi2-se</artifactId>
<artifactId>jersey-hk2</artifactId>
</dependency>
-->

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
Expand Down Expand Up @@ -1412,6 +1407,39 @@
</exclusions>
</dependency>

<!-- Weld CDI -->

<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.1.9.Final</version>
</dependency>

<dependency>
<groupId>org.glassfish.hk2.external</groupId>
<artifactId>bean-validator</artifactId>
<version>2.5.0-b06</version>
</dependency>

<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-shaded</artifactId>
<scope>test</scope>
</dependency>

<!-- Weld CDI -->

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<version>3.0.5</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions dotCMS/src/main/java/com/dotcms/cdi/CDIUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dotcms.cdi;

import com.dotmarketing.util.Logger;
import java.util.Optional;
import javax.enterprise.inject.spi.CDI;

/**
* Utility class to get beans from CDI container
*/
public class CDIUtils {

/**
* Private constructor to avoid instantiation
*/
private CDIUtils() {
}

/**
* Get a bean from CDI container
* @param clazz the class of the bean
* @return an Optional with the bean if found, empty otherwise
*/
public static <T> Optional<T> getBean(Class<T> clazz) {
try {
return Optional.of(CDI.current().select(clazz).get());
} catch (Exception e) {
Logger.error(CDIUtils.class,
String.format("Unable to find bean of class [%s] [%s]", clazz, e.getMessage())
);
}
return Optional.empty();
}


}
2 changes: 2 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/TestResource.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotcms.rest;

import com.dotcms.repackage.org.apache.commons.httpclient.HttpStatus;
import com.dotcms.rest.config.Disabled;
import com.dotcms.util.xstream.XStreamHandler;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
Expand All @@ -27,6 +28,7 @@
* @author Jonathan Gamba
* Date: 8/22/13
*/
@Disabled
@Path ("/testResource")
public class TestResource {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import javax.ws.rs.ext.Provider;

/**
* This decorator reads the annotations on the resources and includes header based on it based on them.
Expand All @@ -31,6 +32,7 @@
*/
@Singleton
@Priority(Priorities.HEADER_DECORATOR)
@Provider
public class HeaderFilter implements ContainerResponseFilter {

private final PermissionsUtil permissionsUtil = PermissionsUtil.getInstance();
Expand Down
2 changes: 2 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/api/CorsFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import com.dotmarketing.util.Logger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import javax.ws.rs.ext.Provider;


/**
* @author Geoff M. Granum
*/
@Provider
public class CorsFilter implements ContainerResponseFilter {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

import com.dotmarketing.util.Logger;
import java.util.concurrent.atomic.AtomicReference;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spi.AbstractContainerLifecycleListener;
import org.glassfish.jersey.server.spi.Container;

/**
* A new Reloader will get created on each reload there can only be one container at a time
*/
class ContainerReloader extends AbstractContainerLifecycleListener {

private static final ContainerReloader INSTANCE = new ContainerReloader();
@Provider
@ApplicationScoped
public class ContainerReloader extends AbstractContainerLifecycleListener {

private static final AtomicReference<Container> containerRef = new AtomicReference<>();

public static ContainerReloader getInstance() {
return INSTANCE;
}

@Override
public void onStartup(Container container) {
Logger.debug(ContainerReloader.class, "Jersey Container started");
Expand Down
19 changes: 19 additions & 0 deletions dotCMS/src/main/java/com/dotcms/rest/config/Disabled.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dotcms.rest.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

/**
* Annotation to mark a resource as disabled.
* This is useful now that resources are activated by classpath scanning.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Disabled {
Response.Status status() default Status.NOT_FOUND;
String message() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.dotcms.rest.config;

import com.dotmarketing.util.Logger;
import com.jayway.jsonpath.internal.Utils;
import java.io.IOException;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

/**
* Filter to check if a resource is disabled.
*/
@Provider
@Priority(1)
public class DisabledResourceFilter implements ContainerRequestFilter {

@Context
private ResourceInfo resourceInfo;

/**
* Check if the resource is disabled.
* @param requestContext request context.
* @throws IOException if an I/O exception occurs.
*/
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
Class<?> resourceClass = resourceInfo.getResourceClass();
if (resourceClass.isAnnotationPresent(Disabled.class)) {
final Disabled annotation = resourceClass.getAnnotation(Disabled.class);
final Status status = annotation.status();
final String message = Utils.isEmpty(annotation.message()) ? status.getReasonPhrase() : annotation.message();
requestContext.abortWith(Response.status(annotation.status()).entity(message).build());
Logger.debug(this.getClass(), "Resource " + resourceClass.getName() + " is marked disabled.");
}
}
}
Loading

0 comments on commit b3427e9

Please sign in to comment.