You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2022/05/18 14:15:58 UTC

[tomee] branch tomee-8.x updated: TOMEE-3967 adding simple example for CDI in a Servlet Filter

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

jgallimore pushed a commit to branch tomee-8.x
in repository https://gitbox.apache.org/repos/asf/tomee.git


The following commit(s) were added to refs/heads/tomee-8.x by this push:
     new 0a70adb812 TOMEE-3967 adding simple example for CDI in a Servlet Filter
0a70adb812 is described below

commit 0a70adb812caa432cd4cfca679da9a5428b29f02
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Wed May 18 15:15:37 2022 +0100

    TOMEE-3967 adding simple example for CDI in a Servlet Filter
---
 examples/filter-cdi/README.adoc                    | 91 ++++++++++++++++++++++
 examples/filter-cdi/pom.xml                        | 74 ++++++++++++++++++
 .../java/org/superbiz/filterexample/Counter.java   | 31 ++++++++
 .../superbiz/filterexample/ExampleEndpoint.java    | 39 ++++++++++
 .../org/superbiz/filterexample/ExampleFilter.java  | 37 +++++++++
 .../org/superbiz/filterexample/RequestRole.java    | 36 +++++++++
 .../filter-cdi/src/main/webapp/WEB-INF/web.xml     | 24 ++++++
 examples/pom.xml                                   |  1 +
 8 files changed, 333 insertions(+)

