tomcat app ex (#2625)
This commit is contained in:
parent
dca9403876
commit
8ed5c8e293
3
guest/tomcat-app/WebContent/META-INF/MANIFEST.MF
Normal file
3
guest/tomcat-app/WebContent/META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
Class-Path:
|
||||||
|
|
28
guest/tomcat-app/WebContent/WEB-INF/web.xml
Normal file
28
guest/tomcat-app/WebContent/WEB-INF/web.xml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||||
|
id="WebApp_ID" version="3.1">
|
||||||
|
<display-name>tomcat-app</display-name>
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>tomcat-app</servlet-name>
|
||||||
|
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
|
||||||
|
<init-param>
|
||||||
|
<param-name>javax.ws.rs.Application</param-name>
|
||||||
|
<param-value>com.stackify.ApplicationInitializer</param-value>
|
||||||
|
</init-param>
|
||||||
|
<load-on-startup>1</load-on-startup>
|
||||||
|
</servlet>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>tomcat-app</servlet-name>
|
||||||
|
<url-pattern>/*</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
<filter>
|
||||||
|
<filter-name>javamelody</filter-name>
|
||||||
|
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
|
||||||
|
<init-param>
|
||||||
|
<param-name>gzip-compression-disabled</param-name>
|
||||||
|
<param-value>true</param-value>
|
||||||
|
</init-param>
|
||||||
|
</filter>
|
||||||
|
</web-app>
|
70
guest/tomcat-app/pom.xml
Normal file
70
guest/tomcat-app/pom.xml
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<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.stackify</groupId>
|
||||||
|
<artifactId>tomcat-app</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.containers</groupId>
|
||||||
|
<artifactId>jersey-container-servlet</artifactId>
|
||||||
|
<version>2.25.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.media</groupId>
|
||||||
|
<artifactId>jersey-media-moxy</artifactId>
|
||||||
|
<version>2.25.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.rest-assured</groupId>
|
||||||
|
<artifactId>rest-assured</artifactId>
|
||||||
|
<version>3.0.3</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.195</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>2.8.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bull.javamelody</groupId>
|
||||||
|
<artifactId>javamelody-core</artifactId>
|
||||||
|
<version>1.69.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>3.0.0</version>
|
||||||
|
<configuration>
|
||||||
|
<warSourceDirectory>WebContent</warSourceDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.stackify;
|
||||||
|
|
||||||
|
import org.glassfish.jersey.server.ResourceConfig;
|
||||||
|
|
||||||
|
public class ApplicationInitializer extends ResourceConfig {
|
||||||
|
public ApplicationInitializer() {
|
||||||
|
packages("com.stackify.services");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package com.stackify.daos;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import com.stackify.models.User;
|
||||||
|
import com.stackify.utils.ConnectionUtil;
|
||||||
|
|
||||||
|
public class UserDAO {
|
||||||
|
|
||||||
|
private Logger logger = LogManager.getLogger(UserDAO.class);
|
||||||
|
|
||||||
|
public void createTable() {
|
||||||
|
try (Connection con = ConnectionUtil.getConnection()) {
|
||||||
|
String createQuery = "CREATE TABLE IF NOT EXISTS users(email varchar(50) primary key, name varchar(50))";
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(createQuery);
|
||||||
|
|
||||||
|
pstmt.execute();
|
||||||
|
} catch (SQLException exc) {
|
||||||
|
logger.error(exc.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(User user) {
|
||||||
|
try (Connection con = ConnectionUtil.getConnection()) {
|
||||||
|
|
||||||
|
String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)";
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(insertQuery);
|
||||||
|
pstmt.setString(1, user.getEmail());
|
||||||
|
pstmt.setString(2, user.getName());
|
||||||
|
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
} catch (SQLException exc) {
|
||||||
|
logger.error(exc.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> findAll() {
|
||||||
|
List<User> users = new ArrayList<>();
|
||||||
|
|
||||||
|
try (Connection con = ConnectionUtil.getConnection()) {
|
||||||
|
String query = "SELECT * FROM users";
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(query);
|
||||||
|
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
while (rs.next()) {
|
||||||
|
User user = new User();
|
||||||
|
user.setEmail(rs.getString("email"));
|
||||||
|
user.setName(rs.getString("name"));
|
||||||
|
users.add(user);
|
||||||
|
}
|
||||||
|
} catch (SQLException exc) {
|
||||||
|
logger.error(exc.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
guest/tomcat-app/src/main/java/com/stackify/models/User.java
Normal file
43
guest/tomcat-app/src/main/java/com/stackify/models/User.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package com.stackify.models;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.Link;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private String email;
|
||||||
|
private String name;
|
||||||
|
private Link link;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(String email, String name) {
|
||||||
|
super();
|
||||||
|
this.email = email;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Link getLink() {
|
||||||
|
return link;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLink(Link link) {
|
||||||
|
this.link = link;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.stackify.services;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.ws.rs.container.ContainerRequestContext;
|
||||||
|
import javax.ws.rs.container.ContainerResponseContext;
|
||||||
|
import javax.ws.rs.container.ContainerResponseFilter;
|
||||||
|
import javax.ws.rs.ext.Provider;
|
||||||
|
|
||||||
|
@Provider
|
||||||
|
public class CorsFilter implements ContainerResponseFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void filter(final ContainerRequestContext requestContext,
|
||||||
|
final ContainerResponseContext response) throws IOException {
|
||||||
|
response.getHeaders().add("Access-Control-Allow-Origin", "*");
|
||||||
|
response.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.stackify.services;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
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.stackify.daos.UserDAO;
|
||||||
|
import com.stackify.models.User;
|
||||||
|
|
||||||
|
@Path("/users")
|
||||||
|
public class UserService {
|
||||||
|
private UserDAO userDao = new UserDAO();
|
||||||
|
|
||||||
|
public UserService (){
|
||||||
|
userDao.createTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
|
public Response addUser(User user) {
|
||||||
|
userDao.add(user);
|
||||||
|
return Response.ok()
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
public List<User> getUsers() {
|
||||||
|
return userDao.findAll();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.stackify.utils;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.naming.Context;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
public class ConnectionUtil {
|
||||||
|
|
||||||
|
private static Logger logger = LogManager.getLogger(ConnectionUtil.class);
|
||||||
|
|
||||||
|
public static Connection getConnection() {
|
||||||
|
try {
|
||||||
|
String jndiName = "java:/comp/env/jdbc/MyDataSource";
|
||||||
|
|
||||||
|
Context initialContext = new InitialContext();
|
||||||
|
DataSource datasource = (DataSource)initialContext.lookup(jndiName);
|
||||||
|
if (datasource != null) {
|
||||||
|
return datasource.getConnection();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
logger.error("Failed to lookup datasource.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (NamingException | SQLException exc) {
|
||||||
|
logger.error(exc.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user