Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jonelrelucio committed May 8, 2023
0 parents commit f031d9e
Show file tree
Hide file tree
Showing 28 changed files with 505 additions and 0 deletions.
25 changes: 25 additions & 0 deletions TIW-HTML-project-taxonomy/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-19">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v9.0">
<attributes>
<attribute name="owner.project.facets" value="jst.web"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/mysql-connector-j-8.0.32.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/thymeleaf-3.0.14.RELEASE.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/slf4j-api-2.0.7.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/slf4j-nop-2.0.7.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/javassist-3.29.0-GA.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/ognl-3.3.4.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/attoparser-2.0.5.RELEASE.jar"/>
<classpathentry kind="lib" path="src/main/webapp/WEB-INF/lib/unbescape-1.1.6.RELEASE.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
1 change: 1 addition & 0 deletions TIW-HTML-project-taxonomy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build/
31 changes: 31 additions & 0 deletions TIW-HTML-project-taxonomy/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TIW-HTML-project-taxonomy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions TIW-HTML-project-taxonomy/.settings/.jsdtscope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**/node_modules/*|**/*.min.js|**/bower_components/*" kind="src" path="src/main/webapp"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
10 changes: 10 additions & 0 deletions TIW-HTML-project-taxonomy/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=19
org.eclipse.jdt.core.compiler.compliance=19
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=19
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="TIW-HTML-project-taxonomy">
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<property name="context-root" value="TIW-HTML-project-taxonomy"/>
<property name="java-output-path" value="/TIW-HTML-project-taxonomy/build/classes"/>
</wb-module>
</project-modules>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v9.0"/>
<fixed facet="java"/>
<fixed facet="jst.web"/>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="19"/>
<installed facet="jst.web" version="4.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package it.polimi.tiw.taxonomy.beans;

public class User {


private int id;
private String username;
private String name;
private String surname;

public int getId() { return id; }
public String getUsername() { return username; }
public String getName() { return name; }
public String getSurname() { return surname; }

public void setId(int id) { this.id = id; }
public void setUsername(String username) { this.username = username; }
public void setName(String name) { this.name = name; }
public void setSurname(String surname) { this.surname = surname; }



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package it.polimi.tiw.taxonomy.controllers;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import it.polimi.tiw.taxonomy.beans.User;
import it.polimi.tiw.taxonomy.utils.ConnectionHandler;
import it.polimi.tiw.taxonomy.utils.TemplateEngineManager;

@WebServlet("/home")
public class Home extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection connection = null;
private TemplateEngine templateEngine;

public Home() {
super();
}

public void init() throws ServletException {
ServletContext context = getServletContext();
this.connection = ConnectionHandler.getConnection(context);
this.templateEngine = TemplateEngineManager.getTemplateEngine(context);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String loginPath = getServletContext().getContextPath() + "/index.html";
HttpSession session = request.getSession();

if(session.isNew() || session.getAttribute("user") == null ) {
response.sendRedirect(loginPath);
return;
}

User user = (User) session.getAttribute("user");
System.out.println(user.getUsername());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package it.polimi.tiw.taxonomy.controllers;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import it.polimi.tiw.taxonomy.beans.User;
import it.polimi.tiw.taxonomy.dao.UserDAO;
import it.polimi.tiw.taxonomy.utils.ConnectionHandler;
import it.polimi.tiw.taxonomy.utils.TemplateEngineManager;

@WebServlet("/login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection connection = null;
private TemplateEngine templateEngine;

public Login() {
super();
}

public void init() throws ServletException {
ServletContext context = getServletContext();
this.connection = ConnectionHandler.getConnection(context);
this.templateEngine = TemplateEngineManager.getTemplateEngine(context);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String username = null;
String password = null;

try {
username = request.getParameter("username");
password = request.getParameter("password");

if (username == null || password == null || username.isBlank() || password.isBlank()) {
throw new Exception("Missing or empty credential values");
}
} catch (Exception e) {
response.sendError( HttpServletResponse.SC_BAD_REQUEST ,"Missing credential value.");
return;
}


UserDAO userdao = new UserDAO(connection);
User user = null;
try {
user = userdao.checkCredentials(username, password);
} catch (SQLException e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Not possible to check credentials.");
}


String path;
if (user == null ) {
ServletContext context = getServletContext();
final WebContext ctx = new WebContext(request, response, context, request.getLocale());
ctx.setVariable("errorMsg", "Incorrect username or password");
path = "index.html";
templateEngine.process(path, ctx, response.getWriter());
} else {
request.getSession().setAttribute("user", user);
path = getServletContext().getContextPath() + "/home";
response.sendRedirect(path);;
}

}

public void destroy() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException sqle) {
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package it.polimi.tiw.taxonomy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import it.polimi.tiw.taxonomy.beans.User;

public class UserDAO {
private Connection connection;

public UserDAO(Connection connection) {
this.connection = connection;
}

public User checkCredentials(String username, String password) throws SQLException {

User user = null;
String query = "SELECT * FROM user WHERE username = ? AND password = ?";

PreparedStatement pstatement = null;
ResultSet result = null;

try {
pstatement = connection.prepareStatement(query);
pstatement.setString(1, username);
pstatement.setString(2, password);
result = pstatement.executeQuery();
while (result.next()) {
user = new User();
user.setId(result.getInt("id"));
user.setName(result.getString("name"));
user.setSurname(result.getString("surname"));
user.setUsername(result.getString("username"));
}
} catch (SQLException e) {
throw e;
}

finally {
try {
if (result != null)
result.close();
} catch (SQLException e1) {
throw e1;
}
try {
if (pstatement != null)
pstatement.close();
} catch (SQLException e1) {
throw e1;
}
}

return user;

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it.polimi.tiw.taxonomy.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.UnavailableException;

public class ConnectionHandler {

public static Connection getConnection(ServletContext context) throws UnavailableException {
try {
String driver = context.getInitParameter("dbDriver");
String url = context.getInitParameter("dbUrl");
String user = context.getInitParameter("dbUser");
String password = context.getInitParameter("dbPassword");
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new UnavailableException("Can't load database driver");
} catch (SQLException e) {
e.printStackTrace();
throw new UnavailableException("Couldn't get db connection");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.polimi.tiw.taxonomy.utils;

import javax.servlet.ServletContext;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

public class TemplateEngineManager {


public static TemplateEngine getTemplateEngine(ServletContext context) {
TemplateEngine templateEngine = new TemplateEngine();
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(context);
templateResolver.setTemplateMode(TemplateMode.HTML);
templateEngine.setTemplateResolver(templateResolver);
templateResolver.setSuffix(".html");
return templateEngine;
}

}
Loading

0 comments on commit f031d9e

Please sign in to comment.