diff --git a/open-liberty/pom.xml b/open-liberty/pom.xml index f07d2aabb3..b2ed213af6 100644 --- a/open-liberty/pom.xml +++ b/open-liberty/pom.xml @@ -1,109 +1,120 @@ - 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/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.baeldung - open-liberty - 1.0-SNAPSHOT - war + com.baeldung + open-liberty + 1.0-SNAPSHOT + war - - 1.8 - 1.8 - UTF-8 - UTF-8 - false - - 3.1 - 3.2.3 - 2.22.2 - 2.22.2 - - 9080 - 9443 - + + 1.8 + 1.8 + UTF-8 + UTF-8 + false + + 3.1 + 3.2.3 + 2.22.2 + 2.22.2 + + 9080 + 9443 + openliberty + - - - - jakarta.platform - jakarta.jakartaee-web-api - 8.0.0 - provided + + + org.apache.derby + derby + 10.14.2.0 + + + com.h2database + h2 + 1.4.186 - - org.eclipse.microprofile - microprofile - 3.0 - pom - provided - - - - org.junit.jupiter - junit-jupiter-engine - 5.5.2 - test - - - org.apache.cxf - cxf-rt-rs-client - 3.3.4 - test - - - org.apache.cxf - cxf-rt-rs-extension-providers - 3.3.4 - test - - - org.glassfish - javax.json - 1.1.4 - test - - - - javax.xml.bind - jaxb-api - 2.3.1 - test - - + + + jakarta.platform + jakarta.jakartaee-web-api + 8.0.0 + provided + + + org.eclipse.microprofile + microprofile + 3.0 + pom + provided + + + + org.junit.jupiter + junit-jupiter-engine + 5.5.2 + test + + + org.apache.cxf + cxf-rt-rs-client + 3.3.4 + test + + + org.apache.cxf + cxf-rt-rs-extension-providers + 3.3.4 + test + + + org.glassfish + javax.json + 1.1.4 + test + + + + javax.xml.bind + jaxb-api + 2.3.1 + test + + - - ${project.artifactId} - - - - io.openliberty.tools - liberty-maven-plugin - ${version.liberty-maven-plugin} - - - org.apache.maven.plugins - maven-war-plugin - ${version.maven-war-plugin} - - - org.apache.maven.plugins - maven-surefire-plugin - ${version.maven-surefire-plugin} - - - - org.apache.maven.plugins - maven-failsafe-plugin - ${version.maven-failsafe-plugin} - - - ${liberty.var.default.http.port} - - - - - + + ${project.artifactId} + + + + io.openliberty.tools + liberty-maven-plugin + ${version.liberty-maven-plugin} + + + org.apache.maven.plugins + maven-war-plugin + ${version.maven-war-plugin} + + + org.apache.maven.plugins + maven-surefire-plugin + ${version.maven-surefire-plugin} + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${version.maven-failsafe-plugin} + + + ${liberty.var.default.http.port} + + + + + \ No newline at end of file diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/SystemApplication.java b/open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java similarity index 58% rename from open-liberty/src/main/java/com/baeldung/openliberty/SystemApplication.java rename to open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java index a755d0e5fa..92855b5876 100644 --- a/open-liberty/src/main/java/com/baeldung/openliberty/SystemApplication.java +++ b/open-liberty/src/main/java/com/baeldung/openliberty/ApiApplication.java @@ -3,7 +3,7 @@ package com.baeldung.openliberty; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; -@ApplicationPath("/system") -public class SystemApplication extends Application { +@ApplicationPath("/api") +public class ApiApplication extends Application { } diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/SystemLivenessCheck.java b/open-liberty/src/main/java/com/baeldung/openliberty/SystemLivenessCheck.java deleted file mode 100644 index cf941cf0a1..0000000000 --- a/open-liberty/src/main/java/com/baeldung/openliberty/SystemLivenessCheck.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.openliberty; - -import javax.enterprise.context.ApplicationScoped; - -import java.lang.management.MemoryMXBean; -import java.lang.management.ManagementFactory; - -import org.eclipse.microprofile.health.Liveness; -import org.eclipse.microprofile.health.HealthCheck; -import org.eclipse.microprofile.health.HealthCheckResponse; - -@Liveness -@ApplicationScoped -public class SystemLivenessCheck implements HealthCheck { - - @Override - public HealthCheckResponse call() { - MemoryMXBean memBean = ManagementFactory.getMemoryMXBean(); - long memUsed = memBean.getHeapMemoryUsage().getUsed(); - long memMax = memBean.getHeapMemoryUsage().getMax(); - - return HealthCheckResponse.named( - SystemResource.class.getSimpleName() + " liveness check") - .withData("memory used", memUsed) - .withData("memory max", memMax) - .state(memUsed < memMax * 0.9).build(); - } - -} \ No newline at end of file diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/SystemReadinessCheck.java b/open-liberty/src/main/java/com/baeldung/openliberty/SystemReadinessCheck.java deleted file mode 100644 index 567d02204b..0000000000 --- a/open-liberty/src/main/java/com/baeldung/openliberty/SystemReadinessCheck.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.openliberty; - -import javax.enterprise.context.ApplicationScoped; - -import javax.inject.Inject; -import javax.inject.Provider; - -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.eclipse.microprofile.health.Readiness; -import org.eclipse.microprofile.health.HealthCheck; -import org.eclipse.microprofile.health.HealthCheckResponse; -import org.eclipse.microprofile.health.HealthCheckResponseBuilder; - -@Readiness -@ApplicationScoped -public class SystemReadinessCheck implements HealthCheck { - - @Inject - @ConfigProperty(name = "io_openliberty_guides_system_inMaintenance") - Provider inMaintenance; - - @Override - public HealthCheckResponse call() { - HealthCheckResponseBuilder builder = HealthCheckResponse.named( - SystemResource.class.getSimpleName() + " readiness check"); - if (inMaintenance != null && inMaintenance.get().equalsIgnoreCase("true")) { - return builder.withData("services", "not available").down().build(); - } - return builder.withData("services", "available").up().build(); - } - -} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/SystemResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/SystemResource.java deleted file mode 100644 index 10253161af..0000000000 --- a/open-liberty/src/main/java/com/baeldung/openliberty/SystemResource.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.openliberty; - -import javax.ws.rs.core.Response; - -import javax.enterprise.context.RequestScoped; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -import org.eclipse.microprofile.metrics.annotation.Counted; -import org.eclipse.microprofile.metrics.annotation.Timed; - -@RequestScoped -@Path("/properties") -public class SystemResource { - - @GET - @Produces(MediaType.APPLICATION_JSON) - @Timed(name = "getPropertiesTime", description = "Time needed to get the JVM system properties") - @Counted(absolute = true, description = "Number of times the JVM system properties are requested") - public Response getProperties() { - return Response.ok(System.getProperties()).build(); - } -} - diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/hello/HelloServlet.java b/open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java similarity index 69% rename from open-liberty/src/main/java/com/baeldung/openliberty/hello/HelloServlet.java rename to open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java index 8232306f37..526e13c0e9 100644 --- a/open-liberty/src/main/java/com/baeldung/openliberty/hello/HelloServlet.java +++ b/open-liberty/src/main/java/com/baeldung/openliberty/app/AppServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.openliberty.hello; +package com.baeldung.openliberty.app; import java.io.IOException; @@ -8,13 +8,13 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -@WebServlet(urlPatterns="/hello") -public class HelloServlet extends HttpServlet { +@WebServlet(urlPatterns="/app") +public class AppServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - response.getWriter().append("Hello! How are you today?\n"); + response.getWriter().append("Open Liberty: greet

Hello! Welcome to Open Liberty

"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java new file mode 100644 index 0000000000..86f2709f4d --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserResource.java @@ -0,0 +1,50 @@ +package com.baeldung.openliberty.user; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.baeldung.openliberty.user.dao.UserDao; +import com.baeldung.openliberty.user.model.User; + +@RequestScoped +@Path("user") +public class UserResource { + + @Inject + UserService userService; + + @Inject + private UserDao userDAO; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public User getUser() { + return userService.getUser(); + } + + /** + * This method creates a new user from the submitted data (firstName, lastName and + * email) + */ + @POST + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + @Transactional + public Response addNewUser(@FormParam("firstName") String firstName, + @FormParam("lastName") String lastName, @FormParam("email") String email) { + System.out.println(firstName); + System.out.println(lastName); + System.out.println(email); + User newUser = new User(firstName, lastName, email); + userDAO.createUser(newUser); + return Response.status(Response.Status.NO_CONTENT).build(); + } +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java new file mode 100644 index 0000000000..4c79b9057f --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/user/UserService.java @@ -0,0 +1,18 @@ +package com.baeldung.openliberty.user; + +import javax.enterprise.context.ApplicationScoped; + +import com.baeldung.openliberty.user.model.User; + +@ApplicationScoped +public class UserService { + + public User getUser() { + User user = new User(); + user.setFirstName("Norman"); + user.setLastName("Lewis"); + user.setEmail("normanlewis@email.com"); + return user; + } + +} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java new file mode 100644 index 0000000000..4c006db4d7 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/user/dao/UserDao.java @@ -0,0 +1,42 @@ +package com.baeldung.openliberty.user.dao; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import com.baeldung.openliberty.user.model.User; + +import javax.enterprise.context.RequestScoped; + +@RequestScoped +public class UserDao { + + @PersistenceContext(name = "jpa-unit") + private EntityManager em; + + public void createUser(User user) { + em.persist(user); + } + + public User readUser(int userId) { + return em.find(User.class, userId); + } + + public void updateUser(User user) { + em.merge(user); + } + + public void deleteUser(User user) { + em.remove(user); + } + + public List readAllUsers() { + return em.createNamedQuery("User.findAll", User.class).getResultList(); + } + + public List findUser(String name, String location, String time) { + return em.createNamedQuery("User.findUser", User.class) + .setParameter("name", name) + .setParameter("location", location) + .setParameter("time", time).getResultList(); + } +} \ No newline at end of file diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java b/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java new file mode 100644 index 0000000000..6c49c95bf7 --- /dev/null +++ b/open-liberty/src/main/java/com/baeldung/openliberty/user/model/User.java @@ -0,0 +1,63 @@ +package com.baeldung.openliberty.user.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "Event") +public class User { + + @GeneratedValue(strategy = GenerationType.AUTO) + @Id + @Column(name = "id") + private int id; + + @Column(name = "first_name") + private String firstName; + + @Column(name = "last_name") + private String lastName; + + @Column(name = "email") + private String email; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public User(String firstName, String lastName, String email) { + super(); + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + } + + public User() { + super(); + } + +} diff --git a/open-liberty/src/main/liberty/config/server.xml b/open-liberty/src/main/liberty/config/server.xml index 6dfa7a8cb1..a83c0b462c 100644 --- a/open-liberty/src/main/liberty/config/server.xml +++ b/open-liberty/src/main/liberty/config/server.xml @@ -1,24 +1,51 @@ - - jaxrs-2.1 - jsonp-1.1 - cdi-2.0 - mpMetrics-2.0 - mpHealth-2.0 - mpConfig-1.3 - + + appSecurity-2.0 + jaxrs-2.1 + jsonp-1.1 + cdi-2.0 + jpa-2.2 + mpMetrics-2.0 + mpHealth-2.0 + mpConfig-1.3 + restConnector-2.0 + jdbc-4.2 + - - - + + - - - + + - + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file diff --git a/open-liberty/src/main/resources/META-INF/persistence.xml b/open-liberty/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..1ad7dd2bd8 --- /dev/null +++ b/open-liberty/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,24 @@ + + + + + org.eclipse.persistence.jpa.PersistenceProvider + + jdbc/h2test + + false + + \ No newline at end of file diff --git a/open-liberty/src/webapp/META-INF/microprofile-config.properties b/open-liberty/src/webapp/META-INF/microprofile-config.properties new file mode 100644 index 0000000000..e69de29bb2