diff --git a/.gitignore b/.gitignore index 033f8f9..6db5371 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ # Java *.war *.jar -*.class \ No newline at end of file +*.class +Shevtsov/src/main/webapp/res/themes/manuscript/index.html \ No newline at end of file diff --git a/Shevtsov/.gitignore b/Shevtsov/.gitignore new file mode 100644 index 0000000..64325e2 --- /dev/null +++ b/Shevtsov/.gitignore @@ -0,0 +1,2 @@ +*/taget/ +*/classes/ diff --git a/Shevtsov/BlogSources/pom.xml b/Shevtsov/BlogSources/pom.xml new file mode 100644 index 0000000..1e982c4 --- /dev/null +++ b/Shevtsov/BlogSources/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + com.example + blogsources + 0.0.1 + war + + demo + web blog demo + + + UTF-8 + 1.7 + 4.1.6.RELEASE + + + + com.example.blogsources + URLMapping + 0.0.1 + jar + + + org.apache.tomcat.embed + tomcat-embed-jasper + 8.0.23 + provided + + + javax.servlet + jstl + 1.2 + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-web + ${spring.version} + + + + org.springframework + spring-webmvc + ${spring.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${java.version} + ${java.version} + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + http://localhost:8080/manager/text + TomcatServer + /demo + + + + + diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java new file mode 100644 index 0000000..078c1f4 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java @@ -0,0 +1,115 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; +import java.util.Date; + +public abstract class AbstractPost implements Post { + private int id; + private int userId; + private String title; + private Date creationDate; + private Date updateDate; + protected String type = null; + protected String postContent = null; + + public AbstractPost() { + } + + public AbstractPost(int id, String title, int userId) { + this.id = id; + this.userId = userId; + this.title = title; + creationDate = new Date(); + updateDate = creationDate; + setPostType(); + } + + @Override + public int getPostId() { + return id; + } + + @Override + public Date getPostCreationDate() { + return creationDate; + } + + @Override + public Date getPostLastUpdateDate() { + return updateDate; + } + + public String getPostTitle() { + return title; + } + + @Override + public void setPostLastUpdateDate(Date lastUpdateDate) { + updateDate = lastUpdateDate; + } + + @Override + public String getPostType() { + return type; + } + + protected abstract void setPostType(); + protected void setPostContant(String postContant) { + this.postContent = postContant; + } + + @Override + public abstract void displayPost(PrintStream out); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getPostContent() { + return postContent; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java new file mode 100644 index 0000000..7a3b145 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Post.Exceptions; + +public class IllegalPageTypeException extends PageExeptions { + + public IllegalPageTypeException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java new file mode 100644 index 0000000..68e8d4f --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Post.Exceptions; + +public class NotExistsPageException extends PageExeptions { + + public NotExistsPageException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java new file mode 100644 index 0000000..f8d287d --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java @@ -0,0 +1,5 @@ +package com.example.blogsources.Post.Exceptions; + +public class PageExeptions extends RuntimeException{ + PageExeptions (String s) {super(s);} +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java new file mode 100644 index 0000000..90aaeae --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java @@ -0,0 +1,19 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class ImagePost extends AbstractPost { + + public ImagePost(int id, String content, int userId) { + super(id,content,userId); + } + + @Override + protected void setPostType() { + type = "image"; + } + + @Override + public void displayPost(PrintStream out) {out.println("There is I have to show a image - " + super.getPostTitle()); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java new file mode 100644 index 0000000..6b307c6 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java @@ -0,0 +1,15 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; +import java.util.Date; + +public interface Post { + int getPostId(); + String getPostTitle(); + String getPostContent(); + String getPostType(); + void displayPost(PrintStream out); + Date getPostCreationDate(); + Date getPostLastUpdateDate(); + void setPostLastUpdateDate(Date lastUpdateDate); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java new file mode 100644 index 0000000..5ccb8c3 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java @@ -0,0 +1,29 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class RegularPost extends AbstractPost { + + public RegularPost(int id, String content, int userId) { + super(id,content,userId); + } + + public RegularPost() { + super(); + setPostType(); + } + + @Override + protected void setPostType() { + type = "regular"; + } + + @Override + public void displayPost(PrintStream out) { + System.out.println("The Regular page was created" + super.getPostCreationDate() + "Content:S"); + out.println(super.getPostTitle() + "\n" ); + } + public void setPostContent (String content){ + postContent = content; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java new file mode 100644 index 0000000..6750592 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java @@ -0,0 +1,21 @@ +package com.example.blogsources.Post; + +public class URLMap { + public static final String PROTO = "http://"; + public static final String HOST = "localhost"; + public static final String PORT = ":8080/"; + public static final String CONTEXT = "demo"; + public static final String FULL_URL = PROTO + HOST + PORT + CONTEXT; + public static final String LOGIN = "/login"; + public static final String FULL_LOGIN = FULL_URL + LOGIN; + public static final String LOGOUT = "/logout"; + public static final String FULL_LOGOUT = FULL_URL + LOGOUT; + public static final String REGISTER = "/register"; + public static final String FULL_REGISTER = FULL_URL + REGISTER; + public static final String ADDPOST = "/addPost"; + public static final String FULL_ADDPOST = FULL_URL + ADDPOST; + public static final String WELCOME = "/welcome"; + public static final String FULL_WELCOME = FULL_URL + WELCOME; + public static final String ABOUT = "/about"; + public static final String FULL_ABOUT = FULL_URL + ABOUT; +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java new file mode 100644 index 0000000..c1b5438 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java @@ -0,0 +1,20 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class URLPost extends AbstractPost { + + public URLPost(int id, String content, int userId) { + super(id,content,userId); + } + + @Override + protected void setPostType() { + type = "url"; + } + + @Override + public void displayPost(PrintStream out) { + out.println("There is I have to get and show data from the extended URL - " + super.getPostTitle()); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java new file mode 100644 index 0000000..9633e15 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java @@ -0,0 +1,82 @@ +package com.example.blogsources.Users; + +import com.example.blogsources.utils.Encyption; + +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; + +public class BasicUser implements User , Serializable { + private int userId; + private String userName; + private String password; + private String email; + private int userPrivilegeLevel; + // 0 - root privilege, + // 1 - user that add comments + // 2+ - like a guest + // 100 - a guest + + public BasicUser() { + this.userId = -1; + this.userName = ""; + this.userPrivilegeLevel = 2; + } + + public BasicUser(int id, String userName,int userPrivilegeLevel){ + this.userId = id; + this.userName = userName; + this.userPrivilegeLevel = userPrivilegeLevel; + } + + public BasicUser(String userName,int userPrivilegeLevel){ + this.userId = -1; + this.userName = userName; + this.userPrivilegeLevel = userPrivilegeLevel; + } + @Override + public int getUserId() { + return userId; + } + + @Override + public String getUserName() { + return userName; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + @Override + public void setUserName(String userName) { + this.userName = userName; + } + + public void setUserPrivilegeLevel(int userPrivilegeLevel) { + this.userPrivilegeLevel = userPrivilegeLevel; + } + + @Override + public int getUserPrivilegeLevel() { + return userPrivilegeLevel; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException { + this.password = Encyption.ecrypt(password); + } + + @Override + public String getEmail() { + return email; + } + + @Override + public void setEmail(String email) { + this.email = email; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java new file mode 100644 index 0000000..f7d795e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class IllegalPrivilegeLevelException extends UserExeptions { + + public IllegalPrivilegeLevelException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java new file mode 100644 index 0000000..8a8c306 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class NotExistUserException extends UserExeptions { + + public NotExistUserException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java new file mode 100644 index 0000000..34028ec --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class UserAlreadyExistsException extends UserExeptions { + + public UserAlreadyExistsException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java new file mode 100644 index 0000000..c4f83b4 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java @@ -0,0 +1,5 @@ +package com.example.blogsources.Users.Exceptions; + +public class UserExeptions extends RuntimeException{ + UserExeptions(String s) {super(s);} +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java new file mode 100644 index 0000000..00e2d6f --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java @@ -0,0 +1,20 @@ +package com.example.blogsources.Users; + +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; + +public interface User { + int getUserId(); + void setUserId(int userId); + String getUserName(); + void setUserName(String userName); + String getPassword(); + void setPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException; + String getEmail(); + void setEmail(String email); + // 0 - root privilege, + // 1 - user that add comments + // 3 - a guest + int getUserPrivilegeLevel(); + void setUserPrivilegeLevel(int userPrivilegeLevel); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java new file mode 100644 index 0000000..824ab4e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java @@ -0,0 +1,16 @@ +package com.example.blogsources.Users; + +import java.io.PrintStream; + +public interface UserStorage { + public void addNewUser (User user); + public void removeUser (int id); + public void removeUser (String name); + public void showAllUser (PrintStream output); + public int getUserId(String name); + public String getUserName(int id); + public int getUserPrivilegeLevel(String name); + public boolean userIdExists(int id); + public boolean userNameExists(String name); + public boolean validateUser(User user); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java new file mode 100644 index 0000000..a772002 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java @@ -0,0 +1,106 @@ +package com.example.blogsources.Users; + +import com.example.blogsources.Users.Exceptions.IllegalPrivilegeLevelException; +import com.example.blogsources.Users.Exceptions.NotExistUserException; +import com.example.blogsources.Users.Exceptions.UserAlreadyExistsException; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; + +public class UsersInRam implements UserStorage { + private Map users = new HashMap<>(); + private int lastUserId=0; + + public void addNewUser (User user) { + if (userNameExists(user.getUserName())){ + throw new UserAlreadyExistsException("The user name already existed: " + user.getUserName()); + } + + if (user.getUserPrivilegeLevel() >=0 && user.getUserPrivilegeLevel() <= 3) { + user.setUserId(lastUserId + 1); + users.put(user.getUserName(), user); + lastUserId++; + }else { + throw new IllegalPrivilegeLevelException("Invalid userPrivilegeLevel: " + user.getUserPrivilegeLevel() + "Expected values 0...3"); + } + } + + @Override + public void removeUser(int id) { + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + removeUser(entry.getKey()); + break; + } + } + throw new NotExistUserException("User with id: " + id + " wasn't found"); + } + + @Override + public void removeUser(String name) { + users.remove(name); + } + + @Override + public void showAllUser(PrintStream output) { + output.println("#Id #UserName #UserPrivilegeLevel"); + for (String key: users.keySet()) { + output.println(users.get(key).getUserId() + " " + users.get(key).getUserName() + " " + users.get(key).getUserPrivilegeLevel()); + } + } + + + @Override + public int getUserId(String name) { + return users.get(name).getUserId(); + } + + public User getUserByName(String name) { + return users.get(name); + } + + @Override + public String getUserName(int id) { + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + return entry.getValue().getUserName(); + } + } + throw new NotExistUserException("User with id: " + id + " wasn't found"); + } + + public int getUserPrivilegeLevel(String name) { + return users.get(name).getUserPrivilegeLevel(); + } + + @Override + public boolean userIdExists(int id) { + boolean ret = false; + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + ret = true; + break; + } + } + return ret; + } + + @Override + public boolean userNameExists(String name) { + return (users.containsKey(name)); + } + + @Override + public boolean validateUser(User user) { + if (userNameExists(user.getUserName()) ){ + User userInStorage = getUserByName(user.getUserName()); + return userInStorage.getPassword().equals(user.getPassword()); + }else{ + return false; + } + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java new file mode 100644 index 0000000..096f6d3 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java @@ -0,0 +1,221 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.blogsources.controllers; + +import java.util.List; + +import com.example.blogsources.Post.Post; +import com.example.blogsources.Post.RegularPost; +import com.example.blogsources.Users.BasicUser; +import com.example.blogsources.Users.User; +import com.example.blogsources.Users.UsersInRam; +import com.example.blogsources.services.PostStorage; +import com.example.blogsources.utils.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import static com.example.blogsources.URLMap.URLMap.*; + +// TODO: Add log4j +// TODO: Add DB +// TODO: Add Spring Security +// TODO: Add REST API +// TODO: Add i18n + +@Controller +@RequestMapping("/") +public class WebController { + + @Autowired + @Qualifier("postStorage") + PostStorage postStorage; + + @Autowired + @Qualifier("users") + UsersInRam users; + + @PostConstruct + public void initIt() throws Exception { + User user = new BasicUser("admin", 0); + if (!users.userNameExists(user.getUserName())){ + user.setPassword("1"); + user.setEmail("admin@example.com"); + users.addNewUser(user); + System.out.println("Add user admin"); + } + + User guest = new BasicUser("guest", 3); + if (!users.userNameExists(guest.getUserName())){ + guest.setPassword("1"); + guest.setEmail("guest@example.com"); + users.addNewUser(guest); + System.out.println("Add user guest"); + } + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String hello(){ + return "welcome"; + } + + @RequestMapping(value = ABOUT, method = RequestMethod.GET) + public String about(){ + return "about"; + } + + @RequestMapping(value = WELCOME, method = RequestMethod.GET) + public ModelAndView welcome(){ + List posts = postStorage.displayAllPosts(); + return new ModelAndView("webBlog", "posts", posts); + } + + @RequestMapping(value = ADDPOST, method = RequestMethod.GET) + public ModelAndView addPost(HttpServletRequest request){ + ModelAndView modelAndView = new ModelAndView(); + User user = (User) request.getSession().getAttribute("currentUser"); + if (user == null || StringUtils.isEmpty(user.getUserName())){ + modelAndView.addObject("error_message", "Please log in"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (user.getUserPrivilegeLevel() != 0){ + modelAndView.addObject("error_message", "You don't have enough permissions to add a new post"); + modelAndView.setViewName("error"); + return modelAndView; + } + return new ModelAndView("addPost", "command", new RegularPost()); + } + + @RequestMapping(value = ADDPOST, method = RequestMethod.POST) + public ModelAndView addPost(@ModelAttribute("post") RegularPost post){ + ModelAndView modelAndView = new ModelAndView(); + if (post == null){ + modelAndView.addObject("error_message", "The object post is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } +// System.out.println("This is addPost in WebController. Title - " + post.getPostTitle() + " Content: " + post.getPostContent()); + postStorage.addNewPost(post); + List posts = postStorage.displayAllPosts(); + return new ModelAndView("webBlog", "posts", posts); + } + + + @RequestMapping(value = REGISTER, method = RequestMethod.GET) + public ModelAndView register(){ + return new ModelAndView("register", "command", new BasicUser()); + } + + @RequestMapping(value = REGISTER, method = RequestMethod.POST) + public ModelAndView register(@ModelAttribute("user") BasicUser user){ + ModelAndView modelAndView = new ModelAndView(); + if (user == null){ + modelAndView.addObject("error_message", "The object user is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getEmail()) ) { + modelAndView.addObject("error_message", "The user or email are incorrect !!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (users.getUserByName(user.getUserName()) !=null) { + modelAndView.addObject("error_message", "The user '" + user.getUserName()+ "' already exists!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + users.addNewUser(user); + if (users.getUserByName(user.getUserName()) ==null) { + modelAndView.addObject("error_message", "The user '" + user.getUserName()+ "' was not registered!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } +// modelAndView.addObject("message", "Id: " + users.getUserId(user.getUserName()) + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + modelAndView.addObject("message", "The new user '" + user.getUserName()+ "' was successfully registered!!!"); + modelAndView.setViewName("message"); + return modelAndView; + } + @RequestMapping(value = "/posts", method = RequestMethod.GET) + public ModelAndView getPosts(){ + List posts = postStorage.displayAllPosts(); + Post post = posts.get(posts.size() - 1); + System.out.println("The amount of posts: " + posts.size()); + System.out.println("The last one. Title - " + post.getPostTitle() + "Content: " + post.getPostContent()); + return new ModelAndView("webBlog", "posts", posts); + } + +// @RequestMapping(value = "/post", method = RequestMethod.GET) +// public ModelAndView getPost(@RequestParam("postTitle") String postTitle){ +// PostFromDemo post = postStorage.getPostByTitle(postTitle); +// return new ModelAndView("post", "post", post); +// } + + + @RequestMapping(value = LOGIN, method = RequestMethod.GET) + public ModelAndView login(){ + return new ModelAndView("login", "command", new BasicUser()); + } + + + @RequestMapping(value = LOGIN, method = {RequestMethod.POST}) + public ModelAndView login(@ModelAttribute("user") BasicUser user, HttpServletRequest request){ + ModelAndView modelAndView = new ModelAndView(); + if (user == null){ + modelAndView.addObject("error_message", "The object user is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (users.validateUser(user)){ + modelAndView.addObject("message", "Log in successful."); +// modelAndView.addObject("message", "Log in successful. Id: " + users.getUserId(user.getUserName()) + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + HttpSession ses = request.getSession(); + ses.setAttribute("currentUser", users.getUserByName(user.getUserName())); + modelAndView.setViewName("message"); + return modelAndView; + }else{ +// modelAndView.addObject("error_message", "Login failed: " + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + modelAndView.addObject("error_message", "Login failed. Please enter correct user and password."); + modelAndView.setViewName("error"); + return modelAndView; + } + } + + @RequestMapping(value = LOGOUT, method = RequestMethod.GET) + public ModelAndView logout(HttpServletRequest request, HttpServletResponse response){ + { + request.getSession().removeAttribute("currentUser"); + request.getSession().invalidate(); + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Expires", "-1"); + ModelAndView modelAndView = new ModelAndView(); + modelAndView.addObject("message", "Log Out successful"); + modelAndView.setViewName("message"); + return modelAndView; + } + } + + +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java new file mode 100644 index 0000000..f011639 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java @@ -0,0 +1,13 @@ +package com.example.blogsources.services; + +import com.example.blogsources.Post.Post; + +import java.util.List; + +public interface PostStorage { + void addNewPost(String type, String content, int userId); + void removePost(int id); +// void displayPost(int id, PrintStream out); + List displayAllPosts(); + void addNewPost(Post post); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java new file mode 100644 index 0000000..7477398 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java @@ -0,0 +1,58 @@ +package com.example.blogsources.services; + +import com.example.blogsources.Post.Exceptions.NotExistsPageException; +import com.example.blogsources.Post.Post; +import com.example.blogsources.Post.RegularPost; +import org.springframework.stereotype.Service; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +@Service +public class PostsInRam implements PostStorage { + private List posts = new ArrayList<>(); + + public void addNewPost(String type, String content, int userId) { + posts.add(new RegularPost(posts.size(), content, userId)); + +// switch (type){ +// case "regular": //System.out.println("Add a new regular page #" + posts.size() + " and userId = " + userId+ "\n" + "Content:" + content+"\n"); +// posts.add(new RegularPost(posts.size(), content, userId)); +// break; +// case "url": posts.add( new URLPost(posts.size(), content, userId)); +// break; +// case "image": posts.add( new ImagePost(posts.size(), content, userId)); +// break; +// default: +// throw new IllegalPageTypeException("Invalid type of the page: " + type + "Expected values regular/url/image"); +// } + } + + + public void removePost(int id) { + if (id < 0 || id > posts.size()) { + throw new NotExistsPageException("Invalid id of the page: " + id + "\nmin value: 0;\n" + "Max Value: " + posts.size() ); + } else { + posts.remove(id); + } + } + + public void displayPost(int id, PrintStream out) { +// out.println("try to get page #"+id + " " + posts.get(id).getPostType() + " " + posts.get(id).getPostCreationDate() + "\n"); + if (id < 0 || id > posts.size()) { + throw new NotExistsPageException("Invalid id of the page: " + id + "\nmin value: 0;\n" + "Max Value: " + posts.size() ); + } else { + posts.get(id).displayPost(out); + } + } + + @Override + public List displayAllPosts() { + return posts; + } + + public void addNewPost(Post post) { + posts.add(post); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java new file mode 100644 index 0000000..ed0b80e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java @@ -0,0 +1,19 @@ +package com.example.blogsources.utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Encyption { + + public static String ecrypt(String plainTest) throws NoSuchAlgorithmException, UnsupportedEncodingException { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(plainTest.getBytes("UTF-8")); + byte[] byteData = md.digest(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < byteData.length; i++) { + sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); + } + return sb.toString(); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java new file mode 100644 index 0000000..fc84f4e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java @@ -0,0 +1,7 @@ +package com.example.blogsources.utils; + +public class StringUtils { + public static boolean isEmpty(String string){ + return (string == null || string.isEmpty()) ? true : false; + } +} diff --git a/Shevtsov/BlogSources/src/main/resources/application.properties b/Shevtsov/BlogSources/src/main/resources/application.properties new file mode 100644 index 0000000..7179b58 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.view.prefix: /WEB-INF/jsp/ +spring.view.suffix: .jsp +application.message: Hello Phil +server.servlet-path=/ +server.port=8080 \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp new file mode 100644 index 0000000..9974b09 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp @@ -0,0 +1,21 @@ +<%@include file="parts/begin.jsp" %> + + + <%@include file="parts/header.jsp" %> + + + + + About this blog + This is a template for a blog which uses Spring MVC + + + + <%@include file="parts/leftmenu.jsp" %> + + <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> + + + +