diff --git a/examples/filter-cdi/README.adoc b/examples/filter-cdi/README.adoc
new file mode 100644
index 0000000000..f909288942
--- /dev/null
+++ b/examples/filter-cdi/README.adoc
@@ -0,0 +1,91 @@
+= Filter CDI
+:index-group: Misc
+:jbake-type: page
+:jbake-status: published
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== Using CDI beans in a Servlet Filter
+
+This is a simple example to demonstrate using CDI beans in a Servlet Filter. This example comprises four Java Classes:
+
+* Counter - this is an Application Scoped CDI bean to provide a global counter
+* RequestRole - this is a Request Scoped CDI bean which holds a role name. This bean is injected into a filter to set the role name, and into a JAX-RS endpoint to read it
+* ExampleFilter - Example Servlet Filter to set the rolename on the Request Role
+* ExampleEndpoint - JAX-RS endpoint that reads the rolename on the Request Role
+
+
+[source,java,numbered]
+----
+@ApplicationScoped
+public class Counter {
+
+    private final AtomicInteger count = new AtomicInteger(0);
+
+    public int get() {
+        return count.incrementAndGet();
+    }
+
+}
+----
+
+[source,java,numbered]
+----
+@RequestScoped
+public class RequestRole {
+
+    private String role;
+
+    public RequestRole() {
+    }
+
+    public String getRole() {
+        return role;
+    }
+
+    public void setRole(final String role) {
+        this.role = role;
+    }
+}
+----
+
+[source,java,numbered]
+----
+@WebFilter(urlPatterns = "/*")
+public class ExampleFilter implements Filter {
+
+    @Inject
+    private RequestRole requestRole;
+
+    @Inject Counter counter;
+
+    @Override
+    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
+        requestRole.setRole("sample-role-" + counter.get());
+        chain.doFilter(request, response);
+    }
+}
+----
+
+[source,java,numbered]
+----
+@Path("example")
+public class ExampleEndpoint {
+
+	@Inject
+	private RequestRole requestRole;
+
+	@GET
+	@Produces(MediaType.TEXT_PLAIN)
+	public String get(@Context HttpServletRequest request) {
+		return requestRole.getRole();
+	}
+
+}
+----
+
+Steps to replicate:
+
+   1. Run mvn clean install tomee:run
+   2. Startup server and go to http://localhost:8080/filter-cdi/example
+   3. You should see the sample role set in the filter, with a unique number appended on the end.
\ No newline at end of file
diff --git a/examples/filter-cdi/pom.xml b/examples/filter-cdi/pom.xml
new file mode 100644
index 0000000000..fa064237ec
--- /dev/null
+++ b/examples/filter-cdi/pom.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<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>org.superbiz</groupId>
+  <artifactId>filter-cdi</artifactId>
+  <packaging>war</packaging>
+  <version>8.0.12-SNAPSHOT</version>
+  <name>TomEE :: Examples :: Filter with CDI</name>
+
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tomee</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>8.0-5</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <finalName>filter-cdi</finalName>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.tomee.maven</groupId>
+        <artifactId>tomee-maven-plugin</artifactId>
+        <version>${tomee.version}</version>
+      </plugin>
+    </plugins>
+  </build>
+  <properties>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
+    <tomee.version>8.0.12-SNAPSHOT</tomee.version>
+  </properties>
+
+  <!--
+  This section allows you to configure where to publish libraries for sharing.
+  It is not required and may be deleted.  For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/
+  -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+
+</project>
+
diff --git a/examples/filter-cdi/src/main/java/org/superbiz/filterexample/Counter.java b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/Counter.java
new file mode 100644
index 0000000000..031cedeae6
--- /dev/null
+++ b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/Counter.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.filterexample;
+
+import javax.enterprise.context.ApplicationScoped;
+import java.util.concurrent.atomic.AtomicInteger;
+
+@ApplicationScoped
+public class Counter {
+
+    private final AtomicInteger count = new AtomicInteger(0);
+
+    public int get() {
+        return count.incrementAndGet();
+    }
+
+}
diff --git a/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleEndpoint.java b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleEndpoint.java
new file mode 100644
index 0000000000..8825c1b474
--- /dev/null
+++ b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleEndpoint.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.filterexample;
+
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+
+@Path("example")
+public class ExampleEndpoint {
+
+	@Inject
+	private RequestRole requestRole;
+	
+	@GET
+	@Produces(MediaType.TEXT_PLAIN)
+	public String get(@Context HttpServletRequest request) {
+		return requestRole.getRole();
+	}
+	
+}
diff --git a/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleFilter.java b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleFilter.java
new file mode 100644
index 0000000000..f7f1a9f95f
--- /dev/null
+++ b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/ExampleFilter.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.filterexample;
+
+import javax.inject.Inject;
+import javax.servlet.*;
+import javax.servlet.annotation.WebFilter;
+import java.io.IOException;
+
+@WebFilter(urlPatterns = "/*")
+public class ExampleFilter implements Filter {
+
+    @Inject
+    private RequestRole requestRole;
+
+    @Inject Counter counter;
+
+    @Override
+    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
+        requestRole.setRole("sample-role-" + counter.get());
+        chain.doFilter(request, response);
+    }
+}
diff --git a/examples/filter-cdi/src/main/java/org/superbiz/filterexample/RequestRole.java b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/RequestRole.java
new file mode 100644
index 0000000000..07788b5e17
--- /dev/null
+++ b/examples/filter-cdi/src/main/java/org/superbiz/filterexample/RequestRole.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.filterexample;
+
+import javax.enterprise.context.RequestScoped;
+
+@RequestScoped
+public class RequestRole {
+
+    private String role;
+
+    public RequestRole() {
+    }
+
+    public String getRole() {
+        return role;
+    }
+
+    public void setRole(final String role) {
+        this.role = role;
+    }
+}
diff --git a/examples/filter-cdi/src/main/webapp/WEB-INF/web.xml b/examples/filter-cdi/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000000..fdfc0f62b0
--- /dev/null
+++ b/examples/filter-cdi/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         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">
+</web-app>
diff --git a/examples/pom.xml b/examples/pom.xml
index 854c899fbf..7c9fe651a4 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -83,6 +83,7 @@
     <module>ejb-remote-call</module>
     <module>ejb-remote-call-2</module>
     <module>ejb-webservice</module>
+    <module>filter-cdi</module>
     <module>groovy-cdi</module>
     <module>groovy-jpa</module>
     <module>groovy-spock</module>