forked from RedHatTraining/DO288-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP(ch9lab) add source for java-based todo-api app
- Loading branch information
Showing
20 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.redhat.training.example</groupId> | ||
<artifactId>todo-api</artifactId> | ||
<name>WildFly Swarm Example</name> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
|
||
<properties> | ||
<version.wildfly.swarm>2017.12.1</version.wildfly.swarm> | ||
<version.mysql>6.0.6</version.mysql> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<fabric8.maven.plugin.version>3.1.80.redhat-000019</fabric8.maven.plugin.version> | ||
<fabric8.version>2.3.6</fabric8.version> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>bom-all</artifactId> | ||
<version>${version.wildfly.swarm}</version> | ||
<scope>import</scope> | ||
<type>pom</type> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<finalName>demo</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>wildfly-swarm-plugin</artifactId> | ||
<version>${version.wildfly.swarm}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>package</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>io.fabric8</groupId> | ||
<artifactId>fabric8-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>resource</goal> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<generator> | ||
<includes> | ||
<include>wildfly-swarm</include> | ||
</includes> | ||
<excludes> | ||
<exclude>webapp</exclude> | ||
</excludes> | ||
<config> | ||
<wildfly-swarm> | ||
<fromMode>isTag</fromMode> | ||
<from>redhat-openjdk18-openshift</from> | ||
</wildfly-swarm> | ||
</config> | ||
</generator> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- Java EE 7 dependency --> | ||
<dependency> | ||
<groupId>javax</groupId> | ||
<artifactId>javaee-api</artifactId> | ||
<version>7.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- WildFly Swarm Fractions --> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>jaxrs-jsonp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>ejb</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>datasources</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>${version.mysql}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
19 changes: 19 additions & 0 deletions
19
...-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/HelloWorldEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
todo-api-micro/src/main/java/com/redhat/training/example/todoapi/rest/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
todo-api-micro/src/main/java/com/redhat/training/model/Host.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
|
53 changes: 53 additions & 0 deletions
53
todo-api-micro/src/main/java/com/redhat/training/model/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
todo-api-micro/src/main/java/com/redhat/training/rest/CORSRequestFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
todo-api-micro/src/main/java/com/redhat/training/rest/CORSResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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" ); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
todo-api-micro/src/main/java/com/redhat/training/rest/HostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
Oops, something went wrong.