Intellij is not calling Jenkins CORSFilter

已回答

I imported a working Eclipse Jenkins servlet project into Intellij without any errors.  This is in a RH 8.6 VM image using tomcat10 and uses java 17.  The project works with Eclipse jee-2022-12 and a react restful webpage http://localhost:3000 send on localhost:8080.   The CORSFilter implements ContainerResponseFilter is called on each GET/POST so the header origin is updated.  There are no origin errors in react and the webpage works as expected.  However; when I imported the project and run it in the IDE as "explode" the class is not called.  My AppServletContextListener class is called on servlet start up as expect.   My question is why is the IDE not calling the CORSFilter class?  I am using @Provider to register the class so it should be called.  This is the class which works as expected in Eclipse IDE.

/*
* Update the header so CORS is ok.
*/

import java.io.IOException;

import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;

@Provider
public class CORSFilter implements ContainerResponseFilter {

/**
* Update the outgoing message header so CORS permissions pass so the calling application
* can read back the response and not have a network origin error.
*
* @param requestContext ContainerRequestContext
* @param responseContext responseContext
*/
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {

MultivaluedMap<String, Object> headers = responseContext.getHeaders();

// use the header origin to set Allow-Origin if possible like http://localhost:3000
String origin = requestContext.getHeaderString("origin");
if ((origin != null) && (!origin.isEmpty())) {
headers.add("Access-Control-Allow-Origin", origin);
} else {
// *(allow from all servers) OR https://crunchify.com/
headers.add("Access-Control-Allow-Origin", "*");
}

headers.add("Access-Control-Allow-Credentials", "true");

// As part of the response to a request, which HTTP headers can be used during the actual request.
headers.add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");

headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");

}
}

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.frequentisDefense.spawar</groupId>
<artifactId>rtc-servlet</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>rtc-servlet</name>

<properties>
<jersey.version>3.1.0</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<eclipse.packager.version>0.19.0</eclipse.packager.version>
<bouncycastle.version>1.71</bouncycastle.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>

<!-- Get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>

<!-- javax.servlet.http.HttpServlet allows web page to use rest without doing socket connection -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- "@XmlRootElement cannot be resolved to a type" -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.1</version>
<scope>runtime</scope>
</dependency>

<!-- Get XML support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.packager</groupId>
<artifactId>packager-rpm</artifactId>
<version>${eclipse.packager.version}</version>
</dependency>

<!-- read/write yaml configuration file -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>

<!-- log4j 2 logger -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.19.0</version>
</dependency>

<!-- fixes failed to load class "org.slf4j.impl.StaticLoggerBinder" warning -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>

<build>
<finalName>rtc-servlet</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>

<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>


</plugins>
</build>

</project>
0

Hello,

Have you tried to debug application and check call stack?

0

After reading some more, I uninstalled all versions of java and reinstalled JDK 17 and modified the pom.xml file.   I then got a java mismatch when doing the package command and realized maven was defaulting to 1.8 level.  After resetting it to 17 the WAR was created and the CORSFilter class was finally called.

The new pom.xml is now this and I am manually copying the WAR file to tomcat's webapp directory since the tomcat7 plug in does seem to work.  The only other problem was in Eclipse the VM variables are append to the end of the server's variable list so I couldn't override the default setenv.sh tomcat variable.   With Intellij I had to create a debug system variable that the code now checks first before reading in the default variable is not set.  That way I am using the correct version of the configuration file for testing vs. operations.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.frequentisDefense.spawar</groupId>
<artifactId>rtc-servlet</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>

<name>rtc-servlet</name>
<description>Backend for Remote Tower Configuration (RTC) web page</description>

<properties>
<jersey.version>3.1.0</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<eclipse.packager.version>0.19.0</eclipse.packager.version>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>

<!-- Get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>

<!-- jakarta.servlet.http.HttpServlet allows web page to use rest without doing socket connection -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- "@XmlRootElement cannot be resolved to a type" -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>4.0.1</version>
<scope>runtime</scope>
</dependency>

<!-- Get XML support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.packager</groupId>
<artifactId>packager-rpm</artifactId>
<version>${eclipse.packager.version}</version>
</dependency>

<!-- read/write yaml configuration file -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
</dependency>

<!-- log4j 2 logger -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.19.0</version>
</dependency>

<!-- fixes failed to load class "org.slf4j.impl.StaticLoggerBinder" warning -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>

<build>
<finalName>rtc-servlet</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>

0

请先登录再写评论。