From 6e57953cdcd6d4da269b4bf5c9c9a96fa9ad07ca Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 16 Jul 2019 07:13:48 -0400 Subject: [PATCH 01/15] WIP:feat(ch9s6): add initial code for GE The "mvn fabric8:deploy" command still does not work, mostly because it seems that the deployed "service" contains selectors for labels that are not present on the associated Pod. --- micro-java/pom.xml | 102 ++++++++++++++++++ .../openshift/hello/HelloResource.java | 27 +++++ .../openshift/hello/JaxRsActivator.java | 8 ++ 3 files changed, 137 insertions(+) create mode 100644 micro-java/pom.xml create mode 100644 micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java create mode 100644 micro-java/src/main/java/com/redhat/training/openshift/hello/JaxRsActivator.java diff --git a/micro-java/pom.xml b/micro-java/pom.xml new file mode 100644 index 0000000..79154c6 --- /dev/null +++ b/micro-java/pom.xml @@ -0,0 +1,102 @@ + + + + 4.0.0 + + com.redhat.training.openshift + micro-java + 1.0 + war + Red Hat Training Hello Java app + Hello microservice using Thorntail + + + + + UTF-8 + false + + + 2.4.0.Final + + + 3.1 + 2.16 + 2.5 + + 4.1.0 + + + 1.8 + 1.8 + + + + + + + + io.thorntail + bom-all + ${version.thorntail} + import + pom + + + + + + + io.thorntail + cdi + + + io.thorntail + jaxrs + + + + + + + hello + + + + + io.thorntail + thorntail-maven-plugin + ${version.thorntail} + + + + package + + + + + + io.fabric8 + fabric8-maven-plugin + ${version.fabric8.plugin} + + + fmp + + resource + build + + + + + + + + diff --git a/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java new file mode 100644 index 0000000..3db3118 --- /dev/null +++ b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java @@ -0,0 +1,27 @@ +package com.redhat.training.openshift.hello; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +@Path("/") +public class HelloResource { + + @GET + @Path("/hello") + @Produces("text/plain") + public String hello() { + String hostname = System.getenv().getOrDefault("HOSTNAME", "unknown"); + String message = System.getenv().getOrDefault("APP_MSG", null); + String response = ""; + + if (message == null) + response = "Hello world from host "+hostname+"\n"; + else + response = "Hello world from host ["+hostname+"]."; + response += "Message received = "+message+"\n"; + + return response; + } +} diff --git a/micro-java/src/main/java/com/redhat/training/openshift/hello/JaxRsActivator.java b/micro-java/src/main/java/com/redhat/training/openshift/hello/JaxRsActivator.java new file mode 100644 index 0000000..7a6a14a --- /dev/null +++ b/micro-java/src/main/java/com/redhat/training/openshift/hello/JaxRsActivator.java @@ -0,0 +1,8 @@ +package com.redhat.training.openshift.hello; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/api") +public class JaxRsActivator extends Application { +} From 698457ac05ca34aff02b1e7fc83c542540e3f9f2 Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 16 Jul 2019 14:31:01 -0400 Subject: [PATCH 02/15] WIP(ch9s6): update current code --- micro-java/pom.xml | 3 +- micro-java/src/main/fabric8/deployment.yml | 61 ++++++++++++++++++++++ micro-java/src/main/fabric8/route.yml | 19 +++++++ micro-java/src/main/fabric8/svc.yml | 26 +++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 micro-java/src/main/fabric8/deployment.yml create mode 100644 micro-java/src/main/fabric8/route.yml create mode 100644 micro-java/src/main/fabric8/svc.yml diff --git a/micro-java/pom.xml b/micro-java/pom.xml index 79154c6..edaa9bf 100644 --- a/micro-java/pom.xml +++ b/micro-java/pom.xml @@ -28,10 +28,11 @@ 2.5 + 4.1.0 - 1.8 1.8 diff --git a/micro-java/src/main/fabric8/deployment.yml b/micro-java/src/main/fabric8/deployment.yml new file mode 100644 index 0000000..57d2f01 --- /dev/null +++ b/micro-java/src/main/fabric8/deployment.yml @@ -0,0 +1,61 @@ +--- +apiVersion: "v1" +kind: "DeploymentConfig" +metadata: + annotations: + fabric8.io/iconUrl: "img/icons/camel.svg" + labels: + provider: "fabric8" + project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" + version: "1.0" + group: "com.redhat.training" + name: "${project.artifactId}" +spec: + replicas: 1 + selector: + project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" + provider: "fabric8" + group: "com.redhat.training" + strategy: + rollingParams: + timeoutSeconds: 10800 + type: "Rolling" + template: + metadata: + annotations: + fabric8.io/iconUrl: "img/icons/camel.svg" + labels: + provider: "fabric8" + project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" + version: "1.0" + group: "com.redhat.training" + spec: + containers: + - image: "${project.artifactId}:1.0" + imagePullPolicy: "IfNotPresent" + name: "spring-boot" + ports: + - containerPort: 8080 + name: "http" + protocol: "TCP" + #- containerPort: 9081 + # name: "rest" + # protocol: "TCP" + securityContext: + privileged: false + #readinessProbe: + # httpGet: + # path: "/hello/health" + # port: 9081 + # initialDelaySeconds: 20 + triggers: + - type: "ConfigChange" + - imageChangeParams: + automatic: true + containerNames: + - "spring-boot" + from: + kind: "ImageStreamTag" + name: "${project.artifactId}:1.0" + type: "ImageChange" + diff --git a/micro-java/src/main/fabric8/route.yml b/micro-java/src/main/fabric8/route.yml new file mode 100644 index 0000000..1861f0e --- /dev/null +++ b/micro-java/src/main/fabric8/route.yml @@ -0,0 +1,19 @@ +--- +apiVersion: v1 +kind: Route +metadata: + labels: + expose: "true" + app: ${project.artifactId} + provider: fabric8 + version: "1.0" + group: com.redhat.training + name: ${project.artifactId} +spec: +# host: ${hostname} + port: + targetPort: 8080 + to: + kind: Service + name: ${project.artifactId} + diff --git a/micro-java/src/main/fabric8/svc.yml b/micro-java/src/main/fabric8/svc.yml new file mode 100644 index 0000000..8dbcfce --- /dev/null +++ b/micro-java/src/main/fabric8/svc.yml @@ -0,0 +1,26 @@ +--- +apiVersion: v1 +kind: Service +metadata: +# annotations: +# fabric8.io/iconUrl: img/icons/camel.svg + labels: + expose: "true" + app: ${project.artifactId} + provider: fabric8 + version: "1.0" + group: com.redhat.training + name: ${project.artifactId} +spec: + ports: + - name: http-tomcat + port: 8080 + protocol: TCP + targetPort: 8080 +# - name: rest +# port: ${rest-port} +# protocol: TCP +# targetPort: ${rest-port} + selector: + deploymentconfig: ${project.artifactId} + From 4fcb06042fc64f09921c92cb63161de450867f6a Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 16 Jul 2019 15:24:45 -0400 Subject: [PATCH 03/15] WIP:feat(ch9s6): remove YAML fragments The YAML fragments are wrong, and are causing issues when deployed. The "Zero Config" option with fabric8 works well. --- micro-java/src/main/fabric8/deployment.yml | 61 ---------------------- micro-java/src/main/fabric8/route.yml | 19 ------- micro-java/src/main/fabric8/svc.yml | 26 --------- 3 files changed, 106 deletions(-) delete mode 100644 micro-java/src/main/fabric8/deployment.yml delete mode 100644 micro-java/src/main/fabric8/route.yml delete mode 100644 micro-java/src/main/fabric8/svc.yml diff --git a/micro-java/src/main/fabric8/deployment.yml b/micro-java/src/main/fabric8/deployment.yml deleted file mode 100644 index 57d2f01..0000000 --- a/micro-java/src/main/fabric8/deployment.yml +++ /dev/null @@ -1,61 +0,0 @@ ---- -apiVersion: "v1" -kind: "DeploymentConfig" -metadata: - annotations: - fabric8.io/iconUrl: "img/icons/camel.svg" - labels: - provider: "fabric8" - project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" - version: "1.0" - group: "com.redhat.training" - name: "${project.artifactId}" -spec: - replicas: 1 - selector: - project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" - provider: "fabric8" - group: "com.redhat.training" - strategy: - rollingParams: - timeoutSeconds: 10800 - type: "Rolling" - template: - metadata: - annotations: - fabric8.io/iconUrl: "img/icons/camel.svg" - labels: - provider: "fabric8" - project: "${env.RHT_OCP4_DEV_USER}-${project.artifactId}" - version: "1.0" - group: "com.redhat.training" - spec: - containers: - - image: "${project.artifactId}:1.0" - imagePullPolicy: "IfNotPresent" - name: "spring-boot" - ports: - - containerPort: 8080 - name: "http" - protocol: "TCP" - #- containerPort: 9081 - # name: "rest" - # protocol: "TCP" - securityContext: - privileged: false - #readinessProbe: - # httpGet: - # path: "/hello/health" - # port: 9081 - # initialDelaySeconds: 20 - triggers: - - type: "ConfigChange" - - imageChangeParams: - automatic: true - containerNames: - - "spring-boot" - from: - kind: "ImageStreamTag" - name: "${project.artifactId}:1.0" - type: "ImageChange" - diff --git a/micro-java/src/main/fabric8/route.yml b/micro-java/src/main/fabric8/route.yml deleted file mode 100644 index 1861f0e..0000000 --- a/micro-java/src/main/fabric8/route.yml +++ /dev/null @@ -1,19 +0,0 @@ ---- -apiVersion: v1 -kind: Route -metadata: - labels: - expose: "true" - app: ${project.artifactId} - provider: fabric8 - version: "1.0" - group: com.redhat.training - name: ${project.artifactId} -spec: -# host: ${hostname} - port: - targetPort: 8080 - to: - kind: Service - name: ${project.artifactId} - diff --git a/micro-java/src/main/fabric8/svc.yml b/micro-java/src/main/fabric8/svc.yml deleted file mode 100644 index 8dbcfce..0000000 --- a/micro-java/src/main/fabric8/svc.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -apiVersion: v1 -kind: Service -metadata: -# annotations: -# fabric8.io/iconUrl: img/icons/camel.svg - labels: - expose: "true" - app: ${project.artifactId} - provider: fabric8 - version: "1.0" - group: com.redhat.training - name: ${project.artifactId} -spec: - ports: - - name: http-tomcat - port: 8080 - protocol: TCP - targetPort: 8080 -# - name: rest -# port: ${rest-port} -# protocol: TCP -# targetPort: ${rest-port} - selector: - deploymentconfig: ${project.artifactId} - From eaf20b0fe63d0edae705cc260524c13044e66301 Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 16 Jul 2019 15:46:12 -0400 Subject: [PATCH 04/15] WIP:fix(ch9s6): add braces for if block --- .../com/redhat/training/openshift/hello/HelloResource.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java index 3db3118..7b39d65 100644 --- a/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java +++ b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java @@ -16,12 +16,12 @@ public class HelloResource { String message = System.getenv().getOrDefault("APP_MSG", null); String response = ""; - if (message == null) + if (message == null) { response = "Hello world from host "+hostname+"\n"; - else + } else { response = "Hello world from host ["+hostname+"]."; response += "Message received = "+message+"\n"; - + } return response; } } From 213f25767057aeb33fa6d900e69f26c6675e1bd3 Mon Sep 17 00:00:00 2001 From: Dan K Date: Tue, 16 Jul 2019 16:34:24 -0400 Subject: [PATCH 05/15] WIP:feat(ch9s6): add custom dc and cm resources Use the fabric8 plugin to add a custom deployment config and config map as part of the deployment to OpenShift. --- micro-java/src/main/fabric8/cm.yml | 7 +++++++ micro-java/src/main/fabric8/deployment.yml | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 micro-java/src/main/fabric8/cm.yml create mode 100644 micro-java/src/main/fabric8/deployment.yml diff --git a/micro-java/src/main/fabric8/cm.yml b/micro-java/src/main/fabric8/cm.yml new file mode 100644 index 0000000..a6763d0 --- /dev/null +++ b/micro-java/src/main/fabric8/cm.yml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: env-config +data: + APP_MSG: + diff --git a/micro-java/src/main/fabric8/deployment.yml b/micro-java/src/main/fabric8/deployment.yml new file mode 100644 index 0000000..941530b --- /dev/null +++ b/micro-java/src/main/fabric8/deployment.yml @@ -0,0 +1,11 @@ + + +spec: + template: + spec: + containers: + - envFrom: + - configMapRef: + name: env-config + + From 9963c0bd49817821ec545e3cb1d936f70552ee88 Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 10:00:48 -0400 Subject: [PATCH 06/15] style(micro-java): fix pom.xml whitespace --- micro-java/pom.xml | 97 +++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/micro-java/pom.xml b/micro-java/pom.xml index edaa9bf..9c8b9cd 100644 --- a/micro-java/pom.xml +++ b/micro-java/pom.xml @@ -1,46 +1,40 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 - com.redhat.training.openshift - micro-java - 1.0 - war - Red Hat Training Hello Java app - Hello microservice using Thorntail + com.redhat.training.openshift + hello + 1.0 + war + Red Hat Training Hello Java app + Hello microservice using Thorntail - - - - UTF-8 - false + + + + UTF-8 + false - - 2.4.0.Final + + 2.4.0.Final - - 3.1 - 2.16 - 2.5 - - - 4.1.0 - - 1.8 - 1.8 + + 3.1 + 2.16 + 2.5 + 4.1.0 - + + 1.8 + 1.8 + - + io.thorntail @@ -52,26 +46,25 @@ - - - io.thorntail - cdi - - - io.thorntail - jaxrs - + + + io.thorntail + cdi + + + io.thorntail + jaxrs + + - - - - - hello - - - - + + + hello + + + + io.thorntail thorntail-maven-plugin ${version.thorntail} From f9e42817fafbb28b602b0a5873dbd90459a8a81f Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 12:10:29 -0400 Subject: [PATCH 07/15] fix(micro-java): add newline char to output str --- .../java/com/redhat/training/openshift/hello/HelloResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java index 7b39d65..41084a4 100644 --- a/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java +++ b/micro-java/src/main/java/com/redhat/training/openshift/hello/HelloResource.java @@ -19,7 +19,7 @@ public class HelloResource { if (message == null) { response = "Hello world from host "+hostname+"\n"; } else { - response = "Hello world from host ["+hostname+"]."; + response = "Hello world from host ["+hostname+"].\n"; response += "Message received = "+message+"\n"; } return response; From 9c1fe679d71c877c43d506c8e25392ec2d28a5a8 Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 13:38:59 -0400 Subject: [PATCH 08/15] fix(ch9s6): remove YAML fragments --- micro-java/src/main/fabric8/cm.yml | 7 ------- micro-java/src/main/fabric8/deployment.yml | 11 ----------- 2 files changed, 18 deletions(-) delete mode 100644 micro-java/src/main/fabric8/cm.yml delete mode 100644 micro-java/src/main/fabric8/deployment.yml diff --git a/micro-java/src/main/fabric8/cm.yml b/micro-java/src/main/fabric8/cm.yml deleted file mode 100644 index a6763d0..0000000 --- a/micro-java/src/main/fabric8/cm.yml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: env-config -data: - APP_MSG: - diff --git a/micro-java/src/main/fabric8/deployment.yml b/micro-java/src/main/fabric8/deployment.yml deleted file mode 100644 index 941530b..0000000 --- a/micro-java/src/main/fabric8/deployment.yml +++ /dev/null @@ -1,11 +0,0 @@ - - -spec: - template: - spec: - containers: - - envFrom: - - configMapRef: - name: env-config - - From a4c63d7a84035eb43cccebc5ca75a825bb4b2ef6 Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 13:41:15 -0400 Subject: [PATCH 09/15] fix(ch9s6): add placeholder directory --- micro-java/src/main/fabric8/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 micro-java/src/main/fabric8/.gitkeep diff --git a/micro-java/src/main/fabric8/.gitkeep b/micro-java/src/main/fabric8/.gitkeep new file mode 100644 index 0000000..e69de29 From 2c450b33f4c68e5800eb2eac01f5442fe294b5c5 Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 16:25:45 -0400 Subject: [PATCH 10/15] fix(ch4s4): refactor java source for SG read ease --- .../rest/ServerHostEndPoint.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java b/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java index a5ff9c1..8d3163d 100644 --- a/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java +++ b/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java @@ -1,26 +1,26 @@ package com.redhat.training.example.javaserverhost.rest; - import javax.ws.rs.Path; import javax.ws.rs.core.Response; import javax.ws.rs.GET; import javax.ws.rs.Produces; import java.net.InetAddress; - @Path("/") public class ServerHostEndPoint { - @GET - @Produces("text/plain") - public Response doGet() { - String host = ""; - try { - host = InetAddress.getLocalHost().getHostName(); - } - catch (Exception e) { - e.printStackTrace(); - } - return Response.ok("I am running on server "+host+" Version 1.0 \n").build(); - } + @GET + @Produces("text/plain") + public Response doGet() { + String host = ""; + try { + host = InetAddress.getLocalHost().getHostName(); + } + catch (Exception e) { + e.printStackTrace(); + } + String msg = "I am running on server "+host+" Version 1.0 \n" + return Response.ok(msg).build(); + } } + From c37d409c1d8ca28f3fbe38ee32415a226b5a8db4 Mon Sep 17 00:00:00 2001 From: Dan K Date: Fri, 19 Jul 2019 16:42:26 -0400 Subject: [PATCH 11/15] fix(ch4s4): correct syntax error --- .../example/javaserverhost/rest/ServerHostEndPoint.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java b/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java index 8d3163d..b09770a 100644 --- a/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java +++ b/java-serverhost/src/main/java/com/redhat/training/example/javaserverhost/rest/ServerHostEndPoint.java @@ -19,7 +19,7 @@ public class ServerHostEndPoint { catch (Exception e) { e.printStackTrace(); } - String msg = "I am running on server "+host+" Version 1.0 \n" + String msg = "I am running on server "+host+" Version 1.0 \n"; return Response.ok(msg).build(); } } From 81fbcfc2409f885dd8405042b05d272d4eb629e5 Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 22 Jul 2019 15:38:35 -0400 Subject: [PATCH 12/15] WIP(ch9lab) add source for java-based todo-api app --- todo-api-micro/pom.xml | 111 ++++++++++++++++++ .../src/main/fabric8/deployment.yml | 21 ++++ todo-api-micro/src/main/fabric8/route.yml | 18 +++ todo-api-micro/src/main/fabric8/svc.yml | 21 ++++ .../todoapi/rest/HelloWorldEndpoint.java | 19 +++ .../training/example/todoapi/rest/Main.java | 25 ++++ .../java/com/redhat/training/model/Host.java | 24 ++++ .../java/com/redhat/training/model/Item.java | 53 +++++++++ .../training/rest/CORSRequestFilter.java | 30 +++++ .../training/rest/CORSResponseFilter.java | 32 +++++ .../com/redhat/training/rest/HostService.java | 28 +++++ .../com/redhat/training/rest/ItemService.java | 97 +++++++++++++++ .../redhat/training/rest/RestApplication.java | 8 ++ .../training/ui/PaginatedListWrapper.java | 72 ++++++++++++ .../main/resources/META-INF/persistence.xml | 18 +++ .../modules/com/mysql/main/module.xml | 16 +++ .../src/main/resources/project-defaults.yml | 13 ++ .../src/main/resources/sql/create.sql | 1 + .../src/main/resources/sql/drop.sql | 1 + .../src/main/resources/sql/load.sql | 2 + 20 files changed, 610 insertions(+) create mode 100755 todo-api-micro/pom.xml create mode 100644 todo-api-micro/src/main/fabric8/deployment.yml create mode 100644 todo-api-micro/src/main/fabric8/route.yml create mode 100644 todo-api-micro/src/main/fabric8/svc.yml create mode 100755 todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/HelloWorldEndpoint.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/Main.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/model/Host.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/model/Item.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/rest/CORSRequestFilter.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/rest/CORSResponseFilter.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/rest/HostService.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/rest/ItemService.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/rest/RestApplication.java create mode 100644 todo-api-micro/src/main/java/com/redhat/training/ui/PaginatedListWrapper.java create mode 100644 todo-api-micro/src/main/resources/META-INF/persistence.xml create mode 100644 todo-api-micro/src/main/resources/modules/com/mysql/main/module.xml create mode 100644 todo-api-micro/src/main/resources/project-defaults.yml create mode 100644 todo-api-micro/src/main/resources/sql/create.sql create mode 100644 todo-api-micro/src/main/resources/sql/drop.sql create mode 100644 todo-api-micro/src/main/resources/sql/load.sql diff --git a/todo-api-micro/pom.xml b/todo-api-micro/pom.xml new file mode 100755 index 0000000..b227a11 --- /dev/null +++ b/todo-api-micro/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + com.redhat.training.example + todo-api + WildFly Swarm Example + 1.0.0-SNAPSHOT + war + + + 2017.12.1 + 6.0.6 + 1.8 + 1.8 + false + UTF-8 + 3.1.80.redhat-000019 + 2.3.6 + + + + + + org.wildfly.swarm + bom-all + ${version.wildfly.swarm} + import + pom + + + + + + demo + + + org.wildfly.swarm + wildfly-swarm-plugin + ${version.wildfly.swarm} + + + + package + + + + + + io.fabric8 + fabric8-maven-plugin + + + + resource + build + + + + + + + wildfly-swarm + + + webapp + + + + isTag + redhat-openjdk18-openshift + + + + + + + + + + + + javax + javaee-api + 7.0 + provided + + + + org.wildfly.swarm + jaxrs-jsonp + + + org.wildfly.swarm + jpa + + + org.wildfly.swarm + ejb + + + org.wildfly.swarm + datasources + + + + mysql + mysql-connector-java + ${version.mysql} + + + diff --git a/todo-api-micro/src/main/fabric8/deployment.yml b/todo-api-micro/src/main/fabric8/deployment.yml new file mode 100644 index 0000000..f9fa89d --- /dev/null +++ b/todo-api-micro/src/main/fabric8/deployment.yml @@ -0,0 +1,21 @@ +spec: + template: + spec: + containers: + - + resources: + requests: + cpu: "0.2" +# memory: 256Mi + limits: + cpu: "1.0" +# memory: 256Mi + env: + - name: DATABASE_USER + value: '${database.user}' + - name: DATABASE_PASSWORD + value: '${database.password}' + - name: DATABASE_SVC_HOSTNAME + value: '${database.svc.hostname}' + - name: DATABASE_NAME + value: '${database.name}' diff --git a/todo-api-micro/src/main/fabric8/route.yml b/todo-api-micro/src/main/fabric8/route.yml new file mode 100644 index 0000000..d7f59ab --- /dev/null +++ b/todo-api-micro/src/main/fabric8/route.yml @@ -0,0 +1,18 @@ +--- +apiVersion: v1 +kind: Route +metadata: + labels: + expose: "true" + app: ${project.artifactId} + provider: fabric8 + version: "1.0" + group: com.redhat.training + name: ${project.artifactId} +spec: + host: ${hostname} + port: + targetPort: ${service-port} + to: + kind: Service + name: ${project.artifactId} diff --git a/todo-api-micro/src/main/fabric8/svc.yml b/todo-api-micro/src/main/fabric8/svc.yml new file mode 100644 index 0000000..b812eb9 --- /dev/null +++ b/todo-api-micro/src/main/fabric8/svc.yml @@ -0,0 +1,21 @@ +--- +apiVersion: v1 +kind: Service +metadata: + annotations: + fabric8.io/iconUrl: img/icons/camel.svg + labels: + expose: "true" + app: ${project.artifactId} + provider: fabric8 + version: "1.0" + group: com.redhat.training + name: ${project.artifactId} +spec: + ports: + - name: http + port: ${service-port} + protocol: TCP + targetPort: ${service-port} + selector: + deploymentconfig: ${project.artifactId} diff --git a/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/HelloWorldEndpoint.java b/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/HelloWorldEndpoint.java new file mode 100755 index 0000000..ec8817b --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/HelloWorldEndpoint.java @@ -0,0 +1,19 @@ +package com.redhat.training.example.todoapi.rest; + + +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; +import javax.ws.rs.GET; +import javax.ws.rs.Produces; + + +@Path("/hello") +public class HelloWorldEndpoint { + + @GET + @Produces("text/plain") + public Response doGet() { + return Response.ok("Hello World!").build(); + + } +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/Main.java b/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/Main.java new file mode 100644 index 0000000..2dbeece --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/Main.java @@ -0,0 +1,25 @@ +package com.redhat.training.example.todoapi.rest; + +import org.wildfly.swarm.Swarm; +import org.wildfly.swarm.datasources.DatasourcesFraction; + +public class Main { + public static void main(String[] args) throws Exception { + String host = System.getenv("HOST"); + new Swarm() + .fraction(new DatasourcesFraction() + .jdbcDriver("mysql", (d) -> { + d.driverClassName("com.mysql.cj.jdbc.Driver"); + d.xaDatasourceClass("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"); + d.driverModuleName("com.mysql"); + }) + .dataSource("MySQLDS", (ds) -> { + ds.driverName("mysql"); + ds.connectionUrl("jdbc:mysql://"+host+":8889/todo"); + ds.userName("root"); + ds.password("root"); + })) + .start() + .deploy(); + } +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/model/Host.java b/todo-api-micro/src/main/java/com/redhat/training/model/Host.java new file mode 100644 index 0000000..30dbcf2 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/model/Host.java @@ -0,0 +1,24 @@ +package com.redhat.training.model; + +import java.net.InetAddress; + +public class Host { + + private String ip; + private String hostname; + + public Host(String ip, String hostname) { + this.ip = ip; + this.hostname = hostname; + } + + public String getIp() { + return ip; + } + + public String getHostname() { + return hostname; + } + +} + diff --git a/todo-api-micro/src/main/java/com/redhat/training/model/Item.java b/todo-api-micro/src/main/java/com/redhat/training/model/Item.java new file mode 100644 index 0000000..5067fe5 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/model/Item.java @@ -0,0 +1,53 @@ +package com.redhat.training.model; + +import javax.persistence.*; + +@Entity +public class Item { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String description; + + private Boolean done = false; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Boolean isDone() { + return done; + } + + public void setDone(Boolean done) { + this.done = done; + } + + @Override + public boolean equals(Object o) { + if (this == o) { return true; } + if (o == null || getClass() != o.getClass()) { return false; } + + Item item = (Item) o; + + return id.equals(item.id); + } + + @Override + public int hashCode() { + return id.hashCode(); + } +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/rest/CORSRequestFilter.java b/todo-api-micro/src/main/java/com/redhat/training/rest/CORSRequestFilter.java new file mode 100644 index 0000000..d8b03ce --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/rest/CORSRequestFilter.java @@ -0,0 +1,30 @@ +package com.redhat.training.rest; + +import java.io.IOException; +import java.util.logging.Logger; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; + +@Provider +@PreMatching +public class CORSRequestFilter implements ContainerRequestFilter { + + private final static Logger log = Logger.getLogger(CORSRequestFilter.class.getName()); + + @Override + public void filter(ContainerRequestContext requestCtx) throws IOException { + log.fine("Executing REST request filter"); + + // When HttpMethod comes as OPTIONS, just acknowledge that it accepts... + if (requestCtx.getRequest().getMethod().equals( "OPTIONS" )) { + log.fine("HTTP Method (OPTIONS) - Detected!"); + + // Just send a OK signal back to the browser + requestCtx.abortWith(Response.status(Response.Status.OK).build()); + } + } +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/rest/CORSResponseFilter.java b/todo-api-micro/src/main/java/com/redhat/training/rest/CORSResponseFilter.java new file mode 100644 index 0000000..6421190 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/rest/CORSResponseFilter.java @@ -0,0 +1,32 @@ +package com.redhat.training.rest; + +import java.io.IOException; +import java.util.logging.Logger; + +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; + +@Provider +@PreMatching +public class CORSResponseFilter implements ContainerResponseFilter { + + private final static Logger log = Logger.getLogger(CORSResponseFilter.class.getName()); + + @Override + public void filter(ContainerRequestContext requestCtx, ContainerResponseContext responseCtx) throws IOException { + log.fine("Executing REST response filter"); + + MultivaluedMap headers = responseCtx.getHeaders(); + + headers.add("Access-Control-Allow-Origin", "*"); + headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS"); + headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type"); + //responseCtx.getHeaders().add( "Access-Control-Allow-Credentials", "true" ); + } + +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/rest/HostService.java b/todo-api-micro/src/main/java/com/redhat/training/rest/HostService.java new file mode 100644 index 0000000..f62aac1 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/rest/HostService.java @@ -0,0 +1,28 @@ +package com.redhat.training.rest; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +import javax.ejb.Stateless; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import com.redhat.training.model.Host; + +@Stateless +@Path("host") +@Consumes(MediaType.APPLICATION_JSON) +@Produces(MediaType.APPLICATION_JSON) +public class HostService { + + @GET + public Host getHostInfo() throws UnknownHostException { + InetAddress address = InetAddress.getLocalHost(); + Host host = new Host(address.getHostAddress(), address.getHostName()); + return host; + } +} + diff --git a/todo-api-micro/src/main/java/com/redhat/training/rest/ItemService.java b/todo-api-micro/src/main/java/com/redhat/training/rest/ItemService.java new file mode 100644 index 0000000..bf0b030 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/rest/ItemService.java @@ -0,0 +1,97 @@ +package com.redhat.training.rest; + +import com.redhat.training.model.Item; +import com.redhat.training.ui.PaginatedListWrapper; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import javax.ws.rs.*; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; + +import java.util.List; + +@Stateless +@Path("items") +@Consumes(MediaType.APPLICATION_JSON) +@Produces(MediaType.APPLICATION_JSON) +public class ItemService extends Application { + + @PersistenceContext + private EntityManager entityManager; + + private Integer countItems() { + Query query = entityManager.createQuery("SELECT COUNT(i.id) FROM Item i"); + return ((Long) query.getSingleResult()).intValue(); + } + + private List findItems(int startPosition, int maxResults, String sortFields, String sortDirections) { + TypedQuery query = + entityManager.createQuery("SELECT i FROM Item i ORDER BY i." + sortFields + " " + sortDirections, + Item.class); + query.setFirstResult(startPosition); + query.setMaxResults(maxResults); + return query.getResultList(); + } + + private PaginatedListWrapper findItems(PaginatedListWrapper wrapper) { + wrapper.setTotalResults(countItems()); + int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize(); + wrapper.setList(findItems(start, + wrapper.getPageSize(), + wrapper.getSortFields(), + wrapper.getSortDirections())); + return wrapper; + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public PaginatedListWrapper listItems(@DefaultValue("1") + @QueryParam("page") + Integer page, + @DefaultValue("id") + @QueryParam("sortFields") + String sortFields, + @DefaultValue("asc") + @QueryParam("sortDirections") + String sortDirections) { + PaginatedListWrapper paginatedListWrapper = new PaginatedListWrapper(); + paginatedListWrapper.setCurrentPage(page); + paginatedListWrapper.setSortFields(sortFields); + paginatedListWrapper.setSortDirections(sortDirections); + paginatedListWrapper.setPageSize(10); + return findItems(paginatedListWrapper); + } + + @GET + @Path("{id}") + public Item getitem(@PathParam("id") Long id) { + return entityManager.find(Item.class, id); + } + + @POST + public Item saveItem(Item item) { + if (item.getId() == null) { + Item itemToSave = new Item(); + itemToSave.setDescription(item.getDescription()); + itemToSave.setDone(item.isDone()); + entityManager.persist(item); + } else { + Item itemToUpdate = getitem(item.getId()); + itemToUpdate.setDescription(item.getDescription()); + itemToUpdate.setDone(item.isDone()); + item = entityManager.merge(itemToUpdate); + } + + return item; + } + + @DELETE + @Path("{id}") + public void deleteItem(@PathParam("id") Long id) { + entityManager.remove(getitem(id)); + } +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/rest/RestApplication.java b/todo-api-micro/src/main/java/com/redhat/training/rest/RestApplication.java new file mode 100644 index 0000000..d6aa93a --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/rest/RestApplication.java @@ -0,0 +1,8 @@ +package com.redhat.training.rest; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/todo/api") +public class RestApplication extends Application { +} diff --git a/todo-api-micro/src/main/java/com/redhat/training/ui/PaginatedListWrapper.java b/todo-api-micro/src/main/java/com/redhat/training/ui/PaginatedListWrapper.java new file mode 100644 index 0000000..7eaeb51 --- /dev/null +++ b/todo-api-micro/src/main/java/com/redhat/training/ui/PaginatedListWrapper.java @@ -0,0 +1,72 @@ +package com.redhat.training.ui; + +import com.redhat.training.model.Item; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +import java.io.Serializable; +import java.util.List; + +@XmlRootElement +public class PaginatedListWrapper implements Serializable { + + private static final long serialVersionUID = 1L; + + private Integer currentPage; + private Integer pageSize; + private Integer totalResults; + + private String sortFields; + private String sortDirections; + @XmlElement + private List list; + + public Integer getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Integer currentPage) { + this.currentPage = currentPage; + } + + public Integer getPageSize() { + return pageSize; + } + + public void setPageSize(Integer pageSize) { + this.pageSize = pageSize; + } + + public Integer getTotalResults() { + return totalResults; + } + + public void setTotalResults(Integer totalResults) { + this.totalResults = totalResults; + } + + public String getSortFields() { + return sortFields; + } + + public void setSortFields(String sortFields) { + this.sortFields = sortFields; + } + + public String getSortDirections() { + return sortDirections; + } + + public void setSortDirections(String sortDirections) { + this.sortDirections = sortDirections; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } +} diff --git a/todo-api-micro/src/main/resources/META-INF/persistence.xml b/todo-api-micro/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..acf40ff --- /dev/null +++ b/todo-api-micro/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,18 @@ + + + + java:jboss/datasources/MySQLDS + + + + + + + + + + + diff --git a/todo-api-micro/src/main/resources/modules/com/mysql/main/module.xml b/todo-api-micro/src/main/resources/modules/com/mysql/main/module.xml new file mode 100644 index 0000000..6d88728 --- /dev/null +++ b/todo-api-micro/src/main/resources/modules/com/mysql/main/module.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/todo-api-micro/src/main/resources/project-defaults.yml b/todo-api-micro/src/main/resources/project-defaults.yml new file mode 100644 index 0000000..16a4f1e --- /dev/null +++ b/todo-api-micro/src/main/resources/project-defaults.yml @@ -0,0 +1,13 @@ +swarm: + datasources: + jdbc-drivers: + com.mysql: + driver-class-name: com.mysql.cj.jdbc.Driver + xa-datasource-class-name: com.mysql.jdbc.jdbc2.optional.MysqlXADataSource + driver-module-name: com.mysql + data-sources: + MySQLDS: + driver-name: com.mysql + connection-url: jdbc:mysql://${env.DATABASE_SVC_HOSTNAME}:3306/${env.DATABASE_NAME} + user-name: ${env.DATABASE_USER} + password: ${env.DATABASE_PASSWORD} diff --git a/todo-api-micro/src/main/resources/sql/create.sql b/todo-api-micro/src/main/resources/sql/create.sql new file mode 100644 index 0000000..6289f7c --- /dev/null +++ b/todo-api-micro/src/main/resources/sql/create.sql @@ -0,0 +1 @@ +CREATE TABLE `Item` (`id` BIGINT not null auto_increment primary key, `description` VARCHAR(100), `done` BIT); diff --git a/todo-api-micro/src/main/resources/sql/drop.sql b/todo-api-micro/src/main/resources/sql/drop.sql new file mode 100644 index 0000000..35ef5b3 --- /dev/null +++ b/todo-api-micro/src/main/resources/sql/drop.sql @@ -0,0 +1 @@ +DROP TABLE `Item`; diff --git a/todo-api-micro/src/main/resources/sql/load.sql b/todo-api-micro/src/main/resources/sql/load.sql new file mode 100644 index 0000000..6dec667 --- /dev/null +++ b/todo-api-micro/src/main/resources/sql/load.sql @@ -0,0 +1,2 @@ +INSERT INTO `Item` (`id`,`description`,`done`) VALUES (1,'Pick up newspaper', 0); +INSERT INTO `Item` (`id`,`description`,`done`) VALUES (2,'Buy groceries', 1); From c1c73e323723213f0636b45c0d83be2046335d4e Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 22 Jul 2019 16:04:38 -0400 Subject: [PATCH 13/15] WIP(ch9lab) update POM dependencies --- todo-api-micro/pom.xml | 81 ++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/todo-api-micro/pom.xml b/todo-api-micro/pom.xml index b227a11..f2e46ef 100755 --- a/todo-api-micro/pom.xml +++ b/todo-api-micro/pom.xml @@ -1,42 +1,73 @@ - + + 4.0.0 + com.redhat.training.example todo-api - WildFly Swarm Example 1.0.0-SNAPSHOT war + ToDo API Thorntail Example + ToDo API microservice using Thorntail - 2017.12.1 - 6.0.6 - 1.8 - 1.8 - false + + UTF-8 - 3.1.80.redhat-000019 - 2.3.6 + false + + + 2.4.0.Final + + + 3.1 + 2.16 + 2.5 + 4.1.0 + + + 1.8 + 1.8 + - org.wildfly.swarm + io.thorntail bom-all - ${version.wildfly.swarm} + ${version.thorntail} import pom + + + io.thorntail + cdi + + + io.thorntail + jaxrs + + + - demo + + hello + + - org.wildfly.swarm - wildfly-swarm-plugin - ${version.wildfly.swarm} + io.thorntail + thorntail-maven-plugin + ${version.thorntail} @@ -48,30 +79,16 @@ io.fabric8 fabric8-maven-plugin + ${version.fabric8.plugin} + fmp resource build - - - - wildfly-swarm - - - webapp - - - - isTag - redhat-openjdk18-openshift - - - - @@ -108,4 +125,6 @@ ${version.mysql} + + From 16a11f2775b6ab759e3cce2fb4f4b12c5c88f76a Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 22 Jul 2019 16:10:39 -0400 Subject: [PATCH 14/15] WIP(ch9lab) fix pom.xml dependencies --- todo-api-micro/pom.xml | 61 +++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/todo-api-micro/pom.xml b/todo-api-micro/pom.xml index f2e46ef..f834f64 100755 --- a/todo-api-micro/pom.xml +++ b/todo-api-micro/pom.xml @@ -47,6 +47,28 @@ + + javax + javaee-api + 7.0 + provided + + + io.thorntail + jaxrs-jsonp + + + io.thorntail + jpa + + + io.thorntail + ejb + + + io.thorntail + datasources + io.thorntail cdi @@ -55,6 +77,11 @@ io.thorntail jaxrs + + mysql + mysql-connector-java + ${version.mysql} + @@ -93,38 +120,4 @@ - - - - javax - javaee-api - 7.0 - provided - - - - org.wildfly.swarm - jaxrs-jsonp - - - org.wildfly.swarm - jpa - - - org.wildfly.swarm - ejb - - - org.wildfly.swarm - datasources - - - - mysql - mysql-connector-java - ${version.mysql} - - - - From 137aecb997bb7263194c9acee2bff310a0cf17bf Mon Sep 17 00:00:00 2001 From: Dan K Date: Mon, 22 Jul 2019 21:58:46 -0400 Subject: [PATCH 15/15] WIP(ch9lab): update mysql jdbc version in pom.xml --- todo-api-micro/pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/todo-api-micro/pom.xml b/todo-api-micro/pom.xml index f834f64..9689604 100755 --- a/todo-api-micro/pom.xml +++ b/todo-api-micro/pom.xml @@ -22,6 +22,9 @@ 2.4.0.Final + + 8.0.16 + 3.1 2.16