tomcat app ex (#2625)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user