[JAVA-13854] Half list moved (#12598)
* [JAVA-13854] added parent module * [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent) * [JAVA-13854] moved bootique(submodule) to web-modules(parent) * [JAVA-13854] moved dropwizard(submodule) to web-modules(parent) * [JAVA-13854] moved blade(submodule) to web-modules(parent) * [JAVA-13854] moved java-lite(submodule) to web-modules(parent) * [JAVA-13854] moved jooby(submodule) to web-modules(parent) * [JAVA-13854] moved linkrest(submodule) to web-modules(parent) * [JAVA-13854] moved ninja(submodule) to web-modules(parent) * [JAVA-13854] moved ratpack(submodule) to web-modules(parent) * [JAVA-13854] moved resteasy(submodule) to web-modules(parent) * [JAVA-13854] moved restx(submodule) to web-modules(parent) * [JAVA-13854] moved spark-java(submodule) to web-modules(parent) * [JAVA-13854] moved vraptor(submodule) to web-modules(parent) * [JAVA-13854] delete modules that were moved * [JAVA-13854] * [JAVA-13854] * [JAVA-13854] delete ninja submodule + moved raml(submodule) to web-modules(parent) * [JAVA-13854] moved gwt(submodule) to web-modules(parent) * [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent) * [JAVA-13854] moved jee-7(submodule) to web-modules(parent) * [JAVA-13854] moved play-framework(not a module) to web-modules * [JAVA-13854] fix failing test * [JAVA-13854] moved struts-2(submodule) to web-modules(parent) * [JAVA-13854] moved wicket(submodule) to web-modules(parent) * [JAVA-13854] deleted modules that were moved to web-modules * JAVA-13854 Removed moved modules from the main pom.xml Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.models.User;
|
||||
|
||||
import javax.enterprise.context.SessionScoped;
|
||||
import java.io.Serializable;
|
||||
|
||||
@SessionScoped
|
||||
public class UserInfo implements Serializable {
|
||||
|
||||
private User user;
|
||||
|
||||
|
||||
public UserInfo() {}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import br.com.caelum.vraptor.*;
|
||||
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
||||
import br.com.caelum.vraptor.validator.Validator;
|
||||
import com.baeldung.config.UserInfo;
|
||||
import com.baeldung.daos.UserDao;
|
||||
import com.baeldung.models.User;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Controller
|
||||
public class AuthController {
|
||||
|
||||
private Validator validator;
|
||||
private UserDao userDao;
|
||||
private Result result;
|
||||
private UserInfo userInfo;
|
||||
private Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
public AuthController() {
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public AuthController(Validator validator, UserDao userDao, Result result, UserInfo userInfo) {
|
||||
this.validator = validator;
|
||||
this.userDao = userDao;
|
||||
this.result = result;
|
||||
this.userInfo = userInfo;
|
||||
}
|
||||
|
||||
@Get("/register")
|
||||
public void registrationForm() {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/register");
|
||||
}
|
||||
|
||||
@Post("/register")
|
||||
public void register(User user, HttpServletRequest request) {
|
||||
|
||||
validator.validate(user);
|
||||
|
||||
if(validator.hasErrors()) {
|
||||
result.include("errors", validator.getErrors());
|
||||
}
|
||||
|
||||
validator.onErrorRedirectTo(this).registrationForm();
|
||||
|
||||
if(!user.getPassword()
|
||||
.equals(request.getParameter("password_confirmation"))) {
|
||||
result.include("error", "Passwords Do Not Match");
|
||||
result.redirectTo(this).registrationForm();
|
||||
}
|
||||
|
||||
user.setPassword(
|
||||
BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));
|
||||
|
||||
Object resp = userDao.add(user);
|
||||
|
||||
if(resp != null) {
|
||||
result.include("status", "Registration Successful! Now Login");
|
||||
result.redirectTo(this).loginForm();
|
||||
} else {
|
||||
result.include("error", "There was an error during registration");
|
||||
result.redirectTo(this).registrationForm();
|
||||
}
|
||||
}
|
||||
|
||||
@Get("/login")
|
||||
public void loginForm() {
|
||||
result.use(FreemarkerView.class).withTemplate("auth/login");
|
||||
}
|
||||
|
||||
@Post("/login")
|
||||
public void login(HttpServletRequest request) {
|
||||
|
||||
String password = request.getParameter("user.password");
|
||||
String email = request.getParameter("user.email");
|
||||
|
||||
if(email.isEmpty() || password.isEmpty()) {
|
||||
result.include("error", "Email/Password is Required!");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
}
|
||||
|
||||
User user = userDao.findByEmail(email);
|
||||
if(user != null && BCrypt.checkpw(password, user.getPassword())) {
|
||||
userInfo.setUser(user);
|
||||
result.include("status", "Login Successful!");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include("error", "Email/Password Does Not Match!");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import br.com.caelum.vraptor.Controller;
|
||||
import br.com.caelum.vraptor.Path;
|
||||
import br.com.caelum.vraptor.Result;
|
||||
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
||||
import com.baeldung.daos.PostDao;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
private PostDao postDao;
|
||||
private final Result result;
|
||||
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
|
||||
|
||||
public IndexController() {
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public IndexController(Result result, PostDao postDao) {
|
||||
this.result = result;
|
||||
this.postDao = postDao;
|
||||
}
|
||||
|
||||
@Path("/")
|
||||
public void index() {
|
||||
result.include("posts", postDao.all());
|
||||
result.use(FreemarkerView.class).withTemplate("index");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import br.com.caelum.vraptor.Controller;
|
||||
import br.com.caelum.vraptor.Get;
|
||||
import br.com.caelum.vraptor.Path;
|
||||
import br.com.caelum.vraptor.Result;
|
||||
import br.com.caelum.vraptor.freemarker.FreemarkerView;
|
||||
import br.com.caelum.vraptor.validator.Validator;
|
||||
import com.baeldung.config.UserInfo;
|
||||
import com.baeldung.daos.PostDao;
|
||||
import com.baeldung.models.Post;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Controller
|
||||
public class PostController {
|
||||
|
||||
private Result result;
|
||||
private Logger logger = Logger.getLogger(PostController.class.getName());
|
||||
private PostDao postDao;
|
||||
private UserInfo userInfo;
|
||||
private Validator validator;
|
||||
|
||||
public PostController() {}
|
||||
|
||||
@Inject
|
||||
public PostController(Result result, PostDao postDao, UserInfo userInfo, Validator validator) {
|
||||
this.result = result;
|
||||
this.postDao = postDao;
|
||||
this.userInfo = userInfo;
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
@Get("/post/add")
|
||||
public void addForm() {
|
||||
|
||||
if(Objects.isNull(userInfo.getUser())) {
|
||||
result.include("error", "Please Login to Proceed");
|
||||
result.redirectTo(AuthController.class).loginForm();
|
||||
return;
|
||||
}
|
||||
|
||||
result.use(FreemarkerView.class).withTemplate("posts/add");
|
||||
}
|
||||
|
||||
@br.com.caelum.vraptor.Post("/post/add")
|
||||
public void add(Post post) {
|
||||
post.setAuthor(userInfo.getUser());
|
||||
validator.validate(post);
|
||||
if(validator.hasErrors())
|
||||
result.include("errors", validator.getErrors());
|
||||
validator.onErrorRedirectTo(this).addForm();
|
||||
|
||||
Object id = postDao.add(post);
|
||||
|
||||
if(Objects.nonNull(id)) {
|
||||
result.include("status", "Post Added Successfully");
|
||||
result.redirectTo(IndexController.class).index();
|
||||
} else {
|
||||
result.include(
|
||||
"error", "There was an error creating the post. Try Again");
|
||||
result.redirectTo(this).addForm();
|
||||
}
|
||||
}
|
||||
|
||||
@Get("/posts/{id}")
|
||||
public void view(int id) {
|
||||
result.include("post", postDao.findById(id));
|
||||
result.use(FreemarkerView.class).withTemplate("view");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.daos;
|
||||
|
||||
import com.baeldung.controllers.PostController;
|
||||
import com.baeldung.models.Post;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@RequestScoped
|
||||
public class PostDao {
|
||||
|
||||
private Logger logger = Logger.getLogger(PostController.class.getName());
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
public PostDao() {}
|
||||
|
||||
@Inject
|
||||
public PostDao(SessionFactory sessionFactory) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
public Object add(Post post) {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Object id = session.save(post);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return id;
|
||||
}
|
||||
|
||||
public Post findById(int id) {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Post post = (Post) session.get(Post.class, id);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return post;
|
||||
}
|
||||
|
||||
|
||||
public List<Post> all() {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
List<Post> posts = (List<Post>) session.createQuery("FROM Post p").list();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return posts;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.daos;
|
||||
|
||||
import com.baeldung.models.User;
|
||||
import org.hibernate.Criteria;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.criterion.Criterion;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.omg.PortableInterceptor.LOCATION_FORWARD;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@RequestScoped
|
||||
public class UserDao {
|
||||
|
||||
SessionFactory sessionFactory;
|
||||
|
||||
public UserDao() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public UserDao(SessionFactory sessionFactory) {
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
|
||||
public Object add(User user) {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Object Id = session.save(user);
|
||||
session.getTransaction().commit();
|
||||
return Id;
|
||||
}
|
||||
|
||||
public User findByEmail(String email) {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Criteria criteria = session.createCriteria(User.class);
|
||||
criteria.add(Restrictions.eq("email", email));
|
||||
criteria.setMaxResults(1);
|
||||
User u = (User) criteria.uniqueResult();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return u;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.baeldung.models;
|
||||
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Entity
|
||||
@Table(name = "posts")
|
||||
public class Post {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@NotNull @Size(min = 10)
|
||||
@Column(columnDefinition = "longtext")
|
||||
private String post;
|
||||
|
||||
@NotNull @Size(min = 5, max = 100)
|
||||
@Column(unique = true)
|
||||
private String title;
|
||||
|
||||
@NotNull
|
||||
@ManyToOne(targetEntity = User.class, optional = false)
|
||||
private User author;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private Date createdAt;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private Date updatedAt;
|
||||
|
||||
@Transient
|
||||
private Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
public Post() {
|
||||
createdAt = new Date();
|
||||
updatedAt = new Date();
|
||||
}
|
||||
|
||||
public Post(String title, String post, User author) {
|
||||
this.title = title;
|
||||
this.post = post;
|
||||
this.author = author;
|
||||
createdAt = new Date();
|
||||
updatedAt = new Date();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getPost() {
|
||||
return post;
|
||||
}
|
||||
|
||||
public void setPost(String post) {
|
||||
this.post = post;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public User getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(User author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(Date createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public Date getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
public void setUpdatedAt(Date updatedAt) {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "title: " + this.title
|
||||
+ "\npost: " + this.post
|
||||
+ "\nauthor: " + this.author
|
||||
+"\ncreatetdAt: " + this.createdAt
|
||||
+ "\nupdatedAt: " + this.updatedAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.models;
|
||||
|
||||
import org.hibernate.validator.constraints.Email;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@NotNull
|
||||
@Size(min=3, max=255)
|
||||
private String name;
|
||||
|
||||
@Column(unique = true)
|
||||
@NotNull @Email
|
||||
private String email;
|
||||
|
||||
@NotNull
|
||||
@Size(min=6, max=255)
|
||||
private String password;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private Date createdAt;
|
||||
|
||||
@Column(name = "updated_at")
|
||||
private Date updatedAt;
|
||||
|
||||
public User() {
|
||||
updatedAt = new Date();
|
||||
createdAt = new Date();
|
||||
}
|
||||
|
||||
public User(String name, String email, String password) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
createdAt = new Date();
|
||||
updatedAt = new Date();
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(Date createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public Date getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
public void setUpdatedAt(Date updatedAt) {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Name: " + this.name
|
||||
+ "\nEmail: " + this.email;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user